/// <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);
            }
        }
Пример #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);
        }
 public DeadManSwitchBenchmarks()
 {
     _deadManSwitchRunner         = DeadManSwitchRunner.Create();
     _infiniteDeadManSwitchRunner = InfiniteDeadManSwitchRunner.Create();
     _deadManSwitchOptions        = new DeadManSwitchOptions
     {
         Timeout = TimeSpan.FromSeconds(1),
         NumberOfNotificationsToKeep = 5
     };
 }
        public TestsForDeadManSwitchRunner(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 <TestsForDeadManSwitchRunner>();
            _sessionFactory = new CapturingDeadManSwitchSessionFactory(new DeadManSwitchSessionFactory(loggerFactory));
            _runner         = new DeadManSwitchRunner(loggerFactory.CreateLogger <DeadManSwitchRunner>(), _sessionFactory);
        }
Пример #5
0
        /// <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);
            }
        }