奥尔良提醒执行是否交错?

Mr.*_*oor 4 orleans

如果同一个grain激活有两个不同的提醒在同一点被触发,假设grain执行上下文是单线程的,那么两个提醒是否会同时执行和交错?

另外,提醒执行是否受到默认 30 秒超时的限制?

Reu*_*ond 7

使用常规grain方法调用来调用提醒:该IRemindable接口是常规grain接口。IRemindable.ReceiveReminder(...)未标记为[AlwaysInterleave],因此仅当您的谷物类别标记为 时才会交错[Reentrant]

简而言之:不,默认情况下提醒调用不会交错。

提醒不会覆盖该SiloMessagingOptions.ResponseTimeout值,因此默认执行时间为 30 秒。

如果您有可能需要很长时间才能执行的提醒,则可以遵循以下模式:在后台任务中启动长时间运行的工作,并确保每当相关提醒触发时它仍在运行(未完成或出现故障)。

这是使用该模式的示例:

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.
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)