public void Execute(BackgroundProcessContext context)
        {
            _logger.Trace($"{BackgroundServerProcess.GetServerTemplate(context.ServerId)} waiting for {_interval} delay before sending a heartbeat");

            context.ShutdownToken.Wait(_interval);
            context.ShutdownToken.ThrowIfCancellationRequested();

            try
            {
                using (var connection = context.Storage.GetConnection())
                {
                    connection.Heartbeat(context.ServerId);
                }

                if (_faultedSince == null)
                {
                    _logger.Debug($"{BackgroundServerProcess.GetServerTemplate(context.ServerId)} heartbeat successfully sent");
                }
                else
                {
                    _logger.Info($"{BackgroundServerProcess.GetServerTemplate(context.ServerId)} is now able to continue sending heartbeats");
                    _faultedSince = null;
                }
            }
            catch (BackgroundServerGoneException)
            {
                if (!context.ShutdownToken.IsCancellationRequested)
                {
                    _logger.Warn($"{BackgroundServerProcess.GetServerTemplate(context.ServerId)} was considered dead by other servers, restarting...");
                    _requestRestart();
                }

                return;
            }
            catch (Exception ex)
            {
                _logger.WarnException($"{BackgroundServerProcess.GetServerTemplate(context.ServerId)} encountered an exception while sending heartbeat", ex);

                if (_faultedSince == null)
                {
                    _faultedSince = Stopwatch.StartNew();
                }
                if (_faultedSince.Elapsed >= _serverTimeout)
                {
                    _logger.Error($"{BackgroundServerProcess.GetServerTemplate(context.ServerId)} will be restarted due to server time out");

                    _requestRestart();
                    return;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BackgroundProcessingServer"/>
        /// class and immediately starts all the given background processes.
        /// </summary>
        internal BackgroundProcessingServer(
            [NotNull] BackgroundServerProcess process,
            [NotNull] BackgroundProcessingServerOptions options)
        {
            _process = process ?? throw new ArgumentNullException(nameof(process));
            _options = options ?? throw new ArgumentNullException(nameof(options));

            _dispatcher = CreateDispatcher();

#if !NETSTANDARD1_3
            AppDomain.CurrentDomain.DomainUnload += OnCurrentDomainUnload;
            AppDomain.CurrentDomain.ProcessExit  += OnCurrentDomainUnload;
#endif
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BackgroundProcessingServer"/>
        /// class and immediately starts all the given background processes.
        /// </summary>
        internal BackgroundProcessingServer(
            [NotNull] BackgroundServerProcess process,
            [NotNull] BackgroundProcessingServerOptions options)
        {
            _process = process ?? throw new ArgumentNullException(nameof(process));
            _options = options ?? throw new ArgumentNullException(nameof(options));

            _dispatcher = CreateDispatcher();

#if !NETSTANDARD1_3
            AppDomain.CurrentDomain.DomainUnload += OnCurrentDomainUnload;
            AppDomain.CurrentDomain.ProcessExit  += OnCurrentDomainUnload;
#endif

            _shutdownRegistration = AspNetShutdownDetector.GetShutdownToken().Register(Dispose);
        }