【问题标题】:Hangfire job automatically rescheduled after 1 min when using MSMQ with DTC将 MSMQ 与 DTC 一起使用时,Hangfire 作业会在 1 分钟后自动重新安排
【发布时间】:2016-05-24 14:01:03
【问题描述】:

我正在使用带有 MSMQ 和 DTC 事务的 Hangfire 1.5.6(为了使用远程 MSMQ)。问题是每个长时间运行的作业(> 1 分钟)都会在 1 分钟后自动重新安排。这也是以奇怪的顺序完成的:在取消旧作业之前再次启动作业。

如果我将 Hangfire 配置为不使用 DTC 事务,作业运行正常。

在源代码中,我注意到 MsmqDtcTransaction 正在打开 TransactionScope。在作业执行期间是否需要打开此范围?对于长时间运行的作业(此特定作业将大量数据插入数据库),SQL Server 事务日志会发生什么情况?

我尝试在 app.config 中设置事务超时(这也需要在 machine.config 中进行更改):

<system.transactions>
<machineSettings maxTimeout="02:00:00"/>
<defaultSettings timeout="02:00:00" />
</system.transactions>

有了这些更改,作业在 DTC 事务上运行正常。

这就是 Hangfire 与远程 MSMQ 的工作方式吗?可以不用交易吗?

【问题讨论】:

  • "在作业执行期间是否需要打开 TransactionScope?"取决于如果 SQL 作业失败,是否应在源队列中恢复 MSMQ 消息。
  • 你能解决这个问题吗?我们在使用远程 MSMQ 时遇到了同样的问题。长时间运行的作业每分钟重试一次。

标签: c# msmq hangfire


【解决方案1】:

Hangfire 作者已在 1.6.3 中签入对此的修复:Hangfire Forums

来自 Hangfire 源代码:

public MsmqDtcTransaction()
{
   _scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.Zero);
}

public Message Receive(MessageQueue queue, TimeSpan timeout)
{
    var message = queue.Receive(timeout, MessageQueueTransactionType.Automatic);
    _suppressedScope = new TransactionScope(TransactionScopeOption.Suppress, TimeSpan.Zero);

    return message;
}

【讨论】:

  • 我已经测试了 1.6.5 版本,没有 1 分钟的超时,而是 10 分钟的超时,这是 Windows 最大事务超时。有没有人尝试在 DTC 开启的情况下运行超过 10 分钟的作业?
猜你喜欢
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
相关资源
最近更新 更多