在我的一个编排中,计时器根据编排输入对象中指定的日期获取。有时需要在编排开始后更新此日期。我目前正在尝试实现一个事件,该事件取消现有计时器并创建一个具有更新日期的新计时器。
我能够成功取消现有计时器,并且我也能够设置新计时器,但新计时器似乎没有启动。
我的编排中的 onEvent 方法根据输入查找所需的事件处理程序,然后调用 HandleProcess 方法。
public override async void OnEvent(OrchestrationContext context, string eventName, string input)
{
IEventHandler eventHandler = await context.ScheduleTask<IEventHandler>(typeof(GetEventHandler), eventName);
EventHandlerInput eventInput = new EventHandlerInput(context, customState, input);
customState = eventHandler.HandleProcess(eventInput);
}
日期更改事件处理程序 HandleProcess 方法取消当前计时器并安排一个新计时器
public CustomOrchestrationState HandleProcess<T>(T input)
{
EventHandlerInput customInput = (input as EventHandlerInput);
if (!customInput.CustomOrchestrationState.AllowActionDateChange)
{
//date change not allowed
throw new Exception();
}
DateTime newDate = new DateTime();
try
{
newDate = Convert.ToDateTime(customInput.EventInput);
}
catch (Exception ex)
{
//Invalid Date format
}
CancellationTokenSource newToken = new CancellationTokenSource();
customInput.OrchestrationContext.CreateTimer(newDate, customInput, newToken.Token);
customInput.CustomOrchestrationState.TimerCancelationToken?.Dispose();
CancelCurrentTimer(customInput.CustomOrchestrationState);
customInput.CustomOrchestrationState.ActionDate = newDate;
customInput.CustomOrchestrationState.TimerCancelationToken = newToken;
return customInput.CustomOrchestrationState;
}
在 SQL 数据库中,我可以看到新计时器已创建,但当新计时器应该触发时,编排似乎仍然停滞不前。
此行为是预期行为还是错误?我是否遗漏了什么或做错了什么?
DateTime
API 不应该在协调器中使用,因为它们是不确定的。请参阅持久的协调器约束。
我认为您可能需要以
IDurableOrchestrationContext.CurrentUtcDateTime
为基础,并从那里进行任何日期数学计算。