You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sonarr/NzbDrone.Host/Router.cs

118 lines
4.3 KiB

using System.ServiceProcess;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo;
namespace NzbDrone.Host
{
public class Router
{
12 years ago
private readonly INzbDroneServiceFactory _nzbDroneServiceFactory;
private readonly IServiceProvider _serviceProvider;
private readonly IStartupArguments _startupArguments;
12 years ago
private readonly IConsoleService _consoleService;
private readonly IRuntimeInfo _runtimeInfo;
12 years ago
private readonly Logger _logger;
public Router(INzbDroneServiceFactory nzbDroneServiceFactory, IServiceProvider serviceProvider, IStartupArguments startupArguments,
IConsoleService consoleService, IRuntimeInfo runtimeInfo, Logger logger)
{
12 years ago
_nzbDroneServiceFactory = nzbDroneServiceFactory;
_serviceProvider = serviceProvider;
_startupArguments = startupArguments;
12 years ago
_consoleService = consoleService;
_runtimeInfo = runtimeInfo;
12 years ago
_logger = logger;
}
public void Route()
{
var appMode = GetApplicationMode();
Route(appMode);
}
12 years ago
public void Route(ApplicationModes applicationModes)
{
12 years ago
_logger.Info("Application mode: {0}", applicationModes);
12 years ago
switch (applicationModes)
{
12 years ago
case ApplicationModes.Service:
{
12 years ago
_logger.Trace("Service selected");
12 years ago
_serviceProvider.Run(_nzbDroneServiceFactory.Build());
break;
}
case ApplicationModes.Interactive:
{
12 years ago
_logger.Trace("Console selected");
12 years ago
_nzbDroneServiceFactory.Start();
break;
}
12 years ago
case ApplicationModes.InstallService:
{
12 years ago
_logger.Trace("Install Service selected");
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
12 years ago
_consoleService.PrintServiceAlreadyExist();
}
else
{
_serviceProvider.Install(ServiceProvider.NZBDRONE_SERVICE_NAME);
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
break;
}
12 years ago
case ApplicationModes.UninstallService:
{
12 years ago
_logger.Trace("Uninstall Service selected");
if (!_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
_consoleService.PrintServiceDoesNotExist();
}
else
{
_serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
break;
}
default:
{
12 years ago
_consoleService.PrintHelp();
break;
}
}
}
private ApplicationModes GetApplicationMode()
{
12 years ago
if (!_runtimeInfo.IsUserInteractive &&
OsInfo.IsWindows &&
_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME) &&
_serviceProvider.GetStatus(ServiceProvider.NZBDRONE_SERVICE_NAME) == ServiceControllerStatus.StartPending)
{
return ApplicationModes.Service;
}
if (_startupArguments.Flags.Contains(StartupArguments.HELP))
{
return ApplicationModes.Help;
}
if (!OsInfo.IsLinux && _startupArguments.Flags.Contains(StartupArguments.INSTALL_SERVICE))
{
return ApplicationModes.InstallService;
}
if (!OsInfo.IsLinux && _startupArguments.Flags.Contains(StartupArguments.UNINSTALL_SERVICE))
{
return ApplicationModes.UninstallService;
}
return ApplicationModes.Interactive;
}
}
}