Esempio n. 1
0
        void RestartService(object state)
        {
            try
            {
                _log.InfoFormat("Restarting service: {0}", _settings.ServiceName);

                var control = new AppDomainHostControl(this);
                _service.Stop(control);

                UnloadServiceAppDomain();

                _log.DebugFormat("Service AppDomain unloaded: {0}", _settings.ServiceName);

                _service = CreateServiceInAppDomain();

                _log.DebugFormat("Service created in new AppDomain: {0}", _settings.ServiceName);

                _service.Start(control);

                _log.InfoFormat("The service has been restarted: {0}", _settings.ServiceName);
            }
            catch (Exception ex)
            {
                _log.Error("Failed to restart service", ex);
                _hostControl.Stop();
            }
        }
Esempio n. 2
0
        private async Task BlockUntilWeAreTheLeader()
        {
            var token = config.ServiceIsStopping.Token;

            while (!await config.LeaseManager.AcquireLease(new LeaseOptions(config.NodeId), token))
            {
                logger.DebugFormat("NodeId {0} failed to aquire a lease. Waiting {1}", config.NodeId, config.LeaseConfiguration.LeaseCriteria.AquireLeaseEvery);
                await Task.Delay(config.LeaseConfiguration.LeaseCriteria.AquireLeaseEvery, token);
            }
            logger.DebugFormat("NodeId {0} has been elected as leader", config.NodeId);
            config.WhenLeaderIsElected(true);
        }
Esempio n. 3
0
        public TopshelfExitCode Run()
        {
            if (!_environment.IsServiceInstalled(_settings.ServiceName))
            {
                _log.ErrorFormat("The {0} service is not installed.", _settings.ServiceName);
                return(TopshelfExitCode.ServiceNotInstalled);
            }

            if (_environment.IsServiceStopped(_settings.ServiceName))
            {
                _log.ErrorFormat("The {0} service is not running.", _settings.ServiceName);
                return(TopshelfExitCode.ServiceNotRunning);
            }

            _log.DebugFormat("Sending command {0} to {1}", _command, _settings.ServiceName);

            try
            {
                _environment.SendServiceCommand(_settings.ServiceName, _command);

                _log.InfoFormat("The command {0} was sent to the {1} service.", _command, _settings.ServiceName);
                return(TopshelfExitCode.Ok);
            }
            catch (Exception ex)
            {
                _log.Error("The command could not be sent to the service.", ex);
                return(TopshelfExitCode.ServiceControlRequestFailed);
            }
        }
Esempio n. 4
0
        public TopshelfExitCode Run()
        {
            if (!_environment.IsServiceInstalled(_settings.ServiceName))
            {
                _log.ErrorFormat("The {0} service is not installed.", _settings.ServiceName);
                return(TopshelfExitCode.ServiceNotInstalled);
            }

            if (!_environment.IsAdministrator)
            {
                if (_sudo)
                {
                    if (_environment.RunAsAdministrator())
                    {
                        return(TopshelfExitCode.Ok);
                    }
                }

                _log.ErrorFormat("The {0} service can only be uninstalled as an administrator", _settings.ServiceName);
                return(TopshelfExitCode.SudoRequired);
            }

            _log.DebugFormat("Uninstalling {0}", _settings.ServiceName);

            _environment.UninstallService(_settings, ExecutePreActions, ExecutePostActions);

            return(TopshelfExitCode.Ok);
        }
Esempio n. 5
0
        public TopshelfExitCode Run()
        {
            if (!_environment.IsServiceInstalled(_settings.ServiceName))
            {
                string message = string.Format("The {0} service is not installed.", _settings.ServiceName);
                _log.Error(message);

                return(TopshelfExitCode.ServiceNotInstalled);
            }

            if (!_environment.IsAdministrator)
            {
                if (!_environment.RunAsAdministrator())
                {
                    _log.ErrorFormat("The {0} service can only be stopped by an administrator", _settings.ServiceName);
                }

                return(TopshelfExitCode.SudoRequired);
            }

            _log.DebugFormat("Stopping {0}", _settings.ServiceName);

            try
            {
                _environment.StopService(_settings.ServiceName);

                _log.InfoFormat("The {0} service was stopped.", _settings.ServiceName);
                return(TopshelfExitCode.Ok);
            }
            catch (Exception ex)
            {
                _log.Error("The service failed to stop.", ex);
                return(TopshelfExitCode.StopServiceFailed);
            }
        }
