Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Orleans.Runtime;
namespace Orleans.Hosting
{
public static class ShutdownTaskSiloBuilderExtensions
{
public static ISiloBuilder AddShutdownTask(
this ISiloBuilder builder,
Func<IServiceProvider, CancellationToken, Task> shutdownTask,
int stage = ServiceLifecycleStage.Active)
{
builder.ConfigureServices(services =>
services.AddTransient<ILifecycleParticipant<ISiloLifecycle>>(sp =>
new ShutdownTask(
sp,
shutdownTask,
stage)));
return builder;
}
/// <inheritdoc />
private class ShutdownTask : ILifecycleParticipant<ISiloLifecycle>
{
private readonly IServiceProvider serviceProvider;
private readonly Func<IServiceProvider, CancellationToken, Task> func;
private readonly int stage;
public ShutdownTask(
IServiceProvider serviceProvider,
Func<IServiceProvider, CancellationToken, Task> func,
int stage)
{
this.serviceProvider = serviceProvider;
this.func = func;
this.stage = stage;
}
/// <inheritdoc />
public void Participate(ISiloLifecycle lifecycle)
{
lifecycle.Subscribe<ShutdownTask>(
stage: this.stage,
onStart: cancellation => Task.CompletedTask,
onStop: cancellation => this.func(this.serviceProvider, cancellation));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.