2011-10-23 08:26:43 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
using System.Configuration.Install;
|
2011-10-25 09:11:50 +03:00
|
|
|
using System.Diagnostics;
|
2011-10-23 08:26:43 +03:00
|
|
|
using System.Linq;
|
|
|
|
using System.ServiceProcess;
|
|
|
|
using NLog;
|
|
|
|
|
|
|
|
namespace NzbDrone.Common
|
|
|
|
{
|
|
|
|
public class ServiceProvider
|
|
|
|
{
|
2011-10-26 20:15:47 +03:00
|
|
|
public const string NZBDRONE_SERVICE_NAME = "NzbDrone";
|
2011-10-23 08:26:43 +03:00
|
|
|
|
2011-11-26 02:46:29 +03:00
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-10-23 08:26:43 +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));
|
|
|
|
}
|
|
|
|
|
2011-11-13 07:07:06 +03:00
|
|
|
public virtual bool IsServiceRunning(string name)
|
|
|
|
{
|
|
|
|
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.Running;
|
|
|
|
}
|
|
|
|
|
2011-10-26 20:15:47 +03:00
|
|
|
public virtual void Install(string serviceName)
|
2011-10-23 08:26:43 +03:00
|
|
|
{
|
2011-10-26 20:15:47 +03:00
|
|
|
Logger.Info("Installing service '{0}'", serviceName);
|
2011-10-23 08:26:43 +03:00
|
|
|
|
|
|
|
|
|
|
|
var installer = new ServiceProcessInstaller
|
|
|
|
{
|
|
|
|
Account = ServiceAccount.LocalSystem
|
|
|
|
};
|
|
|
|
|
|
|
|
var serviceInstaller = new ServiceInstaller();
|
|
|
|
|
|
|
|
|
2011-10-25 09:47:28 +03:00
|
|
|
String[] cmdline = { @"/assemblypath=" + Process.GetCurrentProcess().MainModule.FileName };
|
2011-10-23 08:26:43 +03:00
|
|
|
|
|
|
|
var context = new InstallContext("service_install.log", cmdline);
|
|
|
|
serviceInstaller.Context = context;
|
2011-10-26 20:15:47 +03:00
|
|
|
serviceInstaller.DisplayName = serviceName;
|
|
|
|
serviceInstaller.ServiceName = serviceName;
|
2011-10-23 08:26:43 +03:00
|
|
|
serviceInstaller.Description = "NzbDrone Application Server";
|
|
|
|
serviceInstaller.StartType = ServiceStartMode.Automatic;
|
|
|
|
|
|
|
|
serviceInstaller.Parent = installer;
|
|
|
|
|
|
|
|
serviceInstaller.Install(new ListDictionary());
|
|
|
|
|
|
|
|
Logger.Info("Service Has installed successfully.");
|
|
|
|
}
|
|
|
|
|
2011-10-26 20:15:47 +03:00
|
|
|
public virtual void UnInstall(string serviceName)
|
2011-10-23 08:26:43 +03:00
|
|
|
{
|
2011-10-26 20:15:47 +03:00
|
|
|
Logger.Info("Uninstalling {0} service", serviceName);
|
2012-01-17 10:12:22 +03:00
|
|
|
|
|
|
|
Stop(serviceName);
|
|
|
|
|
2011-10-23 08:26:43 +03:00
|
|
|
var serviceInstaller = new ServiceInstaller();
|
|
|
|
|
|
|
|
var context = new InstallContext("service_uninstall.log", null);
|
|
|
|
serviceInstaller.Context = context;
|
2011-10-26 20:15:47 +03:00
|
|
|
serviceInstaller.ServiceName = serviceName;
|
2011-10-23 08:26:43 +03:00
|
|
|
serviceInstaller.Uninstall(null);
|
|
|
|
|
2011-10-26 20:15:47 +03:00
|
|
|
Logger.Info("{0} successfully uninstalled", serviceName);
|
2011-10-23 08:26:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Run(ServiceBase service)
|
|
|
|
{
|
|
|
|
ServiceBase.Run(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual ServiceController GetService(string serviceName)
|
|
|
|
{
|
|
|
|
return ServiceController.GetServices().Where(
|
|
|
|
c => String.Equals(c.ServiceName, serviceName, StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
.FirstOrDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Stop(string serviceName)
|
|
|
|
{
|
2011-11-26 02:46:29 +03:00
|
|
|
Logger.Info("Stopping {0} Service...", serviceName);
|
2011-10-23 08:26:43 +03:00
|
|
|
var service = GetService(serviceName);
|
|
|
|
if (service == null)
|
|
|
|
{
|
|
|
|
Logger.Warn("Unable to stop {0}. no service with that name exists.", serviceName);
|
2011-10-25 09:11:50 +03:00
|
|
|
return;
|
2011-10-23 08:26:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Info("Service is currently {0}", service.Status);
|
|
|
|
|
2011-11-15 05:38:15 +03:00
|
|
|
if (service.Status != ServiceControllerStatus.Stopped)
|
2011-10-23 08:26:43 +03:00
|
|
|
{
|
2011-11-15 05:38:15 +03:00
|
|
|
service.Stop();
|
|
|
|
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60));
|
|
|
|
|
|
|
|
service.Refresh();
|
|
|
|
if (service.Status == ServiceControllerStatus.Stopped)
|
|
|
|
{
|
2011-11-26 02:46:29 +03:00
|
|
|
Logger.Info("{0} has stopped successfully.", serviceName);
|
2011-11-15 05:38:15 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Error("Service stop request has timed out. {0}", service.Status);
|
|
|
|
}
|
2011-10-23 08:26:43 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-15 05:38:15 +03:00
|
|
|
Logger.Warn("Service {0} is already in stopped state.", service.ServiceName);
|
2011-10-23 08:26:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Start(string serviceName)
|
|
|
|
{
|
2011-11-13 21:16:31 +03:00
|
|
|
Logger.Info("Starting {0} Service...", serviceName);
|
2011-10-23 08:26:43 +03:00
|
|
|
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-10-23 08:26:43 +03:00
|
|
|
}
|
|
|
|
|
2011-11-13 21:16:31 +03:00
|
|
|
if (service.Status != ServiceControllerStatus.Paused && service.Status != ServiceControllerStatus.Stopped)
|
2011-10-23 08:26:43 +03:00
|
|
|
{
|
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);
|
2011-10-23 08:26:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2011-10-23 08:26:43 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Error("Service start request has timed out. {0}", service.Status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|