1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Common/ServiceProvider.cs

176 lines
6.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Specialized;
using System.Configuration.Install;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using NLog;
namespace NzbDrone.Common
{
2013-05-11 02:53:50 +03:00
public interface IServiceProvider
{
bool ServiceExist(string name);
bool IsServiceRunning(string name);
void Install(string serviceName);
void UnInstall(string serviceName);
void Run(ServiceBase service);
2013-08-14 06:48:50 +03:00
ServiceController GetService(string serviceName);
2013-05-11 02:53:50 +03:00
void Stop(string serviceName);
void Start(string serviceName);
2013-08-14 07:02:56 +03:00
ServiceControllerStatus GetStatus(string serviceName);
2013-05-11 02:53:50 +03:00
}
public class ServiceProvider : IServiceProvider
{
public const string NZBDRONE_SERVICE_NAME = "NzbDrone";
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2013-08-14 06:48:50 +03:00
public virtual bool ServiceExist(string name)
{
Logger.Debug("Checking if service {0} exists.", name);
return
ServiceController.GetServices().Any(
s => String.Equals(s.ServiceName, name, StringComparison.InvariantCultureIgnoreCase));
}
2013-08-14 06:48:50 +03:00
public virtual bool IsServiceRunning(string name)
2011-11-13 07:07:06 +03:00
{
Logger.Debug("Checking if '{0}' service is running", name);
var service = ServiceController.GetServices()
.SingleOrDefault(s => String.Equals(s.ServiceName, name, StringComparison.InvariantCultureIgnoreCase));
return service != null && (
service.Status != ServiceControllerStatus.Stopped ||
service.Status == ServiceControllerStatus.StopPending ||
service.Status == ServiceControllerStatus.Paused ||
service.Status == ServiceControllerStatus.PausePending);
2011-11-13 07:07:06 +03:00
}
2013-08-14 06:48:50 +03:00
public virtual void Install(string serviceName)
{
Logger.Info("Installing service '{0}'", serviceName);
var installer = new ServiceProcessInstaller
{
Account = ServiceAccount.LocalSystem
};
var serviceInstaller = new ServiceInstaller();
String[] cmdline = { @"/assemblypath=" + Process.GetCurrentProcess().MainModule.FileName };
var context = new InstallContext("service_install.log", cmdline);
serviceInstaller.Context = context;
serviceInstaller.DisplayName = serviceName;
serviceInstaller.ServiceName = serviceName;
serviceInstaller.Description = "NzbDrone Application Server";
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.Parent = installer;
serviceInstaller.Install(new ListDictionary());
Logger.Info("Service Has installed successfully.");
}
2013-08-14 06:48:50 +03:00
public virtual void UnInstall(string serviceName)
{
Logger.Info("Uninstalling {0} service", serviceName);
Stop(serviceName);
2013-04-16 07:52:41 +03:00
var serviceInstaller = new ServiceInstaller();
var context = new InstallContext("service_uninstall.log", null);
serviceInstaller.Context = context;
serviceInstaller.ServiceName = serviceName;
serviceInstaller.Uninstall(null);
Logger.Info("{0} successfully uninstalled", serviceName);
}
2013-08-14 06:48:50 +03:00
public virtual void Run(ServiceBase service)
{
ServiceBase.Run(service);
}
2013-08-14 06:48:50 +03:00
public virtual ServiceController GetService(string serviceName)
{
2013-04-16 07:52:41 +03:00
return ServiceController.GetServices().FirstOrDefault(c => String.Equals(c.ServiceName, serviceName, StringComparison.InvariantCultureIgnoreCase));
}
2013-08-14 06:48:50 +03:00
public virtual void Stop(string serviceName)
{
Logger.Info("Stopping {0} Service...", serviceName);
var service = GetService(serviceName);
if (service == null)
{
Logger.Warn("Unable to stop {0}. no service with that name exists.", serviceName);
return;
}
Logger.Info("Service is currently {0}", service.Status);
if (service.Status != ServiceControllerStatus.Stopped)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60));
service.Refresh();
if (service.Status == ServiceControllerStatus.Stopped)
{
Logger.Info("{0} has stopped successfully.", serviceName);
}
else
{
Logger.Error("Service stop request has timed out. {0}", service.Status);
}
}
else
{
Logger.Warn("Service {0} is already in stopped state.", service.ServiceName);
}
}
2013-08-14 07:02:56 +03:00
public ServiceControllerStatus GetStatus(string serviceName)
{
return GetService(serviceName).Status;
}
public void Start(string serviceName)
{
2011-11-13 21:16:31 +03:00
Logger.Info("Starting {0} Service...", serviceName);
var service = GetService(serviceName);
if (service == null)
{
Logger.Warn("Unable to start '{0}' no service with that name exists.", serviceName);
2013-01-15 00:14:32 +03:00
return;
}
2011-11-13 21:16:31 +03:00
if (service.Status != ServiceControllerStatus.Paused && service.Status != ServiceControllerStatus.Stopped)
{
2011-11-13 21:16:31 +03:00
Logger.Warn("Service is in a state that can't be started. Current status: {0}", service.Status);
}
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(60));
service.Refresh();
if (service.Status == ServiceControllerStatus.Running)
{
2011-11-13 21:16:31 +03:00
Logger.Info("{0} has started successfully.", serviceName);
}
else
{
Logger.Error("Service start request has timed out. {0}", service.Status);
}
}
}
}