1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-25 09:21:57 +00:00

better service routing

This commit is contained in:
kay.one 2013-08-13 20:48:50 -07:00
parent 9689b07d5b
commit 3b4eacdd31
3 changed files with 20 additions and 14 deletions

View file

@ -2,6 +2,7 @@
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Security.Principal; using System.Security.Principal;
using System.ServiceProcess;
using NLog; using NLog;
namespace NzbDrone.Common.EnvironmentInfo namespace NzbDrone.Common.EnvironmentInfo

View file

@ -15,6 +15,7 @@ public interface IServiceProvider
void Install(string serviceName); void Install(string serviceName);
void UnInstall(string serviceName); void UnInstall(string serviceName);
void Run(ServiceBase service); void Run(ServiceBase service);
ServiceController GetService(string serviceName);
void Stop(string serviceName); void Stop(string serviceName);
void Start(string serviceName); void Start(string serviceName);
} }
@ -25,7 +26,7 @@ public class ServiceProvider : IServiceProvider
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public bool ServiceExist(string name) public virtual bool ServiceExist(string name)
{ {
Logger.Debug("Checking if service {0} exists.", name); Logger.Debug("Checking if service {0} exists.", name);
return return
@ -33,7 +34,7 @@ public bool ServiceExist(string name)
s => String.Equals(s.ServiceName, name, StringComparison.InvariantCultureIgnoreCase)); s => String.Equals(s.ServiceName, name, StringComparison.InvariantCultureIgnoreCase));
} }
public bool IsServiceRunning(string name) public virtual bool IsServiceRunning(string name)
{ {
Logger.Debug("Checking if '{0}' service is running", name); Logger.Debug("Checking if '{0}' service is running", name);
@ -47,7 +48,7 @@ public bool IsServiceRunning(string name)
service.Status == ServiceControllerStatus.PausePending); service.Status == ServiceControllerStatus.PausePending);
} }
public void Install(string serviceName) public virtual void Install(string serviceName)
{ {
Logger.Info("Installing service '{0}'", serviceName); Logger.Info("Installing service '{0}'", serviceName);
@ -76,7 +77,7 @@ public void Install(string serviceName)
Logger.Info("Service Has installed successfully."); Logger.Info("Service Has installed successfully.");
} }
public void UnInstall(string serviceName) public virtual void UnInstall(string serviceName)
{ {
Logger.Info("Uninstalling {0} service", serviceName); Logger.Info("Uninstalling {0} service", serviceName);
@ -92,17 +93,17 @@ public void UnInstall(string serviceName)
Logger.Info("{0} successfully uninstalled", serviceName); Logger.Info("{0} successfully uninstalled", serviceName);
} }
public void Run(ServiceBase service) public virtual void Run(ServiceBase service)
{ {
ServiceBase.Run(service); ServiceBase.Run(service);
} }
public ServiceController GetService(string serviceName) public virtual ServiceController GetService(string serviceName)
{ {
return ServiceController.GetServices().FirstOrDefault(c => String.Equals(c.ServiceName, serviceName, StringComparison.InvariantCultureIgnoreCase)); return ServiceController.GetServices().FirstOrDefault(c => String.Equals(c.ServiceName, serviceName, StringComparison.InvariantCultureIgnoreCase));
} }
public void Stop(string serviceName) public virtual void Stop(string serviceName)
{ {
Logger.Info("Stopping {0} Service...", serviceName); Logger.Info("Stopping {0} Service...", serviceName);
var service = GetService(serviceName); var service = GetService(serviceName);
@ -135,7 +136,7 @@ public void Stop(string serviceName)
} }
} }
public void Start(string serviceName) public virtual void Start(string serviceName)
{ {
Logger.Info("Starting {0} Service...", serviceName); Logger.Info("Starting {0} Service...", serviceName);
var service = GetService(serviceName); var service = GetService(serviceName);

View file

@ -1,4 +1,5 @@
using NLog; using System.ServiceProcess;
using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
@ -32,11 +33,6 @@ public void Route()
public void Route(ApplicationModes applicationModes) public void Route(ApplicationModes applicationModes)
{ {
if (!_runtimeInfo.IsUserInteractive && !OsInfo.IsLinux &&_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
applicationModes = ApplicationModes.Service;
}
_logger.Info("Application mode: {0}", applicationModes); _logger.Info("Application mode: {0}", applicationModes);
switch (applicationModes) switch (applicationModes)
@ -112,6 +108,14 @@ private ApplicationModes GetApplicationMode()
return ApplicationModes.UninstallService; return ApplicationModes.UninstallService;
} }
if (!_runtimeInfo.IsUserInteractive &&
OsInfo.IsWindows &&
_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME) &&
_serviceProvider.GetService(ServiceProvider.NZBDRONE_SERVICE_NAME).Status == ServiceControllerStatus.StartPending)
{
return ApplicationModes.Service;
}
return ApplicationModes.Interactive; return ApplicationModes.Interactive;
} }
} }