gpt4 book ai didi

orleans - 奥尔良提醒执行是交错的吗?

转载 作者:行者123 更新时间:2023-12-02 01:44:45 30 4
gpt4 key购买 nike

如果在同一个grain activation上有两个不同的reminders在同一个点触发,假设grain execution context是单线程的,两个reminders会同时执行和交错吗?

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

最佳答案

提醒是使用常规的 grain 方法调用来调用的:IRemindable 接口(interface)是一个常规的 grain 接口(interface)。 IRemindable.ReceiveReminder(...) 没有被标记为 [AlwaysInterleave],所以它只会在你的 grain 类被标记为 [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.
}
}
}
}

关于orleans - 奥尔良提醒执行是交错的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71065057/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com