From 1e6817220ad7a31d7cf1d67c0967ed868e38dccc Mon Sep 17 00:00:00 2001 From: kayone Date: Mon, 25 Nov 2013 22:53:36 -0800 Subject: [PATCH] applicationmode cleanup. --- src/NzbDrone.App.Test/ContainerFixture.cs | 2 +- src/NzbDrone.App.Test/RouterTest.cs | 2 +- .../AutomationTest.cs | 2 +- .../EnvironmentProviderTest.cs | 4 +- .../StartupArgumentsFixture.cs | 6 +- .../ServiceFactoryFixture.cs | 2 +- .../Composition/ContainerBuilderBase.cs | 2 +- .../EnvironmentInfo/AppFolderInfo.cs | 6 +- .../EnvironmentInfo/RuntimeInfo.cs | 13 +- ...{StartupArguments.cs => StartupContext.cs} | 7 +- .../Instrumentation/LogTargets.cs | 6 +- src/NzbDrone.Common/NzbDrone.Common.csproj | 2 +- src/NzbDrone.Console/ConsoleApp.cs | 23 +--- .../MediaCoverServiceFixture.cs | 2 +- src/NzbDrone.Host/ApplicationServer.cs | 15 +- src/NzbDrone.Host/Bootstrap.cs | 130 +++++++++++++++--- src/NzbDrone.Host/BrowserService.cs | 18 ++- src/NzbDrone.Host/MainAppContainerBuilder.cs | 4 +- src/NzbDrone.Host/Router.cs | 44 +----- .../IntegrationTest.cs | 2 +- src/NzbDrone.Test.Common/LoggingTest.cs | 2 +- src/NzbDrone.Test.Common/TestBase.cs | 2 +- .../StartNzbDroneService.cs | 2 +- src/NzbDrone.Update/UpdateApp.cs | 2 +- src/NzbDrone.Update/UpdateContainerBuilder.cs | 8 +- .../UpdateEngine/StartNzbDrone.cs | 2 +- src/NzbDrone/SysTray/SysTrayApp.cs | 1 - src/NzbDrone/WindowsApp.cs | 20 +-- 28 files changed, 187 insertions(+), 144 deletions(-) rename src/NzbDrone.Common/EnvironmentInfo/{StartupArguments.cs => StartupContext.cs} (91%) diff --git a/src/NzbDrone.App.Test/ContainerFixture.cs b/src/NzbDrone.App.Test/ContainerFixture.cs index a165f9348..d542b9dee 100644 --- a/src/NzbDrone.App.Test/ContainerFixture.cs +++ b/src/NzbDrone.App.Test/ContainerFixture.cs @@ -18,7 +18,7 @@ namespace NzbDrone.App.Test [TestFixture] public class ContainerFixture : TestBase { - StartupArguments args = new StartupArguments("first", "second"); + StartupContext args = new StartupContext("first", "second"); [Test] public void should_be_able_to_resolve_indexers() diff --git a/src/NzbDrone.App.Test/RouterTest.cs b/src/NzbDrone.App.Test/RouterTest.cs index 569af6a9f..0cf7b6c3d 100644 --- a/src/NzbDrone.App.Test/RouterTest.cs +++ b/src/NzbDrone.App.Test/RouterTest.cs @@ -68,7 +68,7 @@ public void Route_should_call_service_start_when_run_in_service_mode() serviceProvider.Setup(c => c.ServiceExist(It.IsAny())).Returns(true); serviceProvider.Setup(c => c.GetStatus(It.IsAny())).Returns(ServiceControllerStatus.StartPending); - Subject.Route(); + Subject.Route(ApplicationModes.Service); serviceProvider.Verify(c => c.Run(It.IsAny()), Times.Once()); } diff --git a/src/NzbDrone.Automation.Test/AutomationTest.cs b/src/NzbDrone.Automation.Test/AutomationTest.cs index a7f3645cf..ed0b7dfa2 100644 --- a/src/NzbDrone.Automation.Test/AutomationTest.cs +++ b/src/NzbDrone.Automation.Test/AutomationTest.cs @@ -23,7 +23,7 @@ public abstract class AutomationTest public AutomationTest() { - new StartupArguments(); + new StartupContext(); LogManager.Configuration = new LoggingConfiguration(); var consoleTarget = new ConsoleTarget { Layout = "${level}: ${message} ${exception}" }; diff --git a/src/NzbDrone.Common.Test/EnvironmentProviderTest.cs b/src/NzbDrone.Common.Test/EnvironmentProviderTest.cs index 588fb06ad..920064f70 100644 --- a/src/NzbDrone.Common.Test/EnvironmentProviderTest.cs +++ b/src/NzbDrone.Common.Test/EnvironmentProviderTest.cs @@ -35,9 +35,9 @@ public void IsProduction_should_return_false_when_run_within_nunit() [Test] public void should_use_path_from_arg_if_provided() { - var args = new StartupArguments("-data=\"c:\\users\\test\\\""); + var args = new StartupContext("-data=\"c:\\users\\test\\\""); - Mocker.SetConstant(args); + Mocker.SetConstant(args); Subject.AppDataFolder.Should().Be("c:\\users\\test\\"); } } diff --git a/src/NzbDrone.Common.Test/EnvironmentTests/StartupArgumentsFixture.cs b/src/NzbDrone.Common.Test/EnvironmentTests/StartupArgumentsFixture.cs index d6bad1e86..c3b94154b 100644 --- a/src/NzbDrone.Common.Test/EnvironmentTests/StartupArgumentsFixture.cs +++ b/src/NzbDrone.Common.Test/EnvironmentTests/StartupArgumentsFixture.cs @@ -11,7 +11,7 @@ public class StartupArgumentsFixture : TestBase [Test] public void empty_array_should_return_empty_flags() { - var args = new StartupArguments(new string[0]); + var args = new StartupContext(new string[0]); args.Flags.Should().BeEmpty(); } @@ -21,7 +21,7 @@ public void empty_array_should_return_empty_flags() [TestCase(" /t ")] public void should_parse_single_flag(string arg) { - var args = new StartupArguments(new[] { arg }); + var args = new StartupContext(new[] { arg }); args.Flags.Should().HaveCount(1); args.Flags.Contains("t").Should().BeTrue(); } @@ -32,7 +32,7 @@ public void should_parse_single_flag(string arg) [TestCase(" /key=\"value\"")] public void should_parse_args_with_alues(string arg) { - var args = new StartupArguments(new[] { arg }); + var args = new StartupContext(new[] { arg }); args.Args.Should().HaveCount(1); args.Args["key"].Should().Be("value"); } diff --git a/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs b/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs index 78dcbe442..157d0893d 100644 --- a/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs +++ b/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs @@ -15,7 +15,7 @@ public class ServiceFactoryFixture : TestBase [SetUp] public void setup() { - Mocker.SetConstant(MainAppContainerBuilder.BuildContainer(new StartupArguments())); + Mocker.SetConstant(MainAppContainerBuilder.BuildContainer(new StartupContext())); } [Test] diff --git a/src/NzbDrone.Common/Composition/ContainerBuilderBase.cs b/src/NzbDrone.Common/Composition/ContainerBuilderBase.cs index 9ac2747f1..7a9154672 100644 --- a/src/NzbDrone.Common/Composition/ContainerBuilderBase.cs +++ b/src/NzbDrone.Common/Composition/ContainerBuilderBase.cs @@ -15,7 +15,7 @@ public abstract class ContainerBuilderBase public IContainer Container { get; private set; } - protected ContainerBuilderBase(IStartupArguments args, params string[] assemblies) + protected ContainerBuilderBase(IStartupContext args, params string[] assemblies) { diff --git a/src/NzbDrone.Common/EnvironmentInfo/AppFolderInfo.cs b/src/NzbDrone.Common/EnvironmentInfo/AppFolderInfo.cs index 04f7b29ab..a7273a2af 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/AppFolderInfo.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/AppFolderInfo.cs @@ -22,7 +22,7 @@ public class AppFolderInfo : IAppFolderInfo private readonly Environment.SpecialFolder DATA_SPECIAL_FOLDER = Environment.SpecialFolder.CommonApplicationData; - public AppFolderInfo(IDiskProvider diskProvider, IStartupArguments startupArguments) + public AppFolderInfo(IDiskProvider diskProvider, IStartupContext startupContext) { _diskProvider = diskProvider; @@ -33,9 +33,9 @@ public AppFolderInfo(IDiskProvider diskProvider, IStartupArguments startupArgume _logger = NzbDroneLogger.GetLogger(this); - if (startupArguments.Args.ContainsKey(StartupArguments.APPDATA)) + if (startupContext.Args.ContainsKey(StartupContext.APPDATA)) { - AppDataFolder = startupArguments.Args[StartupArguments.APPDATA]; + AppDataFolder = startupContext.Args[StartupContext.APPDATA]; } else { diff --git a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs index 858db57b0..a4192a8a3 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.IO; using System.Security.Principal; +using System.ServiceProcess; using NLog; namespace NzbDrone.Common.EnvironmentInfo @@ -11,15 +12,21 @@ public interface IRuntimeInfo { bool IsUserInteractive { get; } bool IsAdmin { get; } + bool IsWindowsService { get; } } public class RuntimeInfo : IRuntimeInfo { private readonly Logger _logger; - public RuntimeInfo(Logger logger) + public RuntimeInfo(Logger logger, IServiceProvider serviceProvider) { _logger = logger; + + IsWindowsService = !IsUserInteractive && + OsInfo.IsWindows && + serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME) && + serviceProvider.GetStatus(ServiceProvider.NZBDRONE_SERVICE_NAME) == ServiceControllerStatus.StartPending; } public bool IsUserInteractive @@ -30,6 +37,8 @@ public bool IsUserInteractive static RuntimeInfo() { IsProduction = InternalIsProduction(); + + } public bool IsAdmin @@ -49,6 +58,8 @@ public bool IsAdmin } } + public bool IsWindowsService { get; private set; } + private static readonly string ProcessName = Process.GetCurrentProcess().ProcessName.ToLower(); public static bool IsProduction { get; private set; } diff --git a/src/NzbDrone.Common/EnvironmentInfo/StartupArguments.cs b/src/NzbDrone.Common/EnvironmentInfo/StartupContext.cs similarity index 91% rename from src/NzbDrone.Common/EnvironmentInfo/StartupArguments.cs rename to src/NzbDrone.Common/EnvironmentInfo/StartupContext.cs index 05b42d4eb..0702fc861 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/StartupArguments.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/StartupContext.cs @@ -2,7 +2,7 @@ namespace NzbDrone.Common.EnvironmentInfo { - public interface IStartupArguments + public interface IStartupContext { HashSet Flags { get; } Dictionary Args { get; } @@ -10,7 +10,7 @@ public interface IStartupArguments bool UninstallService { get; } } - public class StartupArguments : IStartupArguments + public class StartupContext : IStartupContext { public const string APPDATA = "data"; public const string NO_BROWSER = "nobrowser"; @@ -18,7 +18,7 @@ public class StartupArguments : IStartupArguments internal const string UNINSTALL_SERVICE = "u"; public const string HELP = "?"; - public StartupArguments(params string[] args) + public StartupContext(params string[] args) { Flags = new HashSet(); Args = new Dictionary(); @@ -58,5 +58,6 @@ public bool UninstallService return Flags.Contains(UNINSTALL_SERVICE); } } + } } \ No newline at end of file diff --git a/src/NzbDrone.Common/Instrumentation/LogTargets.cs b/src/NzbDrone.Common/Instrumentation/LogTargets.cs index d63520e80..4f028aa60 100644 --- a/src/NzbDrone.Common/Instrumentation/LogTargets.cs +++ b/src/NzbDrone.Common/Instrumentation/LogTargets.cs @@ -9,9 +9,9 @@ namespace NzbDrone.Common.Instrumentation { public static class LogTargets { - public static void Register(IStartupArguments startupArguments, bool updateApp, bool inConsole) + public static void Register(IStartupContext startupContext, bool updateApp, bool inConsole) { - var appFolderInfo = new AppFolderInfo(new DiskProvider(), startupArguments); + var appFolderInfo = new AppFolderInfo(new DiskProvider(), startupContext); LogManager.Configuration = new LoggingConfiguration(); @@ -24,7 +24,7 @@ public static void Register(IStartupArguments startupArguments, bool updateApp, } else { - if (inConsole && (OsInfo.IsLinux || new RuntimeInfo(null).IsUserInteractive)) + if (inConsole && (OsInfo.IsLinux || new RuntimeInfo(null, new ServiceProvider()).IsUserInteractive)) { RegisterConsole(); } diff --git a/src/NzbDrone.Common/NzbDrone.Common.csproj b/src/NzbDrone.Common/NzbDrone.Common.csproj index 6a6cc35f4..883ece0a3 100644 --- a/src/NzbDrone.Common/NzbDrone.Common.csproj +++ b/src/NzbDrone.Common/NzbDrone.Common.csproj @@ -86,7 +86,7 @@ - + diff --git a/src/NzbDrone.Console/ConsoleApp.cs b/src/NzbDrone.Console/ConsoleApp.cs index 27f5068fe..1cf0aae11 100644 --- a/src/NzbDrone.Console/ConsoleApp.cs +++ b/src/NzbDrone.Console/ConsoleApp.cs @@ -1,7 +1,5 @@ using System; -using System.Threading; using NLog; -using NzbDrone.Common; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Instrumentation; using NzbDrone.Host; @@ -16,26 +14,9 @@ public static void Main(string[] args) { try { - var startupArgs = new StartupArguments(args); + var startupArgs = new StartupContext(args); LogTargets.Register(startupArgs, false, true); - var container = Bootstrap.Start(startupArgs, new ConsoleAlerts()); - - if (startupArgs.InstallService || startupArgs.UninstallService) - { - return; - } - - var serviceFactory = container.Resolve(); - - while (!serviceFactory.IsServiceStopped) - { - Thread.Sleep(1000); - } - } - catch (TerminateApplicationException e) - { - Logger.Info("Application has been terminated. Reason " + e.Reason); - return; + Bootstrap.Start(startupArgs, new ConsoleAlerts()); } catch (Exception e) { diff --git a/src/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs b/src/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs index 3a4317c23..a157164b3 100644 --- a/src/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs +++ b/src/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs @@ -17,7 +17,7 @@ public class MediaCoverServiceFixture : CoreTest [SetUp] public void Setup() { - Mocker.SetConstant(new AppFolderInfo(new DiskProvider(), Mocker.Resolve())); + Mocker.SetConstant(new AppFolderInfo(new DiskProvider(), Mocker.Resolve())); } [Test] diff --git a/src/NzbDrone.Host/ApplicationServer.cs b/src/NzbDrone.Host/ApplicationServer.cs index 3eed9da4c..6ab53244f 100644 --- a/src/NzbDrone.Host/ApplicationServer.cs +++ b/src/NzbDrone.Host/ApplicationServer.cs @@ -8,7 +8,7 @@ namespace NzbDrone.Host { public interface INzbDroneServiceFactory { - bool IsServiceStopped { get; } + bool IsServiceStopped { get; } ServiceBase Build(); void Start(); } @@ -19,18 +19,18 @@ public class NzbDroneServiceFactory : ServiceBase, INzbDroneServiceFactory private readonly IRuntimeInfo _runtimeInfo; private readonly IHostController _hostController; private readonly PriorityMonitor _priorityMonitor; - private readonly IStartupArguments _startupArguments; + private readonly IStartupContext _startupContext; private readonly IBrowserService _browserService; private readonly Logger _logger; - public NzbDroneServiceFactory(IConfigFileProvider configFileProvider, IHostController hostController, - IRuntimeInfo runtimeInfo, PriorityMonitor priorityMonitor, IStartupArguments startupArguments, IBrowserService browserService, Logger logger) + public NzbDroneServiceFactory(IConfigFileProvider configFileProvider, IHostController hostController, + IRuntimeInfo runtimeInfo, PriorityMonitor priorityMonitor, IStartupContext startupContext, IBrowserService browserService, Logger logger) { _configFileProvider = configFileProvider; _hostController = hostController; _runtimeInfo = runtimeInfo; _priorityMonitor = priorityMonitor; - _startupArguments = startupArguments; + _startupContext = startupContext; _browserService = browserService; _logger = logger; } @@ -44,9 +44,8 @@ public void Start() { _hostController.StartServer(); - if (!_startupArguments.Flags.Contains(StartupArguments.NO_BROWSER) && - _runtimeInfo.IsUserInteractive && - _configFileProvider.LaunchBrowser) + if (!_startupContext.Flags.Contains(StartupContext.NO_BROWSER) + && _configFileProvider.LaunchBrowser) { _browserService.LaunchWebUI(); } diff --git a/src/NzbDrone.Host/Bootstrap.cs b/src/NzbDrone.Host/Bootstrap.cs index d5b315156..a11a848ec 100644 --- a/src/NzbDrone.Host/Bootstrap.cs +++ b/src/NzbDrone.Host/Bootstrap.cs @@ -1,4 +1,7 @@ -using System.Reflection; +using System; +using System.Reflection; +using System.Threading; +using NLog; using NzbDrone.Common.Composition; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Instrumentation; @@ -7,40 +10,123 @@ namespace NzbDrone.Host { - public class Bootstrap + public static class Bootstrap { - public IContainer Container { get; private set; } + private static IContainer _container; + private static readonly Logger Logger = NzbDroneLogger.GetLogger(); - public Bootstrap(StartupArguments args, IUserAlert userAlert) + + public static void Start(StartupContext startupContext, IUserAlert userAlert, Action startCallback = null) { - var logger = NzbDroneLogger.GetLogger(); - - GlobalExceptionHandlers.Register(); - IgnoreCertErrorPolicy.Register(); - - logger.Info("Starting NzbDrone Console. Version {0}", Assembly.GetExecutingAssembly().GetName().Version); - - - if (!PlatformValidation.IsValidate(userAlert)) + try { - throw new TerminateApplicationException("Missing system requirements"); + + GlobalExceptionHandlers.Register(); + IgnoreCertErrorPolicy.Register(); + + Logger.Info("Starting NzbDrone Console. Version {0}", Assembly.GetExecutingAssembly().GetName().Version); + + + if (!PlatformValidation.IsValidate(userAlert)) + { + throw new TerminateApplicationException("Missing system requirements"); + } + + _container = MainAppContainerBuilder.BuildContainer(startupContext); + + var appMode = GetApplicationMode(startupContext); + + Start(appMode); + + if (startCallback != null) + { + startCallback(_container); + } + + SpinToExit(appMode); + } + catch (TerminateApplicationException e) + { + Logger.Info("Application has been terminated. Reason " + e.Reason); + } + } + + + private static void Start(ApplicationModes applicationModes) + { + if (!IsInUtilityMode(applicationModes)) + { + EnsureSingleInstance(); } - Container = MainAppContainerBuilder.BuildContainer(args); - - + DbFactory.RegisterDatabase(_container); + _container.Resolve().Route(applicationModes); } - public void Start() + private static void SpinToExit(ApplicationModes applicationModes) { - DbFactory.RegisterDatabase(Container); - Container.Resolve().Route(); + if (IsInUtilityMode(applicationModes)) + { + return; + } + + var serviceFactory = _container.Resolve(); + + while (!serviceFactory.IsServiceStopped) + { + Thread.Sleep(1000); + } + } + + private static void EnsureSingleInstance() + { + _container.Resolve().EnforceSingleInstance(); } - public void EnsureSingleInstance() + + private static ApplicationModes GetApplicationMode(StartupContext startupContext) { - Container.Resolve().EnforceSingleInstance(); + if (startupContext.Flags.Contains(StartupContext.HELP)) + { + return ApplicationModes.Help; + } + + if (!OsInfo.IsLinux && startupContext.InstallService) + { + return ApplicationModes.InstallService; + } + + if (!OsInfo.IsLinux && startupContext.UninstallService) + { + return ApplicationModes.UninstallService; + } + + if (_container.Resolve().IsWindowsService) + { + return ApplicationModes.Service; + } + + return ApplicationModes.Interactive; + } + + + + private static bool IsInUtilityMode(ApplicationModes applicationMode) + { + switch (applicationMode) + { + case ApplicationModes.InstallService: + case ApplicationModes.UninstallService: + case ApplicationModes.Help: + { + return true; + } + default: + { + return false; + } + } } } diff --git a/src/NzbDrone.Host/BrowserService.cs b/src/NzbDrone.Host/BrowserService.cs index 7ae6e13bb..eebb361bc 100644 --- a/src/NzbDrone.Host/BrowserService.cs +++ b/src/NzbDrone.Host/BrowserService.cs @@ -1,5 +1,6 @@ using System; using NLog; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Processes; using NzbDrone.Core.Configuration; @@ -14,12 +15,14 @@ public class BrowserService : IBrowserService { private readonly IProcessProvider _processProvider; private readonly IConfigFileProvider _configFileProvider; + private readonly IRuntimeInfo _runtimeInfo; private readonly Logger _logger; - public BrowserService(IProcessProvider processProvider, IConfigFileProvider configFileProvider, Logger logger) + public BrowserService(IProcessProvider processProvider, IConfigFileProvider configFileProvider, IRuntimeInfo runtimeInfo, Logger logger) { _processProvider = processProvider; _configFileProvider = configFileProvider; + _runtimeInfo = runtimeInfo; _logger = logger; } @@ -28,12 +31,19 @@ public void LaunchWebUI() var url = string.Format("http://localhost:{0}", _configFileProvider.Port); try { - _logger.Info("Starting default browser. {0}", url); - _processProvider.OpenDefaultBrowser(url); + if (_runtimeInfo.IsUserInteractive) + { + _logger.Info("Starting default browser. {0}", url); + _processProvider.OpenDefaultBrowser(url); + } + else + { + _logger.Debug("none-interactive runtime. Won't attempt to open browser."); + } } catch (Exception e) { - _logger.ErrorException("Couldn't open defult browser to " + url, e); + _logger.ErrorException("Couldn't open default browser to " + url, e); } } } diff --git a/src/NzbDrone.Host/MainAppContainerBuilder.cs b/src/NzbDrone.Host/MainAppContainerBuilder.cs index 16a8f0fda..4f85b35ec 100644 --- a/src/NzbDrone.Host/MainAppContainerBuilder.cs +++ b/src/NzbDrone.Host/MainAppContainerBuilder.cs @@ -10,12 +10,12 @@ namespace NzbDrone.Host { public class MainAppContainerBuilder : ContainerBuilderBase { - public static IContainer BuildContainer(StartupArguments args) + public static IContainer BuildContainer(StartupContext args) { return new MainAppContainerBuilder(args).Container; } - private MainAppContainerBuilder(StartupArguments args) + private MainAppContainerBuilder(StartupContext args) : base(args, "NzbDrone.Host", "NzbDrone.Common", "NzbDrone.Core", "NzbDrone.Api", "NzbDrone.SignalR") { diff --git a/src/NzbDrone.Host/Router.cs b/src/NzbDrone.Host/Router.cs index 3d8a2b215..900a3a062 100644 --- a/src/NzbDrone.Host/Router.cs +++ b/src/NzbDrone.Host/Router.cs @@ -1,7 +1,5 @@ -using System.ServiceProcess; -using NLog; +using NLog; using NzbDrone.Common; -using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Host { @@ -9,28 +7,18 @@ public class Router { private readonly INzbDroneServiceFactory _nzbDroneServiceFactory; private readonly IServiceProvider _serviceProvider; - private readonly IStartupArguments _startupArguments; private readonly IConsoleService _consoleService; - private readonly IRuntimeInfo _runtimeInfo; private readonly Logger _logger; - public Router(INzbDroneServiceFactory nzbDroneServiceFactory, IServiceProvider serviceProvider, IStartupArguments startupArguments, - IConsoleService consoleService, IRuntimeInfo runtimeInfo, Logger logger) + public Router(INzbDroneServiceFactory nzbDroneServiceFactory, IServiceProvider serviceProvider, + IConsoleService consoleService, Logger logger) { _nzbDroneServiceFactory = nzbDroneServiceFactory; _serviceProvider = serviceProvider; - _startupArguments = startupArguments; _consoleService = consoleService; - _runtimeInfo = runtimeInfo; _logger = logger; } - public void Route() - { - var appMode = GetApplicationMode(); - Route(appMode); - } - public void Route(ApplicationModes applicationModes) { _logger.Info("Application mode: {0}", applicationModes); @@ -86,32 +74,6 @@ public void Route(ApplicationModes applicationModes) } } - private ApplicationModes GetApplicationMode() - { - 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.InstallService) - { - return ApplicationModes.InstallService; - } - - if (!OsInfo.IsLinux && _startupArguments.UninstallService) - { - return ApplicationModes.UninstallService; - } - - return ApplicationModes.Interactive; - } } } diff --git a/src/NzbDrone.Integration.Test/IntegrationTest.cs b/src/NzbDrone.Integration.Test/IntegrationTest.cs index 90a5ad426..aac665ea1 100644 --- a/src/NzbDrone.Integration.Test/IntegrationTest.cs +++ b/src/NzbDrone.Integration.Test/IntegrationTest.cs @@ -50,7 +50,7 @@ protected IEnumerable SignalRMessages public IntegrationTest() { - new StartupArguments(); + new StartupContext(); LogManager.Configuration = new LoggingConfiguration(); var consoleTarget = new ConsoleTarget { Layout = "${level}: ${message} ${exception}" }; diff --git a/src/NzbDrone.Test.Common/LoggingTest.cs b/src/NzbDrone.Test.Common/LoggingTest.cs index 992d1588c..4d11d696a 100644 --- a/src/NzbDrone.Test.Common/LoggingTest.cs +++ b/src/NzbDrone.Test.Common/LoggingTest.cs @@ -12,7 +12,7 @@ public abstract class LoggingTest protected static void InitLogging() { - new StartupArguments(); + new StartupContext(); TestLogger = LogManager.GetLogger("TestLogger"); diff --git a/src/NzbDrone.Test.Common/TestBase.cs b/src/NzbDrone.Test.Common/TestBase.cs index d6382b397..470cba8e1 100644 --- a/src/NzbDrone.Test.Common/TestBase.cs +++ b/src/NzbDrone.Test.Common/TestBase.cs @@ -93,7 +93,7 @@ public void TestBaseSetup() Mocker.SetConstant(LogManager.GetLogger("TestLogger")); - Mocker.SetConstant(new StartupArguments(new string[0])); + Mocker.SetConstant(new StartupContext(new string[0])); LogManager.ReconfigExistingLoggers(); diff --git a/src/NzbDrone.Update.Test/StartNzbDroneService.cs b/src/NzbDrone.Update.Test/StartNzbDroneService.cs index 3051e529f..75fc9d917 100644 --- a/src/NzbDrone.Update.Test/StartNzbDroneService.cs +++ b/src/NzbDrone.Update.Test/StartNzbDroneService.cs @@ -32,7 +32,7 @@ public void should_start_console_if_app_type_was_service_but_start_failed_becaus Subject.Start(AppType.Service, targetFolder); - Mocker.GetMock().Verify(c => c.SpawnNewProcess("c:\\NzbDrone\\NzbDrone.Console.exe", StartupArguments.NO_BROWSER), Times.Once()); + Mocker.GetMock().Verify(c => c.SpawnNewProcess("c:\\NzbDrone\\NzbDrone.Console.exe", StartupContext.NO_BROWSER), Times.Once()); ExceptionVerification.ExpectedWarns(1); } diff --git a/src/NzbDrone.Update/UpdateApp.cs b/src/NzbDrone.Update/UpdateApp.cs index 0f42e25b1..83531b153 100644 --- a/src/NzbDrone.Update/UpdateApp.cs +++ b/src/NzbDrone.Update/UpdateApp.cs @@ -28,7 +28,7 @@ public static void Main(string[] args) { try { - var startupArgument = new StartupArguments(args); + var startupArgument = new StartupContext(args); LogTargets.Register(startupArgument, true, true); Console.WriteLine("Starting NzbDrone Update Client"); diff --git a/src/NzbDrone.Update/UpdateContainerBuilder.cs b/src/NzbDrone.Update/UpdateContainerBuilder.cs index faac971eb..5f99a618c 100644 --- a/src/NzbDrone.Update/UpdateContainerBuilder.cs +++ b/src/NzbDrone.Update/UpdateContainerBuilder.cs @@ -5,15 +5,15 @@ namespace NzbDrone.Update { public class UpdateContainerBuilder : ContainerBuilderBase { - private UpdateContainerBuilder(IStartupArguments startupArguments) - : base(startupArguments, "NzbDrone.Update", "NzbDrone.Common") + private UpdateContainerBuilder(IStartupContext startupContext) + : base(startupContext, "NzbDrone.Update", "NzbDrone.Common") { } - public static IContainer Build(IStartupArguments startupArguments) + public static IContainer Build(IStartupContext startupContext) { - return new UpdateContainerBuilder(startupArguments).Container; + return new UpdateContainerBuilder(startupContext).Container; } } } \ No newline at end of file diff --git a/src/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs b/src/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs index 211961a83..63079033a 100644 --- a/src/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs +++ b/src/NzbDrone.Update/UpdateEngine/StartNzbDrone.cs @@ -73,7 +73,7 @@ private void Start(string installationFolder, string fileName) _logger.Info("Starting {0}", fileName); var path = Path.Combine(installationFolder, fileName); - _processProvider.SpawnNewProcess(path, StartupArguments.NO_BROWSER); + _processProvider.SpawnNewProcess(path, StartupContext.NO_BROWSER); } } } \ No newline at end of file diff --git a/src/NzbDrone/SysTray/SysTrayApp.cs b/src/NzbDrone/SysTray/SysTrayApp.cs index 47c5342c1..62e73a650 100644 --- a/src/NzbDrone/SysTray/SysTrayApp.cs +++ b/src/NzbDrone/SysTray/SysTrayApp.cs @@ -3,7 +3,6 @@ using System.Windows.Forms; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Host; -using NzbDrone.Host.Owin; namespace NzbDrone.SysTray { diff --git a/src/NzbDrone/WindowsApp.cs b/src/NzbDrone/WindowsApp.cs index 8d070e502..d2b82a2a1 100644 --- a/src/NzbDrone/WindowsApp.cs +++ b/src/NzbDrone/WindowsApp.cs @@ -16,22 +16,16 @@ public static void Main(string[] args) { try { - var startupArgs = new StartupArguments(args); + var startupArgs = new StartupContext(args); LogTargets.Register(startupArgs, false, true); - var bootstrap = new Bootstrap(startupArgs, new MessageBoxUserAlert()); - - bootstrap.EnsureSingleInstance(); - - bootstrap.Start(); - bootstrap.Container.Register(); - bootstrap.Container.Resolve().Start(); - - } - catch (TerminateApplicationException e) - { - Logger.Info("Application has been terminated. Reason " + e.Reason); + Bootstrap.Start(startupArgs, new MessageBoxUserAlert(), container => + { + container.Register(); + var trayApp = container.Resolve(); + trayApp.Start(); + }); } catch (Exception e) {