如何在两次同时运行时取消重复作业?

时间:2016-07-25 08:48:29

标签: c# hangfire

我在作业中添加了一个属性DisableConcurrentExecution(1),但所有这一切都会延迟第二个作业实例的执行,直到第一个作业完成为止。我希望能够检测何时运行并发作业,然后一起取消它。

我想,如果DisableConcurrentExecution(1)会阻止同一个周期作业的两个实例同时运行,它会将第二个作业置于“重试”状态,从而改变它的状态。所以我在作业上添加了额外的自定义属性,它检测失败状态,如下所示:

public class StopConcurrentTask : JobFilterAttribute, IElectStateFilter
{
    public void OnStateElection(ElectStateContext context)
    {
        var failedState = context.CandidateState as FailedState;
        if(failedState != null && failedState.Exception != null)
        {
            if(!string.IsNullOrEmpty(failedState.Exception.Message) && failedState.Exception.Message.Contains("Timeout expired. The timeout elapsed prior to obtaining a distributed lock on"))
            {

            }
        }
    }
}

这使我能够检测作业是否因与同一作业的另一个实例同时运行而失败。问题是,我找不到取消此特定失败作业的方法,并将其从重新运行中删除。就像现在一样,这项工作将被重新安排,而Hangfire将尝试多次运行。

我当然可以在Job上添加一个属性,确保它根本不重试。但是,这不是一个有效的解决方案,因为我想要重新编写作业,除非它们因并发运行而失败。

2 个答案:

答案 0 :(得分:3)

如果您在OnPerformed界面的IServerFilter方法中进行验证,则可以阻止重试。

实施:

public class StopConcurrentTask : JobFilterAttribute, IElectStateFilter, IServerFilter
    {
        // All failed after retry will be catched here and I don't know if you still need this
        // but it is up to you
        public void OnStateElection(ElectStateContext context)
        {
            var failedState = context.CandidateState as FailedState;
            if (failedState != null && failedState.Exception != null)
            {
                if (!string.IsNullOrEmpty(failedState.Exception.Message) && failedState.Exception.Message.Contains("Timeout expired. The timeout elapsed prior to obtaining a distributed lock on"))
                {

                }
            }
        }

        public void OnPerformed(PerformedContext filterContext)
        {
            // Do your exception handling or validation here
            if (filterContext.Exception == null) return;

            using (var connection = _jobStorage.GetConnection())
            {
                var storageConnection = connection as JobStorageConnection;

                if (storageConnection == null)
                    return;

                var jobId = filterContext.BackgroundJob.Id
                // var job = storageConnection.GetJobData(jobId); -- If you want job detail

                var failedState = new FailedState(filterContext.Exception)
                {
                    Reason = "Your Exception Message or filterContext.Exception.Message"
                };

                using (var transaction = connection.GetConnection().CreateWriteTransaction())
                {
                    transaction.RemoveFromSet("retries", jobId);  // Remove from retry state
                    transaction.RemoveFromSet("schedule", jobId); // Remove from schedule state
                    transaction.SetJobState(jobId, failedState);  // update status with failed state
                    transaction.Commit();
                }
            }
        }

        public void OnPerforming(PerformingContext filterContext)
        {
           // Do nothing
        }
    }

我希望这会对你有所帮助。

答案 1 :(得分:0)

我实际上最终使用基于Jr Tabuloc的答案 - 如果它在15秒前被最后执行,它将删除一个作业 - 我注意到服务器唤醒和作业执行之间的时间不同。通常它是以毫秒为单位,但由于我的工作每天执行一次,我认为15秒不会受到伤害。

public class StopWakeUpExecution : JobFilterAttribute, IServerFilter
{
    public void OnPerformed(PerformedContext filterContext)
    {

    }

    public void OnPerforming(PerformingContext filterContext)
    {
        using (var connection = JobStorage.Current.GetConnection())
        {
            var recurring = connection.GetRecurringJobs().FirstOrDefault(p => p.Job.ToString() == filterContext.BackgroundJob.Job.ToString());
            TimeSpan difference = DateTime.UtcNow.Subtract(recurring.LastExecution.Value);
            if (recurring != null && difference.Seconds < 15)
            {
                // Execution was due in the past. We don't want to automaticly execute jobs after server crash though.

                var storageConnection = connection as JobStorageConnection;

                if (storageConnection == null)
                    return;

                var jobId = filterContext.BackgroundJob.Id;

                var deletedState = new DeletedState()
                {
                    Reason = "Task was due in the past. Please Execute manually if required."
                };

                using (var transaction = connection.CreateWriteTransaction())
                {
                    transaction.RemoveFromSet("retries", jobId);  // Remove from retry state
                    transaction.RemoveFromSet("schedule", jobId); // Remove from schedule state
                    transaction.SetJobState(jobId, deletedState);  // update status with failed state
                    transaction.Commit();
                }
            }
        }
    }
}