如果已经运行,请停止Hangfire启动定期作业

Jim*_*mbo 8 asp.net-mvc concurrency attributes hangfire

我有一个在Hangfire中设置的定期作业列表,所有作业间隔为20分钟.

它们都调用相同的方法,但参数不同.例如

作业ID test_1 => MyTestMethod(1)
作业ID test_50 => MyTestMethod(50)
作业ID test_37 => MyTestMethod(37)

有时,作业可能需要比其间隔更长的时间,即超过20分钟,我想确保作业不再运行,而另一个实例已经在运行.

DisableConcurrentExecutionAttribute这里是不适合的,因为该方法可以同时运行,只是没有使用相同的参数(S).

我试图有一个静态Dictionary<string, DateTime>跟踪作业是否已经运行,以便它可以阻止并发方法调用,但问题是,如果应用程序因任何原因重新启动,那么它"忘记"当前正在运行的作业(Hangfire of课程将继续在后台运行)

更新
我还尝试将JobState表添加到我的数据库以跟踪正在运行的作业,然后在MyTestMethod启动时检查该表,但这依赖于MyTestMethod设置running = 1,当它启动并运行= 0时结束并且线程崩溃在那个可能不会发生的工作的中途(无论出于何种原因),从而阻止工作再次运行.

我确信我可以通过简单的工作ID查询Hangfire工作状态来解决这个问题 - 我只是无法找到如何做到这一点?

luc*_*uma 7

我有一个类似的要求,基本上我想使用DisableConcurrentExecutionAttribute但是它考虑了参数.这样,如果一个作业使用相同的参数排队,它仍然会不会并行运行.我拿了示例DisableMultipleQueuedItemsFilter,它实际上删除了作业并修改了DisableConcurrentExecutionAttribute以使用参数.不同之处在于,如果作业具有相同的参数列表,则它们将排队,它们不会并行运行.

这里有两个属性可以看到完整的示例:https://gist.github.com/sbosell/3831f5bb893b20e82c72467baf8aefea

属性的相关代码:

 public class DisableConcurrentExecutionWithParametersAttribute : JobFilterAttribute, IServerFilter
    {
        private readonly int _timeoutInSeconds;

        public DisableConcurrentExecutionWithParametersAttribute (int timeoutInSeconds)
        {
            if (timeoutInSeconds < 0) throw new ArgumentException("Timeout argument value should be greater that zero.");

            _timeoutInSeconds = timeoutInSeconds;
        }

        public void OnPerforming(PerformingContext filterContext)
        {
            var resource = GetResource(filterContext.BackgroundJob.Job);

            var timeout = TimeSpan.FromSeconds(_timeoutInSeconds);

            var distributedLock = filterContext.Connection.AcquireDistributedLock(resource, timeout);
            filterContext.Items["DistributedLock"] = distributedLock;
        }

        public void OnPerformed(PerformedContext filterContext)
        {
            if (!filterContext.Items.ContainsKey("DistributedLock"))
            {
                throw new InvalidOperationException("Can not release a distributed lock: it was not acquired.");
            }

            var distributedLock = (IDisposable)filterContext.Items["DistributedLock"];
            distributedLock.Dispose();
        }

        private static string GetFingerprint(Job job)
        {
            var parameters = string.Empty;
            if (job?.Arguments != null)
            {
                parameters = string.Join(".", job.Arguments);
            }
            if (job?.Type == null || job.Method == null)
            {
                return string.Empty;
            }
            var payload = $"{job.Type.FullName}.{job.Method.Name}.{parameters}";
            var hash = SHA256.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(payload));
            var fingerprint = Convert.ToBase64String(hash);
            return fingerprint;
        }
        private static string GetResource(Job job)
        {
            return GetFingerprint(job);
        }
    }
Run Code Online (Sandbox Code Playgroud)


aet*_*boy 5

您可以使用一个属性来防止并发执行(暂时):

[DisableConcurrentExecution(3600)] // argument in seconds, e.g., an hour
public void MyTestMethod(int id) {
  // ...
}
Run Code Online (Sandbox Code Playgroud)

  • 好点子。没注意到你这么说。还有一些其他的东西可以尝试,其中之一是过滤器:https://gist.github.com/odinserj/a8332a3f486773baa009 并查看此处的建议:https://discuss.hangfire.io/t/checking-for- a-工作状态/57/7 (2认同)