【发布时间】:2018-02-28 20:38:21
【问题描述】:
我一直致力于在 Azure 传输上使用 NServiceBus 获取长时间运行的消息。基于this document,我认为我可以在单独的线程中启动长进程,将事件处理程序任务标记为完成,然后监听自定义的 OperationStarted 或 OperationComplete 事件。我注意到大多数情况下我的处理程序都没有收到 OperationComplete 事件。事实上,只有在 OperationStarted 事件发布后我立即发布它时才收到它。两者之间的任何实际处理都会以某种方式阻止接收完成事件。这是我的代码:
用于长时间运行消息的抽象类
public abstract class LongRunningOperationHandler<TMessage> : IHandleMessages<TMessage> where TMessage : class
{
protected ILog _logger => LogManager.GetLogger<LongRunningOperationHandler<TMessage>>();
public Task Handle(TMessage message, IMessageHandlerContext context)
{
var opStarted = new OperationStarted
{
OperationID = Guid.NewGuid(),
OperationType = typeof(TMessage).FullName
};
var errors = new List<string>();
// Fire off the long running task in a separate thread
Task.Run(() =>
{
try
{
_logger.Info($"Operation Started: {JsonConvert.SerializeObject(opStarted)}");
context.Publish(opStarted);
ProcessMessage(message, context);
}
catch (Exception ex)
{
errors.Add(ex.Message);
}
finally
{
var opComplete = new OperationComplete
{
OperationType = typeof(TMessage).FullName,
OperationID = opStarted.OperationID,
Errors = errors
};
context.Publish(opComplete);
_logger.Info($"Operation Complete: {JsonConvert.SerializeObject(opComplete)}");
}
});
return Task.CompletedTask;
}
protected abstract void ProcessMessage(TMessage message, IMessageHandlerContext context);
}
测试实施
public class TestLongRunningOpHandler : LongRunningOperationHandler<TestCommand>
{
protected override void ProcessMessage(TestCommand message, IMessageHandlerContext context)
{
// If I remove this, or lessen it to something like 200 milliseconds, the
// OperationComplete event gets handled
Thread.Sleep(1000);
}
}
操作事件
public sealed class OperationComplete : IEvent
{
public Guid OperationID { get; set; }
public string OperationType { get; set; }
public bool Success => !Errors?.Any() ?? true;
public List<string> Errors { get; set; } = new List<string>();
public DateTimeOffset CompletedOn { get; set; } = DateTimeOffset.UtcNow;
}
public sealed class OperationStarted : IEvent
{
public Guid OperationID { get; set; }
public string OperationType { get; set; }
public DateTimeOffset StartedOn { get; set; } = DateTimeOffset.UtcNow;
}
处理程序
public class OperationHandler : IHandleMessages<OperationStarted>
, IHandleMessages<OperationComplete>
{
static ILog logger = LogManager.GetLogger<OperationHandler>();
public Task Handle(OperationStarted message, IMessageHandlerContext context)
{
return PrintJsonMessage(message);
}
public Task Handle(OperationComplete message, IMessageHandlerContext context)
{
// This is not hit if ProcessMessage takes too long
return PrintJsonMessage(message);
}
private Task PrintJsonMessage<T>(T message) where T : class
{
var msgObj = new
{
Message = typeof(T).Name,
Data = message
};
logger.Info(JsonConvert.SerializeObject(msgObj, Formatting.Indented));
return Task.CompletedTask;
}
}
我确定context.Publish() 调用正在被命中,因为_logger.Info() 调用正在向我的测试控制台打印消息。我还验证了它们被断点击中。在我的测试中,任何运行时间超过 500 毫秒的东西都会阻止 OperationComplete 事件的处理。
如果有人可以就在 ProcessMessage 实现中经过任何大量时间时 OperationComplete 事件未触发处理程序的原因提出建议,我将非常感激听到他们的声音。谢谢!
-- 更新-- 万一其他人遇到这个并对我最终做了什么感到好奇:
在an exchange 与 NServiceBus 的开发人员合作之后,我决定使用实现 IHandleTimeouts 接口的 watchdog saga 来定期检查作业是否完成。我正在使用在作业完成时更新的 saga 数据来确定是否在超时处理程序中触发 OperationComplete 事件。这带来了另一个问题:当使用 In-Memory Persistence 时,saga 数据在线程间为not persisted,即使它被每个线程锁定。为了解决这个问题,我专门为长期运行的内存数据持久性创建了一个接口。该接口作为单例注入到 saga 中,因此用于跨线程读取/写入 saga 数据以进行长时间运行的操作。
我知道不建议使用 In-Memory Persistence,但根据我的需要配置另一种类型的持久性(如 Azure 表)是多余的;我只是想让OperationComplete 事件在正常情况下触发。如果在运行作业期间发生重新启动,我不需要保留传奇数据。无论如何,该作业将被缩短,如果作业运行时间超过设定的最长时间,saga 超时将处理触发 OperationComplete 事件并出现错误。
【问题讨论】:
-
这个问题也得到了官方 NServiceBus GitHub 仓库的开发人员的回答:github.com/Particular/NServiceBus/issues/5121
-
谢谢,@Sabacc,我更新了我的答案,以链接到我用 NServiceBus 打开的问题,并解释我的最终解决方案是什么。
标签: c# .net multithreading nservicebus