Created
May 14, 2019 14:47
-
-
Save ReubenBond/328c3aa623372c876550d0b9efd442ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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