Skip to content

Instantly share code, notes, and snippets.

@ReubenBond
Created May 14, 2019 14:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReubenBond/328c3aa623372c876550d0b9efd442ac to your computer and use it in GitHub Desktop.
Save ReubenBond/328c3aa623372c876550d0b9efd442ac to your computer and use it in GitHub Desktop.
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