Esempio n. 6
0
        public void Run()
        {
            if (!_environment.IsServiceInstalled(_settings.ServiceName))
            {
                string message = string.Format("The {0} service is not installed.", _settings.ServiceName);
                _log.Error(message);

                return;
            }

            if (!_environment.IsAdministrator)
            {
                if (!_environment.RunAsAdministrator())
                {
                    _log.ErrorFormat("The {0} service can only be stopped by an administrator", _settings.ServiceName);
                }
                return;
            }

            _log.DebugFormat("Stopping {0}", _settings.ServiceName);

            _environment.StopService(_settings.ServiceName);

            _log.InfoFormat("The {0} service was stopped.", _settings.ServiceName);
        }
Esempio n. 7
0
        public void Monitor(object state)
        {
            ManualResetEvent stopHandle  = (ManualResetEvent)state;
            String           machineName = Environment.MachineName;

            try
            {
                Initialize(machineName);
                var snapshots = new ServiceCounterSnapshot[serviceCounters.Count];

                while (!PerfmonWorker.ShouldStop)
                {
                    Thread.Sleep(SleepIntervalInMilliSecs);

                    // this would be our timestamp value by which we will group the snapshots
                    DateTime timeStamp = DateTime.UtcNow;
                    // collect snapshots
                    for (int i = 0; i < serviceCounters.Count; i++)
                    {
                        var snapshot = new ServiceCounterSnapshot();
                        snapshot.CreationTimeUtc     = timeStamp;
                        snapshot.SnapshotMachineName = machineName;
                        snapshot.MachineIP           = ServiceMonitor.MachineIP;
                        snapshot.ServiceCounter      = serviceCounterList.First(x => x.Id == serviceCounters[i].Item1);
                        try
                        {
                            snapshot.ServiceCounter.MachineIP = ServiceMonitor.MachineIP;
                            snapshot.ServiceCounterValue      = serviceCounters[i].Item2.NextValue();
                            logger.DebugFormat("Performance counter {0} read value: {1}", GetPerfCounterPath(serviceCounters[i].Item2),
                                               snapshot.ServiceCounterValue);
                        }
                        catch (InvalidOperationException)
                        {
                            snapshot.ServiceCounterValue = null;
                            logger.DebugFormat("Performance counter {0} didn't send any value.", GetPerfCounterPath(serviceCounters[i].Item2));
                        }
                        snapshots[i] = snapshot;
                    }
                    SaveServiceSnapshots(snapshots);
                }
            }
            finally
            {
                stopHandle.Set();
            }
        }
Esempio n. 8
0
        public override void Install(IDictionary stateSaver)
        {
            Installers.AddRange(_installers);

            if (_log.IsInfoEnabled)
            {
                _log.InfoFormat("Installing {0} service", _settings.DisplayName);
            }

            base.Install(stateSaver);

            if (_log.IsDebugEnabled)
            {
                _log.Debug("Opening Registry");
            }

            using (RegistryKey system = Registry.LocalMachine.OpenSubKey("System"))
                using (RegistryKey currentControlSet = system.OpenSubKey("CurrentControlSet"))
                    using (RegistryKey services = currentControlSet.OpenSubKey("Services"))
                        using (RegistryKey service = services.OpenSubKey(_settings.ServiceName, true))
                        {
                            service.SetValue("Description", _settings.Description);

                            var imagePath = (string)service.GetValue("ImagePath");

                            _log.DebugFormat("Service path: {0}", imagePath);

                            imagePath += _arguments;

                            _log.DebugFormat("Image path: {0}", imagePath);

                            service.SetValue("ImagePath", imagePath);
                        }

            if (_log.IsDebugEnabled)
            {
                _log.Debug("Closing Registry");
            }
        }
Esempio n. 9
0
        public TopshelfExitCode Run()
        {
#if NETCORE
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return(TopshelfExitCode.NotRunningOnWindows);
            }
