Exemplo n.º 1
0
        private void StartMonitoringService()
        {
            const string monitoringServiceName = Constants.LocalWorkerMonitoringServiceName;

            var serviceState = ServicesHelper.GetServiceState(monitoringServiceName);

            if (serviceState.Equals(ServiceState.Running))
            {
                this.Logger.Information("{Service} is running.", monitoringServiceName);
                return;
            }

            this.Logger.Information("Attempting to start the {Service}...", monitoringServiceName);

            if (serviceState.Equals(ServiceState.NotFound))
            {
                ServicesHelper.InstallService(monitoringServiceName, Settings.MonitoringServiceExecutablePath);
            }

            ServicesHelper.StartService(monitoringServiceName);

            this.Logger.Information("{Service} started successfully.", monitoringServiceName);
        }
Exemplo n.º 2
0
        private void OnTimerElapsed(object sender, ElapsedEventArgs args)
        {
            const string localWorkerName = Constants.LocalWorkerServiceName;

            try
            {
                var localWorkerState = ServicesHelper.GetServiceState(localWorkerName);
                switch (localWorkerState)
                {
                case ServiceState.NotFound:
                    logger.Warn($"{localWorkerName} is not found.");
                    InstallAndStartLocalWorker();
                    break;

                case ServiceState.Running:
                    this.totalChecksCount  = 0;
                    this.failsToStartCount = 0;
                    this.numberOfTotalChecksBeforeSendingEmail  = NumberOfTotalChecksBeforeSendingEmail;
                    this.numberOfFailedStartsBeforeSendingEmail = NumberOfFailedStartsBeforeSendingEmail;
                    break;

                case ServiceState.StartPending:
                    logger.Info($"{localWorkerName} is starting...");
                    break;

                case ServiceState.ContinuePending:
                    logger.Warn($"{localWorkerName} is resuming...");
                    break;

                case ServiceState.StopPending:
                    logger.Warn($"{localWorkerName} is stopping...");
                    StartLocalWorker();
                    break;

                case ServiceState.Stopped:
                    logger.Warn($"{localWorkerName} is stopped.");
                    StartLocalWorker();
                    break;

                case ServiceState.PausePending:
                    logger.Warn($"{localWorkerName} is pausing...");
                    StartLocalWorker();
                    break;

                case ServiceState.Paused:
                    logger.Warn($"{localWorkerName} is paused.");
                    StartLocalWorker();
                    break;

                case ServiceState.Unknown:
                    throw new ArgumentException($"{localWorkerName} is in unknown state.");

                default:
                    throw new ArgumentOutOfRangeException(nameof(localWorkerState));
                }
            }
            catch (Exception ex)
            {
                this.failsToStartCount++;
                logger.Error($"An exception was thrown while attempting to start the {localWorkerName}", ex);
            }

            this.totalChecksCount++;

            if (this.failsToStartCount <= this.numberOfFailedStartsBeforeSendingEmail &&
                this.totalChecksCount <= this.numberOfTotalChecksBeforeSendingEmail)
            {
                return;
            }

            var messageTitle = $"{localWorkerName} cannot keep Running state";
            var messageBody  =
                $"{localWorkerName} has started and stopped consecutively more than {this.totalChecksCount} times.";

            if (this.failsToStartCount > this.numberOfFailedStartsBeforeSendingEmail)
            {
                messageTitle = $"{localWorkerName} failed to start";
                messageBody  = $"{localWorkerName} failed to start more than {this.failsToStartCount} times.";
            }

            try
            {
                this.emailSender.SendEmail(
                    this.devEmail,
                    messageTitle,
                    messageBody);

                this.numberOfFailedStartsBeforeSendingEmail *= 2;
                this.numberOfTotalChecksBeforeSendingEmail  *= 2;
            }
            catch (Exception ex)
            {
                logger.Error($"An exception was thrown while sending email to {this.devEmail}", ex);
            }
        }