本文介绍了如何防止Hangfire重复作业在连续执行30分钟后重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个asp.net mvc-5 Web应用程序,我在使用Hangfire工具运行长时间运行的后台作业时遇到了问题。 问题是,如果作业执行时间超过30分钟,那么hangfire将自动启动另一个作业,因此我最终会同时运行两个类似的作业。

I am working on an asp.net mvc-5 web application, and I am facing a problem in using Hangfire tool to run long running background jobs. the problem is that if the job execution exceed 30 minutes , then hangfire will automatically initiate another job, so i will end up having two similar jobs running at the same time.

现在我有以下内容: -

Now I have the following:-


  1. Asp.net mvc-5

  2. IIS-8

  3. Hangfire 1.4.6

  4. Windows server 2012

  1. Asp.net mvc-5
  2. IIS-8
  3. Hangfire 1.4.6
  4. Windows server 2012

现在我已经定义了一个每天17:00运行的篝火重复工作。后台作业主要扫描我们的网络中的服务器和虚拟机并更新数据库,定期作业将在完成执行后发送电子邮件。
定期工作在执行时间不到30分钟时效果很好。但是今天随着我们的系统的发展,经常性的工作在40分钟后完成,而不是像过去那样在22-25分钟之后完成。我收到了2封电子邮件,而不是一封电子邮件(电子邮件之间的时间约为30分钟)。现在我手动重新运行这个工作,我注意到问题如下: -

Now I have defined a hangfire recurring job to run at 17:00 each day. The background job mainly scan our network for servers and vms and update the DB, and the recurring job will send an email after completing the execution.The recurring job used to work well when its execution was less than 30 minutes. But today as our system grow, the recurring job completed after 40 minutes instead of 22-25 minutes as it used to be. and I received 2 emails instead of one email (and the time between the emails was around 30 minutes). Now I re-run the job manually and I have noted that that the problem is as follow:-

现在,如果定期工作不到30分钟(例如29分钟),我将不会遇到任何问题,但如果重复执行的工作超过30分钟然后由于某种原因或另一个篝火将启动一项新工作。
虽然当我在作业执行期间访问hangefire仪表板时,我发现只有一个活动作业,但是当我监视我们的数据库时,我可以从sql profiler看到有两个作业访问数据库。这种情况发生在经常性工作开始30分钟后(在我们的案例中是17:30),这就是为什么我收到2封电子邮件,这意味着2个重复工作在后台运行而不是一个......

Now if the recurring job take less than 30 minutes (for example 29 minute) I will not face any problem, but if the recurring job execution exceed 30 minutes then for a reason or another hangfire will initiate a new job .although when I access the hangefire dashboard during the jobs execution, I can find that there is only one active job, but when I monitor our DB i can see from the sql profiler that there are two jobs accessing the DB. this happen after 30 minutes from the beginning of the recurring job (at 17:30 in our case) ,, and that why I received 2 emails which mean 2 recurring jobs were running in the background instead of one ..

所以有人可以就此提出建议吗,如果目前的重复工作执行时间超过30分钟,我怎能避免因自动启动新的定期工作而引发的篝火?
谢谢

So can anyone advice on this please, how I can avoid hangfire from automatically initiating a new recurring job if the current recurring job execution exceed 30 minutes?Thanks

推荐答案

您是否看过 InvisibilityTimeout 设置 ?

要使其与其他worker不可见,带有
OUTPUT子句的UPDATE语句用于获取排队的作业并更新FetchedAt
值(表示已获取的其他工作者)
原子方式。其他工作人员看到获取的时间戳并忽略了工作。
但是为了处理流程终止,他们将在指定的时间内忽略一个工作
(默认为30分钟)。

To make it invisible from other workers, the UPDATE statement with OUTPUT clause is used to fetch a queued job and update the FetchedAt value (that signals for other workers that it was fetched) in an atomic way. Other workers see the fetched timestamp and ignore a job. But to handle the process termination, they will ignore a job only during a specified amount of time (defaults to 30 minutes).

虽然这种机制确保每个作业都会被处理,有时它会导致长的重试延迟或导致多个
作业执行。请考虑以下情形:

Although this mechanism ensures that every job will be processed, sometimes it may cause either long retry latency or lead to multiple job execution. Consider the following scenario:


  1. 工人A获取一份工作(运行一小时)并在12:00开始工作。

  2. 工人B在12:30获取相同的工作,因为默认的隐身超时已过期。

  3. 工人C(没有获取)13的同一工作: 00,因为(成功演出后
    将被删除。)

如果您使用取消令牌,它将是为工人A设置为
12:30,工作人员B设置为13:00。这可能导致您的
长期工作永远不会被执行。如果你没有使用
取消令牌,它将由WorkerA和
工人B同时执行(自12:30开始),但是工人C将不会获取它,因为它
将是成功演出后删除。

If you are using cancellation tokens, it will be set for Worker A at 12:30, and at 13:00 for Worker B. This may lead to the fact that your long-running job will never be executed. If you aren’t using cancellation tokens, it will be concurrently executed by WorkerA and Worker B (since 12:30), but Worker C will not fetch it, because it will be deleted after successful performance.

因此,如果您有长时间运行的作业,最好配置
隐身超时间隔:



var options = new SqlServerStorageOptions
{
    InvisibilityTimeout = TimeSpan.FromMinutes(30) // default value
};

GlobalConfiguration.Configuration.UseSqlServerStorage("<name or connection string>", options);

截至。正在处理的工作对其他工人是不可见的。

As of Hangfire 1.5 this option is now Obsolete. Jobs that are being worked on are invisible to other workers.

即使在非正常关闭之后,该作业也是如此将立即为其他
工作人员提供,不会有任何延迟。

Even after ungraceful shutdown, the job will be available for other workers instantly, without any delays.

这篇关于如何防止Hangfire重复作业在连续执行30分钟后重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:59