#endif

            if (!_environment.IsServiceInstalled(_settings.ServiceName))
            {
                string message = $"The {_settings.ServiceName} service is not installed.";
                _log.Error(message);

                return(TopshelfExitCode.ServiceNotInstalled);
            }

            if (!_environment.IsAdministrator)
            {
                if (!_environment.RunAsAdministrator())
                {
                    _log.ErrorFormat("The {0} service can only be stopped by an administrator", _settings.ServiceName);
                }

                return(TopshelfExitCode.SudoRequired);
            }

            _log.DebugFormat("Stopping {0}", _settings.ServiceName);

            try
            {
                _environment.StopService(_settings.ServiceName, _settings.StopTimeOut);

                _log.InfoFormat("The {0} service was stopped.", _settings.ServiceName);
                return(TopshelfExitCode.Ok);
            }
            catch (Exception ex)
            {
                _log.Error("The service failed to stop.", ex);
                return(TopshelfExitCode.StopServiceFailed);
            }
        }
Esempio n. 10
0
        public TopshelfExitCode Run()
        {
            if (!_environment.IsAdministrator)
            {
                if (!_environment.RunAsAdministrator())
                {
                    _log.ErrorFormat("The {0} service can only be started by an administrator", _settings.ServiceName);
                }

                return(TopshelfExitCode.SudoRequired);
            }

            if (_parentHost != null)
            {
                _parentHost.Run();
            }

            if (!_environment.IsServiceInstalled(_settings.ServiceName))
            {
                _log.ErrorFormat("The {0} service is not installed.", _settings.ServiceName);
                return(TopshelfExitCode.ServiceNotInstalled);
            }

            _log.DebugFormat("Starting {0}", _settings.ServiceName);

            try
            {
                _environment.StartService(_settings.ServiceName, _settings.StartTimeOut);

                _log.InfoFormat("The {0} service was started.", _settings.ServiceName);
                return(TopshelfExitCode.Ok);
            }
            catch (Exception ex)
            {
                _log.Error("The service failed to start.", ex);
                return(TopshelfExitCode.StartServiceFailed);
            }
        }
Esempio n. 11
0
        public void Run()
        {
            if (!_environment.IsAdministrator)
            {
                if (!_environment.RunAsAdministrator())
                {
                    _log.ErrorFormat("The {0} service can only be started by an administrator", _settings.ServiceName);
                }

                return;
            }

            if (_parentHost != null)
            {
                _parentHost.Run();
            }

            if (!_environment.IsServiceInstalled(_settings.ServiceName))
            {
                _log.ErrorFormat("The {0} service is not installed.", _settings.ServiceName);
                return;
            }

            _log.DebugFormat("Starting {0}", _settings.ServiceName);

            try
            {
                _environment.StartService(_settings.ServiceName);

                _log.InfoFormat("The {0} service was started.", _settings.ServiceName);
            }
            catch (Exception ex)
            {
                _log.Error("The service failed to start.", ex);
            }
        }
Esempio n. 12
0
        private Installer CreateHostInstaller(HostSettings settings, Installer[] installers)
        {
            string arguments = " ";

            if (!string.IsNullOrEmpty(settings.InstanceName))
            {
                arguments += $" -instance \"{settings.InstanceName}\"";
            }

            if (!string.IsNullOrEmpty(settings.DisplayName))
            {
                arguments += $" -displayname \"{settings.DisplayName}\"";
            }

            if (!string.IsNullOrEmpty(settings.Name))
            {
                arguments += $" -servicename \"{settings.Name}\"";
            }

            var pairs = StartParametersExtensions.Commands(_hostConfigurator);

            if (pairs != null)
            {
                foreach (var pair in pairs)
                {
                    _log.DebugFormat("Start parameter '{0}' with value '{1}' added to {2}", pair.Item1, pair.Item2,
                                     string.IsNullOrEmpty(settings.InstanceName)
                            ? settings.ServiceName
                            : settings.ServiceName + "$" + settings.InstanceName);

                    arguments += $" -{pair.Item1} \"{pair.Item2}\"";
                }
            }

            return(new HostInstaller(settings, arguments, installers));
        }
Esempio n. 13
0
        void HostControl.Stop()
        {
            _logWriter.DebugFormat(Resources.ServiceStopRequested, _settings.ServiceName);

            SetStopSignal();
        }
Esempio n. 14
0
        void ServiceAvailabilityHost.StopService(string reason)
        {
            _log.DebugFormat("Service stop requested: {0}", reason);

            _fiber.Add(StopService);
        }
Esempio n. 15
0
        void HostControl.RequestAdditionalTime(TimeSpan timeRemaining)
        {
            _log.DebugFormat("Requesting additional time: {0}", timeRemaining);

            RequestAdditionalTime((int)timeRemaining.TotalMilliseconds);
        }