HangFire循环作业中作业因执行时间太长未完成新作业开启导致重复数据的问题...

针对HangFire中由于作业执行时间过长,新作业启动导致的重复数据问题,可以使用Mutex特性来解决。当作业未完成时,新作业会被加入到计划队列等待,直至前一个作业完成。参考链接:https://github.com/HangfireIO/Hangfire/issues/1053
摘要由CSDN通过智能技术生成

解决方法:在执行的任务方法前加上Mutex特性即可,如果作业未完成,新作业开启的话,新作业会放入计划中的作业队列中,直到前面的作业完成。

必须使用Hangfire.Pro.Redis 和 Hangfire.SqlServer 作为数据库。

参考:https://github.com/HangfireIO/Hangfire/issues/1053

  [Mutex("DownloadVideo")]
  public async Task DownloadVideo()
  {
    
}

 

Mutex特性代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using Hangfire.Common;
using Hangfire.States;
using Hangfire.Storage;

namespace Hangfire.Pro
{
    /// <summary>
    /// Represents a background job filter that helps to disable concurrent execution
    /// without causing worker to wait as in <see cref="Hangfire.DisableConcurrentExecutionAttribute"/>.
    /// </summary>
    public class MutexAttribute : JobFilterAttribute, IElectStateFilter, IApplyStateFilter
    {
        private static readonly TimeSpan DistributedLockTimeout = TimeSpan.FromMinutes(1);

        private readonly string _resource;

        public MutexAttribute(string resource)
        {
            _resource = resource;
            RetryInSeconds = 15;
        }

        public int RetryInSeconds { get; set; }
        public int MaxAttempts { get; set; }

        public void OnS
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值