我正在使用带有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 一起工作的方式吗?可以在没有交易的情况下使用吗?
作者已在 1.6.3 中对此进行了修复: Hangfire 论坛
来自 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;
}