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

本文关键字:作业 两次 取消 运行 | 更新日期: 2023-09-27 17:58:33

我在作业上添加了属性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将尝试多次运行它。

我当然可以在作业上设置一个属性,确保它根本不会重试。但是,这不是一个有效的解决方案,因为我希望对作业进行重试,除非它们由于并发运行而失败。

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

如果在IServerFilter接口的OnPerformed方法中进行验证,则可以防止重试。

实施:

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
        }
    }

我希望这对你有帮助。

我最终使用了基于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();
                }
            }
        }
    }
}