如果在同一点启动同一个晶粒激活上有两个不同的提醒,给定晶粒执行上下文是单线程的,那么两个提醒会同时执行和交错吗?
此外,提醒执行是否受默认的30秒超时限制?
提醒使用常规粒度方法调用:IRemindable
接口是一个常规粒度接口。IRemindable.ReceiveReminder(...)
没有标记为[AlwaysInterleave]
,因此只有当您的grain类标记为[Reentrant]
时,它才会交错。
简而言之:不,默认情况下,提醒呼叫不交错。
提醒不会覆盖SiloMessagingOptions.ResponseTimeout
值,因此默认执行时间为30s。
如果您有一个可能需要很长时间才能执行的提醒,您可以遵循在后台任务中启动长时间运行的工作,并确保在相关提醒触发时它仍在运行(未完成或出现故障(的模式。
以下是使用该模式的示例:
public class MyGrain : Grain, IMyGrain
{
private readonly CancellationTokenSource _deactivating = new CancellationTokenSource();
private Task _processQueueTask;
private IGrainReminder _reminder = null;
public Task ReceiveReminder(string reminderName, TickStatus status)
{
// Ensure that the reminder task is running.
if (_processQueueTask is null || _processQueueTask.IsCompleted)
{
if (_processQueueTask?.Exception is Exception exception)
{
// Log that an error occurred.
}
_processQueueTask = DoLongRunningWork();
_processQueueTask.Ignore();
}
return Task.CompletedTask;
}
public override async Task OnActivateAsync()
{
if (_reminder != null)
{
return;
}
_reminder = await RegisterOrUpdateReminder(
"long-running-work",
TimeSpan.FromMinutes(1),
TimeSpan.FromMinutes(1)
);
}
public override async Task OnDeactivateAsync()
{
_deactivating.Cancel(throwOnFirstException: false);
Task processQueueTask = _processQueueTask;
if (processQueueTask != null)
{
// Optionally add some max deactivation timeout here to stop waiting after (eg) 45 seconds
await processQueueTask;
}
}
public async Task StopAsync()
{
if (_reminder == null)
{
return;
}
await UnregisterReminder(_reminder);
_reminder = null;
}
private async Task DoLongRunningWork()
{
// Log that we are starting the long-running work
while (!_deactivating.IsCancellationRequested)
{
try
{
// Do long-running work
}
catch (Exception exception)
{
// Log exception. Potentially wait before retrying loop, since it seems like GetMessageAsync may have failed for us to end up here.
}
}
}
}