コード例 #1
0
        /// <summary>
        /// Demonstrates how to run (and stop) an infinite worker, 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();

            var infiniteRunner = InfiniteDeadManSwitchRunner.Create(loggerFactory);
            var worker         = new ExampleInfiniteWorker();

            using (var cancellationTokenSource = new CancellationTokenSource())
            {
                var options = new DeadManSwitchOptions
                {
                    Timeout = TimeSpan.FromSeconds(60)
                };
                // do not await this, it will never complete until you cancel the token
                var run = infiniteRunner.RunAsync(worker, options, cancellationTokenSource.Token);

                // let it run for 10s.
                await Task.Delay(TimeSpan.FromSeconds(10), cancellationTokenSource.Token).ConfigureAwait(false);

                // now stop the infinite worker
                cancellationTokenSource.Cancel();

                // let it finish gracefully
                await run.ConfigureAwait(false);
            }
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
0
 public DeadManSwitchBenchmarks()
 {
     _deadManSwitchRunner         = DeadManSwitchRunner.Create();
     _infiniteDeadManSwitchRunner = InfiniteDeadManSwitchRunner.Create();
     _deadManSwitchOptions        = new DeadManSwitchOptions
     {
         Timeout = TimeSpan.FromSeconds(1),
         NumberOfNotificationsToKeep = 5
     };
 }
コード例 #4
0
        public TestsForInfiniteDeadManSwitchRunner(ITestOutputHelper testOutputHelper)
        {
            var logger = new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .Enrich.FromLogContext()
                         .Enrich.WithThreadId()
                         .WriteTo.TestOutput(testOutputHelper,
                                             outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:w5}] #{ThreadId,-3} {SourceContext} {Message}{NewLine}{Exception}")
                         .CreateLogger();

            _loggerFactory = LoggerFactory.Create(builder => { builder.AddSerilog(logger); });
            var loggerFactory = new TestLoggerFactory(_loggerFactory);

            _logger         = _loggerFactory.CreateLogger <TestsForInfiniteDeadManSwitchRunner>();
            _sessionFactory = new CapturingDeadManSwitchSessionFactory(new DeadManSwitchSessionFactory(loggerFactory));
            _runner         = new InfiniteDeadManSwitchRunner(loggerFactory.CreateLogger <InfiniteDeadManSwitchRunner>(), _sessionFactory);
        }