From 125703a2faae971053292ce8f691aba171269940 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 6 Jan 2013 00:11:14 -0800 Subject: [PATCH] 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 --- NzbDrone.App.Test/RouterTest.cs | 6 + NzbDrone.Common/ConsoleProvider.cs | 1 + NzbDrone.Common/EnvironmentProvider.cs | 4 +- NzbDrone.Common/IISProvider.cs | 11 +- NzbDrone.Common/PathExtentions.cs | 9 +- NzbDrone.Common/ProcessProvider.cs | 2 +- NzbDrone.Core/CentralDispatch.cs | 2 + NzbDrone.Core/Jobs/AppRestartJob.cs | 51 ++++++ NzbDrone.Core/Jobs/AppShutdownJob.cs | 61 +++++++ NzbDrone.Core/NzbDrone.Core.csproj | 2 + .../UpdateProviderStartFixture.cs | 2 +- NzbDrone.Update/AppType.cs | 14 ++ NzbDrone.Update/NzbDrone.Update.csproj | 1 + NzbDrone.Update/Providers/UpdateProvider.cs | 26 ++- NzbDrone.Web/Controllers/LogController.cs | 5 - NzbDrone.Web/Controllers/SystemController.cs | 74 ++++++--- .../FakesAssemblies/Ninject.Web.Mvc.Fakes.dll | Bin 91136 -> 91136 bytes NzbDrone.Web/NzbDrone.Web.csproj | 3 +- NzbDrone.Web/Views/Shared/_Layout.cshtml | 2 +- NzbDrone.Web/Views/System/Index.cshtml | 32 ++++ .../{Log/Index.cshtml => System/Logs.cshtml} | 0 NzbDrone.sln | 26 +++ NzbDrone/AppMain.cs | 2 - NzbDrone/Model/ApplicationMode.cs | 3 +- NzbDrone/NzbDrone.Console.csproj | 152 ++++++++++++++++++ NzbDrone/NzbDrone.csproj | 2 +- NzbDrone/Router.cs | 101 +++++++----- .../ServiceInstall/ServiceHelper.cs | 2 +- .../ServiceUninstall/ServiceHelper.cs | 2 +- 29 files changed, 502 insertions(+), 96 deletions(-) create mode 100644 NzbDrone.Core/Jobs/AppRestartJob.cs create mode 100644 NzbDrone.Core/Jobs/AppShutdownJob.cs create mode 100644 NzbDrone.Update/AppType.cs create mode 100644 NzbDrone.Web/Views/System/Index.cshtml rename NzbDrone.Web/Views/{Log/Index.cshtml => System/Logs.cshtml} (100%) create mode 100644 NzbDrone/NzbDrone.Console.csproj diff --git a/NzbDrone.App.Test/RouterTest.cs b/NzbDrone.App.Test/RouterTest.cs index b458a46f2..4af22e6db 100644 --- a/NzbDrone.App.Test/RouterTest.cs +++ b/NzbDrone.App.Test/RouterTest.cs @@ -31,6 +31,12 @@ namespace NzbDrone.App.Test [TestCase("/u", ApplicationMode.UninstallService)] [TestCase("-U", ApplicationMode.UninstallService)] [TestCase("-u", ApplicationMode.UninstallService)] + [TestCase("s", ApplicationMode.Silent)] + [TestCase("S", ApplicationMode.Silent)] + [TestCase("/S", ApplicationMode.Silent)] + [TestCase("/s", ApplicationMode.Silent)] + [TestCase("-S", ApplicationMode.Silent)] + [TestCase("-s", ApplicationMode.Silent)] public void GetApplicationMode_single_arg(string arg, ApplicationMode mode) { Router.GetApplicationMode(new[] { arg }).Should().Be(mode); diff --git a/NzbDrone.Common/ConsoleProvider.cs b/NzbDrone.Common/ConsoleProvider.cs index b942e612c..bbce93c94 100644 --- a/NzbDrone.Common/ConsoleProvider.cs +++ b/NzbDrone.Common/ConsoleProvider.cs @@ -20,6 +20,7 @@ namespace NzbDrone.Common Console.WriteLine(" Commands:"); Console.WriteLine(" /i Install the application as a Windows Service ({0}).", ServiceProvider.NZBDRONE_SERVICE_NAME); Console.WriteLine(" /u Uninstall already installed Windows Service ({0}).", ServiceProvider.NZBDRONE_SERVICE_NAME); + Console.WriteLine(" /s Run NzbDrone without a Console Window."); Console.WriteLine(" Run application in console mode."); } diff --git a/NzbDrone.Common/EnvironmentProvider.cs b/NzbDrone.Common/EnvironmentProvider.cs index 365a1f61f..1cd2ffcee 100644 --- a/NzbDrone.Common/EnvironmentProvider.cs +++ b/NzbDrone.Common/EnvironmentProvider.cs @@ -74,7 +74,7 @@ namespace NzbDrone.Common if (!string.IsNullOrWhiteSpace(applicationPath)) return applicationPath; - applicationPath = CrawlToRoot(NzbDronePathFromEnviroment); + applicationPath = CrawlToRoot(NzbDronePathFromEnvironment); if (!string.IsNullOrWhiteSpace(applicationPath)) return applicationPath; @@ -146,7 +146,7 @@ namespace NzbDrone.Common } } - public virtual string NzbDronePathFromEnviroment + public virtual string NzbDronePathFromEnvironment { get { diff --git a/NzbDrone.Common/IISProvider.cs b/NzbDrone.Common/IISProvider.cs index b04952dc6..043cad7b9 100644 --- a/NzbDrone.Common/IISProvider.cs +++ b/NzbDrone.Common/IISProvider.cs @@ -51,7 +51,6 @@ namespace NzbDrone.Common startInfo.RedirectStandardError = true; startInfo.CreateNoWindow = true; - startInfo.EnvironmentVariables[EnvironmentProvider.NZBDRONE_PATH] = _environmentProvider.ApplicationPath; startInfo.EnvironmentVariables[EnvironmentProvider.NZBDRONE_PID] = Process.GetCurrentProcess().Id.ToString(); @@ -74,6 +73,9 @@ namespace NzbDrone.Common iisProcess.BeginOutputReadLine(); ServerStarted = true; + + iisProcess.EnableRaisingEvents = true; + iisProcess.Exited += IIS_EXITED; } private static void OnErrorDataReceived(object sender, DataReceivedEventArgs e) @@ -84,7 +86,6 @@ namespace NzbDrone.Common IISLogger.Error(e.Data); } - public void RestartServer() { ServerStarted = false; @@ -93,7 +94,6 @@ namespace NzbDrone.Common StartServer(); } - public virtual void StopServer() { _processProvider.Kill(IISProcessId); @@ -114,6 +114,10 @@ namespace NzbDrone.Common } } + public void IIS_EXITED(object obj, EventArgs args) + { + RestartServer(); + } private void OnOutputDataReceived(object s, DataReceivedEventArgs e) { @@ -123,6 +127,5 @@ namespace NzbDrone.Common Console.WriteLine(e.Data); } - } } \ No newline at end of file diff --git a/NzbDrone.Common/PathExtentions.cs b/NzbDrone.Common/PathExtentions.cs index 54d0082f4..38e1fa78a 100644 --- a/NzbDrone.Common/PathExtentions.cs +++ b/NzbDrone.Common/PathExtentions.cs @@ -9,11 +9,11 @@ namespace NzbDrone.Common private const string APP_DATA = "App_Data\\"; public const string IIS_FOLDER = "IISExpress"; public const string IIS_EXE = "iisexpress.exe"; - - + private const string LOG_CONFIG_FILE = "log.config"; private const string APP_CONFIG_FILE = "config.xml"; + public const string NZBDRONE_EXE = "NzbDrone.exe"; public const string NZBDRONE_DB_FILE = "nzbdrone.sdf"; public const string LOG_DB_FILE = "log.sdf"; @@ -156,5 +156,10 @@ namespace NzbDrone.Common { return Path.Combine(environmentProvider.GetAppDataPath(), BACKUP_ZIP_FILE); } + + public static string GetNzbDroneExe(this EnvironmentProvider environmentProvider) + { + return Path.Combine(environmentProvider.ApplicationPath, NZBDRONE_EXE); + } } } \ No newline at end of file diff --git a/NzbDrone.Common/ProcessProvider.cs b/NzbDrone.Common/ProcessProvider.cs index 9943d9ac5..120c1eed5 100644 --- a/NzbDrone.Common/ProcessProvider.cs +++ b/NzbDrone.Common/ProcessProvider.cs @@ -11,6 +11,7 @@ namespace NzbDrone.Common private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public static readonly string NzbDroneProccessName = "NzbDrone"; + public static readonly string NzbDroneConsoleProccessName = "NzbDrone.Console"; public virtual ProcessInfo GetCurrentProcess() { @@ -107,6 +108,5 @@ namespace NzbDrone.Common Name = process.ProcessName }; } - } } \ No newline at end of file diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index 63448a040..dcf558695 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -136,6 +136,8 @@ namespace NzbDrone.Core Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); Kernel.Get().Initialize(); Kernel.Get().StartTimer(30); diff --git a/NzbDrone.Core/Jobs/AppRestartJob.cs b/NzbDrone.Core/Jobs/AppRestartJob.cs new file mode 100644 index 000000000..5fe53ca96 --- /dev/null +++ b/NzbDrone.Core/Jobs/AppRestartJob.cs @@ -0,0 +1,51 @@ +using System; +using System.Linq; +using System.Diagnostics; +using System.IO; +using NLog; +using Ninject; +using NzbDrone.Common; +using NzbDrone.Core.Model.Notification; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Providers.Core; + +namespace NzbDrone.Core.Jobs +{ + public class AppRestartJob : IJob + { + private readonly EnvironmentProvider _environmentProvider; + private readonly ProcessProvider _processProvider; + private readonly ServiceProvider _serviceProvider; + private readonly IISProvider _iisProvider; + + private static readonly Logger logger = LogManager.GetCurrentClassLogger(); + + [Inject] + public AppRestartJob(EnvironmentProvider environmentProvider, ProcessProvider processProvider, + ServiceProvider serviceProvider, IISProvider iisProvider) + { + _environmentProvider = environmentProvider; + _processProvider = processProvider; + _serviceProvider = serviceProvider; + _iisProvider = iisProvider; + } + + public string Name + { + get { return "Restart NzbDrone"; } + } + + public TimeSpan DefaultInterval + { + get { return TimeSpan.FromTicks(0); } + } + + public virtual void Start(ProgressNotification notification, dynamic options) + { + notification.CurrentMessage = "Restarting NzbDrone"; + logger.Info("Restarting NzbDrone"); + + _iisProvider.StopServer(); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Jobs/AppShutdownJob.cs b/NzbDrone.Core/Jobs/AppShutdownJob.cs new file mode 100644 index 000000000..c831e8f84 --- /dev/null +++ b/NzbDrone.Core/Jobs/AppShutdownJob.cs @@ -0,0 +1,61 @@ +using System; +using System.Linq; +using System.Diagnostics; +using System.IO; +using NLog; +using Ninject; +using NzbDrone.Common; +using NzbDrone.Core.Model.Notification; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Providers.Core; + +namespace NzbDrone.Core.Jobs +{ + public class AppShutdownJob : IJob + { + private readonly EnvironmentProvider _environmentProvider; + private readonly ProcessProvider _processProvider; + private readonly ServiceProvider _serviceProvider; + + private static readonly Logger logger = LogManager.GetCurrentClassLogger(); + + [Inject] + public AppShutdownJob(EnvironmentProvider environmentProvider, ProcessProvider processProvider, ServiceProvider serviceProvider) + { + _environmentProvider = environmentProvider; + _processProvider = processProvider; + _serviceProvider = serviceProvider; + } + + public string Name + { + get { return "Shutdown NzbDrone"; } + } + + public TimeSpan DefaultInterval + { + get { return TimeSpan.FromTicks(0); } + } + + public virtual void Start(ProgressNotification notification, dynamic options) + { + notification.CurrentMessage = "Shutting down NzbDrone"; + logger.Info("Shutting down NzbDrone"); + + if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME) + && _serviceProvider.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)) + { + logger.Debug("Stopping NzbDrone Service"); + _serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME); + } + + else + { + logger.Debug("Stopping NzbDrone console"); + + var pid = _environmentProvider.NzbDroneProcessIdFromEnviroment; + _processProvider.Kill(pid); + } + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 8b7f2182f..1888851f0 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -268,6 +268,8 @@ + + diff --git a/NzbDrone.Update.Test/UpdateProviderStartFixture.cs b/NzbDrone.Update.Test/UpdateProviderStartFixture.cs index bcafbb6e3..a2fb25eda 100644 --- a/NzbDrone.Update.Test/UpdateProviderStartFixture.cs +++ b/NzbDrone.Update.Test/UpdateProviderStartFixture.cs @@ -242,7 +242,7 @@ namespace NzbDrone.Update.Test .Verify(c => c.Start(It.IsAny()), Times.Never()); Mocker.GetMock() - .Verify(c => c.Start(TARGET_FOLDER + "nzbdrone.exe"), Times.Once()); + .Verify(c => c.Start(TARGET_FOLDER + "NzbDrone.exe"), Times.Once()); } diff --git a/NzbDrone.Update/AppType.cs b/NzbDrone.Update/AppType.cs new file mode 100644 index 000000000..c7236a893 --- /dev/null +++ b/NzbDrone.Update/AppType.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Update +{ + public enum AppType + { + Normal, + Console, + Service + } +} diff --git a/NzbDrone.Update/NzbDrone.Update.csproj b/NzbDrone.Update/NzbDrone.Update.csproj index ef52b1f25..eaba2a039 100644 --- a/NzbDrone.Update/NzbDrone.Update.csproj +++ b/NzbDrone.Update/NzbDrone.Update.csproj @@ -51,6 +51,7 @@ Properties\SharedAssemblyInfo.cs + diff --git a/NzbDrone.Update/Providers/UpdateProvider.cs b/NzbDrone.Update/Providers/UpdateProvider.cs index e6d1d24bc..fccc3c5d6 100644 --- a/NzbDrone.Update/Providers/UpdateProvider.cs +++ b/NzbDrone.Update/Providers/UpdateProvider.cs @@ -45,20 +45,19 @@ namespace NzbDrone.Update.Providers logger.Info("Verifying Update Folder"); if (!_diskProvider.FolderExists(_environmentProvider.GetUpdatePackageFolder())) throw new DirectoryNotFoundException("Update folder doesn't exist " + _environmentProvider.GetUpdatePackageFolder()); - } public virtual void Start(string targetFolder) { Verify(targetFolder); - bool isService = false; + AppType appType = AppType.Normal; logger.Info("Stopping all running services"); if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME) && _serviceProvider.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)) { - isService = true; + appType = AppType.Service; _serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME); } @@ -68,6 +67,14 @@ namespace NzbDrone.Update.Providers var processes = _processProvider.GetProcessByName(ProcessProvider.NzbDroneProccessName); foreach (var processInfo in processes) { + appType = AppType.Normal; + _processProvider.Kill(processInfo.Id); + } + + var consoleProcesses = _processProvider.GetProcessByName(ProcessProvider.NzbDroneConsoleProccessName); + foreach (var processInfo in consoleProcesses) + { + appType = AppType.Console; _processProvider.Kill(processInfo.Id); } @@ -77,7 +84,6 @@ namespace NzbDrone.Update.Providers logger.Info("Creating backup of existing installation"); _diskProvider.CopyDirectory(targetFolder, _environmentProvider.GetUpdateBackUpFolder()); - logger.Info("Moving update package to target"); try @@ -100,7 +106,7 @@ namespace NzbDrone.Update.Providers } finally { - StartNzbDrone(isService, targetFolder); + StartNzbDrone(appType, targetFolder); } } @@ -112,15 +118,19 @@ namespace NzbDrone.Update.Providers } - private void StartNzbDrone(bool isService, string targetFolder) + private void StartNzbDrone(AppType appType, string targetFolder) { - if (isService) + if (appType == AppType.Service) { _serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME); } + else if(appType == AppType.Console) + { + _processProvider.Start(Path.Combine(targetFolder, "NzbDrone.Console.exe")); + } else { - _processProvider.Start(Path.Combine(targetFolder, "nzbdrone.exe")); + _processProvider.Start(Path.Combine(targetFolder, "NzbDrone.exe")); } } } diff --git a/NzbDrone.Web/Controllers/LogController.cs b/NzbDrone.Web/Controllers/LogController.cs index bc31e062a..4cb3962c7 100644 --- a/NzbDrone.Web/Controllers/LogController.cs +++ b/NzbDrone.Web/Controllers/LogController.cs @@ -26,11 +26,6 @@ namespace NzbDrone.Web.Controllers _diskProvider = diskProvider; } - public ActionResult Index() - { - return View(); - } - public FileContentResult File() { string log = string.Empty; diff --git a/NzbDrone.Web/Controllers/SystemController.cs b/NzbDrone.Web/Controllers/SystemController.cs index 3e08afc24..b7065e6b1 100644 --- a/NzbDrone.Web/Controllers/SystemController.cs +++ b/NzbDrone.Web/Controllers/SystemController.cs @@ -36,6 +36,16 @@ namespace NzbDrone.Web.Controllers _statsProvider = statsProvider; } + public ActionResult Index() + { + return View(); + } + + public ActionResult Logs() + { + return View(); + } + public ActionResult Jobs() { var queue = _jobProvider.Queue.Select(c => new JobQueueItemModel @@ -80,32 +90,6 @@ namespace NzbDrone.Web.Controllers return View((object)serialized); } - public JsonResult SelectConfigAjax() - { - var config = _configProvider.All(); - - return Json(new - { - iTotalRecords = config.Count(), - iTotalDisplayRecords = config.Count(), - aaData = config - }, JsonRequestBehavior.AllowGet); - } - - [HttpPost] - public string SaveConfigAjax(string id, string value) - { - _configProvider.SetValue(id, value); - return value; - } - - [HttpPost] - public string InsertConfigAjax(string key, string value) - { - _configProvider.SetValue(key, value); - return key; - } - //PostDownloadView public ActionResult PendingProcessing() { @@ -184,5 +168,43 @@ namespace NzbDrone.Web.Controllers return View(model); } + + public JsonResult Restart() + { + _jobProvider.QueueJob(typeof(AppRestartJob)); + return JsonNotificationResult.Info("NzbDrone will restart shortly"); + } + + public JsonResult Shutdown() + { + _jobProvider.QueueJob(typeof(AppShutdownJob)); + return JsonNotificationResult.Info("NzbDrone will shutdown shortly"); + } + + public JsonResult SelectConfigAjax() + { + var config = _configProvider.All(); + + return Json(new + { + iTotalRecords = config.Count(), + iTotalDisplayRecords = config.Count(), + aaData = config + }, JsonRequestBehavior.AllowGet); + } + + [HttpPost] + public string SaveConfigAjax(string id, string value) + { + _configProvider.SetValue(id, value); + return value; + } + + [HttpPost] + public string InsertConfigAjax(string key, string value) + { + _configProvider.SetValue(key, value); + return key; + } } } diff --git a/NzbDrone.Web/FakesAssemblies/Ninject.Web.Mvc.Fakes.dll b/NzbDrone.Web/FakesAssemblies/Ninject.Web.Mvc.Fakes.dll index 591db91e6583aaa610a19122b91a34f35f63a8da..96997472452b91054bac8f48e85689c73fd95415 100644 GIT binary patch delta 249 zcmV3q|#u$T6E0nN8aTmiTT3t4OV delta 248 zcmV + + @@ -528,7 +530,6 @@ - diff --git a/NzbDrone.Web/Views/Shared/_Layout.cshtml b/NzbDrone.Web/Views/Shared/_Layout.cshtml index 8d3ac22ef..ecd94320d 100644 --- a/NzbDrone.Web/Views/Shared/_Layout.cshtml +++ b/NzbDrone.Web/Views/Shared/_Layout.cshtml @@ -29,7 +29,7 @@ @MvcHtmlString.Create(Html.CurrentControllerLink("History", "Index", "History")) @MvcHtmlString.Create(Html.CurrentControllerLink("Missing", "Index", "Missing")) @MvcHtmlString.Create(Html.CurrentControllerLink("Settings", "Index", "Settings")) - @MvcHtmlString.Create(Html.CurrentControllerLink("Logs", "Index", "Log")) + @MvcHtmlString.Create(Html.CurrentControllerLink("System", "Index", "System")) diff --git a/NzbDrone.Web/Views/System/Index.cshtml b/NzbDrone.Web/Views/System/Index.cshtml new file mode 100644 index 000000000..7ea2cd454 --- /dev/null +++ b/NzbDrone.Web/Views/System/Index.cshtml @@ -0,0 +1,32 @@ +@using NzbDrone.Web.Helpers +@{ + ViewBag.Title = "System"; +} + +@section HeaderContent +{ + @Html.IncludeCss("Settings.css") + +} + +@section ActionMenu{ + +} + +
+
+ + +
+
\ No newline at end of file diff --git a/NzbDrone.Web/Views/Log/Index.cshtml b/NzbDrone.Web/Views/System/Logs.cshtml similarity index 100% rename from NzbDrone.Web/Views/Log/Index.cshtml rename to NzbDrone.Web/Views/System/Logs.cshtml diff --git a/NzbDrone.sln b/NzbDrone.sln index 1bae6bd8b..34f1a88ff 100644 --- a/NzbDrone.sln +++ b/NzbDrone.sln @@ -51,6 +51,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{1E6B3C EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NzbDrone.Api", "NzbDrone.Api\NzbDrone.Api.csproj", "{FD286DF8-2D3A-4394-8AD5-443FADE55FB2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NzbDrone.Console", "NzbDrone\NzbDrone.Console.csproj", "{3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -494,6 +496,30 @@ Global {FD286DF8-2D3A-4394-8AD5-443FADE55FB2}.Services|Mixed Platforms.Build.0 = Release|Any CPU {FD286DF8-2D3A-4394-8AD5-443FADE55FB2}.Services|x64.ActiveCfg = Release|Any CPU {FD286DF8-2D3A-4394-8AD5-443FADE55FB2}.Services|x86.ActiveCfg = Release|Any CPU + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Debug|Any CPU.ActiveCfg = Debug|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Debug|Mixed Platforms.Build.0 = Debug|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Debug|x64.ActiveCfg = Debug|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Debug|x86.ActiveCfg = Debug|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Debug|x86.Build.0 = Debug|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Pilot|Any CPU.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Pilot|Mixed Platforms.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Pilot|Mixed Platforms.Build.0 = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Pilot|x64.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Pilot|x86.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Pilot|x86.Build.0 = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Release|Any CPU.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Release|Mixed Platforms.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Release|Mixed Platforms.Build.0 = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Release|x64.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Release|x86.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Release|x86.Build.0 = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Services|Any CPU.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Services|Mixed Platforms.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Services|Mixed Platforms.Build.0 = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Services|x64.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Services|x86.ActiveCfg = Release|x86 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976}.Services|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/NzbDrone/AppMain.cs b/NzbDrone/AppMain.cs index e6c0cf332..5e459d0b3 100644 --- a/NzbDrone/AppMain.cs +++ b/NzbDrone/AppMain.cs @@ -45,7 +45,5 @@ namespace NzbDrone MonitoringProvider.AppDomainException(e); } } - - } } \ No newline at end of file diff --git a/NzbDrone/Model/ApplicationMode.cs b/NzbDrone/Model/ApplicationMode.cs index 58eccb381..0222c15ca 100644 --- a/NzbDrone/Model/ApplicationMode.cs +++ b/NzbDrone/Model/ApplicationMode.cs @@ -5,6 +5,7 @@ Console, Help, InstallService, - UninstallService + UninstallService, + Service } } diff --git a/NzbDrone/NzbDrone.Console.csproj b/NzbDrone/NzbDrone.Console.csproj new file mode 100644 index 000000000..a80053dee --- /dev/null +++ b/NzbDrone/NzbDrone.Console.csproj @@ -0,0 +1,152 @@ + + + + Debug + x86 + 8.0.30703 + 2.0 + {3DCA7B58-B8B3-49AC-9D9E-56F4A0460976} + Exe + Properties + NzbDrone + NzbDrone.Console + v4.0 + 512 + + false + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + true + ..\ + true + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + true + BasicCorrectnessRules.ruleset + C:\Users\Mark\AppData\Local\Temp\vs32F1.tmp\x86\Debug\ + + + x86 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + C:\Users\Mark\AppData\Local\Temp\vs32F1.tmp\x86\Release\ + + + NzbDrone.ico + + + NzbDrone.AppMain + + + OnOutputUpdated + + + + True + + + True + + + False + ..\packages\Ninject.3.0.1.10\lib\net40\Ninject.dll + + + ..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll + + + + + + + + Properties\SharedAssemblyInfo.cs + + + Component + + + + + + + + + + + + + + + + + + + + False + Microsoft .NET Framework 4 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + + + False + Windows Installer 3.1 + true + + + + + {F2BE0FDF-6E47-4827-A420-DD4EF82407F8} + NzbDrone.Common + + + + + + + + + + + + + + \ No newline at end of file diff --git a/NzbDrone/NzbDrone.csproj b/NzbDrone/NzbDrone.csproj index 9f1534a77..94c4c7f8c 100644 --- a/NzbDrone/NzbDrone.csproj +++ b/NzbDrone/NzbDrone.csproj @@ -6,7 +6,7 @@ 8.0.30703 2.0 {D12F7F2F-8A3C-415F-88FA-6DD061A84869} - Exe + WinExe Properties NzbDrone NzbDrone diff --git a/NzbDrone/Router.cs b/NzbDrone/Router.cs index 996d61dea..b5e4209ef 100644 --- a/NzbDrone/Router.cs +++ b/NzbDrone/Router.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using NLog; using NzbDrone.Common; @@ -15,73 +16,94 @@ namespace NzbDrone 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) + 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 args) { - Route(GetApplicationMode(args)); } public void Route(ApplicationMode applicationMode) { - logger.Info("Application mode: {0}", applicationMode); + logger.Info("Application mode: {0}", applicationMode); - //TODO:move this outside, it should be one of application modes (ApplicationMode.Service?) - if (!_environmentProvider.IsUserInteractive) + if(!_environmentProvider.IsUserInteractive) { - _serviceProvider.Run(_applicationServer); + applicationMode = ApplicationMode.Service; } - else - { + switch (applicationMode) - { + { + case ApplicationMode.Service: + { + _serviceProvider.Run(_applicationServer); + break; + } - case ApplicationMode.Console: + case ApplicationMode.Console: + { + _applicationServer.Start(); + _consoleProvider.WaitForClose(); + break; + } + case ApplicationMode.InstallService: + { + if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)) { - _applicationServer.Start(); - _consoleProvider.WaitForClose(); - break; + _consoleProvider.PrintServiceAlreadyExist(); } - case ApplicationMode.InstallService: + else { - if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)) - { - _consoleProvider.PrintServiceAlreadyExist(); - } - else - { - _serviceProvider.Install(ServiceProvider.NZBDRONE_SERVICE_NAME); - _serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME); - } - break; + _serviceProvider.Install(ServiceProvider.NZBDRONE_SERVICE_NAME); + _serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME); } - case ApplicationMode.UninstallService: + break; + } + case ApplicationMode.UninstallService: + { + if (!_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)) { - if (!_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)) - { - _consoleProvider.PrintServiceDoestExist(); - } - else - { - _serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME); - } - - break; + _consoleProvider.PrintServiceDoestExist(); } - default: + else { - _consoleProvider.PrintHelp(); - break; + _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; + } } } @@ -97,6 +119,7 @@ namespace NzbDrone if (arg == "i") return ApplicationMode.InstallService; if (arg == "u") return ApplicationMode.UninstallService; + if (arg == "s") return ApplicationMode.Silent; return ApplicationMode.Help; } diff --git a/ServiceHelpers/ServiceInstall/ServiceHelper.cs b/ServiceHelpers/ServiceInstall/ServiceHelper.cs index c8c96d0a5..c066a4b18 100644 --- a/ServiceHelpers/ServiceInstall/ServiceHelper.cs +++ b/ServiceHelpers/ServiceInstall/ServiceHelper.cs @@ -13,7 +13,7 @@ namespace ServiceInstall { get { - return Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "nzbdrone.exe"); + return Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "NzbDrone.Console.exe"); } } diff --git a/ServiceHelpers/ServiceUninstall/ServiceHelper.cs b/ServiceHelpers/ServiceUninstall/ServiceHelper.cs index 1069c2fff..f20f08c75 100644 --- a/ServiceHelpers/ServiceUninstall/ServiceHelper.cs +++ b/ServiceHelpers/ServiceUninstall/ServiceHelper.cs @@ -13,7 +13,7 @@ namespace ServiceUninstall { get { - return Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "nzbdrone.exe"); + return Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, "NzbDrone.Console.exe"); } }