/// <summary> /// Demonstrates how you can run a worker once, using a dead man's switch /// </summary> public static async Task Main() { // This example uses NLog, but it only requires a trivial amount of code to use any other logging library. var loggerFactory = new NLoggerFactory(); // You can also use Create() which disables logging var runner = DeadManSwitchRunner.Create(loggerFactory); var worker = new ExampleWorker(); using (var cancellationTokenSource = new CancellationTokenSource()) { var options = new DeadManSwitchOptions { Timeout = TimeSpan.FromSeconds(60) }; var run = runner.RunAsync(worker, options, cancellationTokenSource.Token); // if you want to cancel at some point: cancellationTokenSource.Cancel(); var result = await run.ConfigureAwait(false); Debug.Assert(result == Math.PI); } }
/// <summary> /// Adds the dead man's switch to the provided <see cref="IServiceCollection"/> /// </summary> public static IServiceCollection AddDeadManSwitch(this IServiceCollection serviceCollection) { serviceCollection.AddSingleton <IDeadManSwitchLoggerFactory, DeadManSwitchLoggerFactory>(); serviceCollection.AddSingleton(sp => DeadManSwitchRunner.Create(sp.GetRequiredService <IDeadManSwitchLoggerFactory>())); serviceCollection.AddSingleton(sp => InfiniteDeadManSwitchRunner.Create(sp.GetRequiredService <IDeadManSwitchLoggerFactory>())); return(serviceCollection); }
public DeadManSwitchBenchmarks() { _deadManSwitchRunner = DeadManSwitchRunner.Create(); _infiniteDeadManSwitchRunner = InfiniteDeadManSwitchRunner.Create(); _deadManSwitchOptions = new DeadManSwitchOptions { Timeout = TimeSpan.FromSeconds(1), NumberOfNotificationsToKeep = 5 }; }
/// <summary> /// Demonstrates how you can run a worker once, using a dead man's switch /// </summary> public static async Task Main() { var loggerFactory = new NLoggerFactory(); var runner = DeadManSwitchRunner.Create(loggerFactory); var worker = new ExampleWorker(); using (var cancellationTokenSource = new CancellationTokenSource()) { var options = new DeadManSwitchOptions { Timeout = TimeSpan.FromSeconds(60) }; var run = runner.RunAsync(worker, options, cancellationTokenSource.Token); // if you want to cancel at some point: cancellationTokenSource.Cancel(); var result = await run.ConfigureAwait(false); Debug.Assert(result == Math.PI); } }