1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-27 01:57:21 +00:00
Lidarr/NzbDrone/Router.cs
Mark McDowall 125703a2fa Hidden startup, shutodwn and restart
New: Run without console window by default
New: Added NzbDrone.Console to run with console window
New: Shutdown from UI
New: Restart from UI
2013-01-06 00:25:45 -08:00

127 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using NLog;
using NzbDrone.Common;
using NzbDrone.Model;
namespace NzbDrone
{
public class Router
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private readonly ApplicationServer _applicationServer;
private readonly ServiceProvider _serviceProvider;
private readonly ConsoleProvider _consoleProvider;
private readonly EnvironmentProvider _environmentProvider;
private readonly ProcessProvider _processProvider;
public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider,
ConsoleProvider consoleProvider, EnvironmentProvider environmentProvider,
ProcessProvider processProvider)
{
_applicationServer = applicationServer;
_serviceProvider = serviceProvider;
_consoleProvider = consoleProvider;
_environmentProvider = environmentProvider;
_processProvider = processProvider;
}
public void Route(IEnumerable<string> args)
{
Route(GetApplicationMode(args));
}
public void Route(ApplicationMode applicationMode)
{
logger.Info("Application mode: {0}", applicationMode);
if(!_environmentProvider.IsUserInteractive)
{
applicationMode = ApplicationMode.Service;
}
switch (applicationMode)
{
case ApplicationMode.Service:
{
_serviceProvider.Run(_applicationServer);
break;
}
case ApplicationMode.Console:
{
_applicationServer.Start();
_consoleProvider.WaitForClose();
break;
}
case ApplicationMode.InstallService:
{
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
_consoleProvider.PrintServiceAlreadyExist();
}
else
{
_serviceProvider.Install(ServiceProvider.NZBDRONE_SERVICE_NAME);
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
break;
}
case ApplicationMode.UninstallService:
{
if (!_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
_consoleProvider.PrintServiceDoestExist();
}
else
{
_serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
break;
}
case ApplicationMode.Silent:
{
var startInfo = new ProcessStartInfo();
startInfo.FileName = _environmentProvider.GetNzbDroneExe();
startInfo.WorkingDirectory = _environmentProvider.ApplicationPath;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
_processProvider.Start(startInfo);
Environment.Exit(0);
break;
}
default:
{
_consoleProvider.PrintHelp();
break;
}
}
}
public static ApplicationMode GetApplicationMode(IEnumerable<string> args)
{
if (args == null) return ApplicationMode.Console;
var cleanArgs = args.Where(c => c != null && !String.IsNullOrWhiteSpace(c)).ToList();
if (cleanArgs.Count == 0) return ApplicationMode.Console;
if (cleanArgs.Count != 1) return ApplicationMode.Help;
var arg = cleanArgs.First().Trim('/', '\\', '-').ToLower();
if (arg == "i") return ApplicationMode.InstallService;
if (arg == "u") return ApplicationMode.UninstallService;
if (arg == "s") return ApplicationMode.Silent;
return ApplicationMode.Help;
}
}
}