并发运行两次时如何取消循环作业?

How to cancel a recurrent job when it runs concurrently twice?

我在作业上添加了一个属性 DisableConcurrentExecution(1),但所做的只是将作业的第二个实例的执行延迟到第一个实例完成之后。我希望能够检测到并发作业何时 运行,然后将其全部取消。

我想,如果 DisableConcurrentExecution(1) 会阻止同一循环作业的两个实例同时 运行 宁,它会将第二个作业放在 "retry" 上,从而改变它的状态.所以我在作业上添加了额外的自定义属性,它检测失败状态,如下所示:

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();
                }
            }
        }
    }
}