Пример #1
0
        private async Task <int> RunApplicationEntryPoint([NotNull] CancellationTokenSource cts)
        {
            try
            {
                int exitcode;
                try
                {
                    exitcode = await _ApplicationEntryPoint.Execute(cts.Token);
                }
                finally
                {
                    _ApplicationLifetimeManager.SignalGracefulTermination();
                    await _BackgroundServicesManager.WaitForBackgroundServicesToStop();
                }

                return(exitcode);
            }
            catch (TaskCanceledException)
            {
                if (!WasCancelledByUser && !_ApplicationLifetimeManager.GracefulTerminationCancellationToken.IsCancellationRequested)
                {
                    System.Console.Error.WriteLine("program terminated early, unknown reason");
                }

                return(1);
            }
            catch (Exception ex) when(!Debugger.IsAttached)
            {
                Logger.LogException(ex);
                throw;
            }
        }
Пример #2
0
        private void OnConsoleOnCancelKeyPress(object s, ConsoleCancelEventArgs e)
        {
            Logger.Log(LogLevel.Debug, "User cancelled application with Ctrl+C");
            WasCancelledByUser = true;
            if (e != null)
            {
                e.Cancel = true;
            }

            _CtrlCCancellationTokenSource.Cancel();
            _ApplicationLifetimeManager.SignalGracefulTermination();
            Bus.PublishAsync(new UserCancellationKeypressMessage());
        }
Пример #3
0
        protected override void OnStop()
        {
            using (_Logger.LogScope(LogLevel.Debug, $"Stopping background services in '{ServiceName}'"))
            {
                _Logger.LogInformation($"Stopping background services in '{ServiceName}'");

                lock (_Lock)
                {
                    _CancellationTokenRegistration?.Dispose();
                    _CancellationTokenRegistration = null;
                }

                _ApplicationLifetimeManager.SignalGracefulTermination();
                _BackgroundServicesManager.WaitForBackgroundServicesToStop().ConfigureAwait(false).GetAwaiter().GetResult();

                _Logger.LogInformation($"Background services in '{ServiceName}' stopped successfully");
            }

            base.OnStop();
        }
        public async Task Execute(CancellationToken cancellationToken)
        {
            if (PidFilePaths.Length == 0)
            {
                return;
            }

            DeleteQuitFiles();

            while (!cancellationToken.IsCancellationRequested)
            {
                await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken).NotNull();

                if (AnyQuitFilesExists())
                {
                    _ApplicationLifetimeManager.SignalGracefulTermination();
                    DeleteQuitFiles();
                }
            }
        }
        private async Task RunBackgroundService(CancellationToken cancellationToken)
        {
            await Task.Yield();

            try
            {
                await _BackgroundService.Execute(cancellationToken);
            }
            catch (TaskCanceledException)
            {
            }
            catch (Exception ex)
            {
                _Logger.LogException(ex);
                _Logger.LogError(
                    $"background service '{_TypeHelper.NameOf(_BackgroundService.GetType())}' threw an exception, terminating program");

                // TODO: Still do this?
                _ApplicationLifetimeManager.SignalGracefulTermination();
            }
        }
Пример #6
0
 public void Execute()
 {
     _Logger.LogInformation("User selected to exit application, terminating gracefully");
     _ApplicationLifetimeManager.SignalGracefulTermination();
 }
Пример #7
0
        public static void RunWinFormsMainWindow <T>(bool useBackgroundServices)
            where T : class, IServicesBootstrapper
        {
            var container = ContainerFactory.Bootstrap <ServicesBootstrapper <T> >();

            IBackgroundServicesManager  backgroundServicesManager  = container.Resolve <IBackgroundServicesManager>().NotNull();
            IApplicationLifetimeManager applicationLifetimeManager = container.Resolve <IApplicationLifetimeManager>();

            if (useBackgroundServices)
            {
                backgroundServicesManager.StartBackgroundServices();
            }

            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                Form mainForm = container.Resolve <Form>(serviceKey: "MainForm");

                bool closingHasBeenHandled = true;
                applicationLifetimeManager.GracefulTerminationCancellationToken.Register(
                    () =>
                {
                    if (closingHasBeenHandled)
                    {
                        return;
                    }

                    closingHasBeenHandled = true;
                    var close             = new Action(() => mainForm.Close());
                    if (mainForm.InvokeRequired)
                    {
                        mainForm.BeginInvoke(close);
                    }
                    else
                    {
                        close();
                    }
                });

                mainForm.FormClosed += (s, e) =>
                {
                    closingHasBeenHandled = true;
                    applicationLifetimeManager.SignalGracefulTermination();
                };

                Application.Run(mainForm);
            }
            catch (Exception) when(!Debugger.IsAttached)
            {
                Environment.ExitCode = 1;
                throw;
            }
            finally
            {
                if (useBackgroundServices)
                {
                    backgroundServicesManager.WaitForBackgroundServicesToStop().GetAwaiter().GetResult();
                }
            }
        }
 private void SessionEnding(object sender, [NotNull] SessionEndingEventArgs e)
 {
     _Logger.Log(LogLevel.Information, $"Session is ending with reason '{e.Reason}', application gracefully shutting down");
     _ApplicationLifetimeManager.SignalGracefulTermination();
 }
Пример #9
0
 public Task <bool> Handle(StopApplicationCommand input)
 {
     _ApplicationLifetimeManager.SignalGracefulTermination();
     return(Task.FromResult(true));
 }