From 6b7923bd528d02a0d7c3249036f28d1df95452ee Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Wed, 12 Oct 2011 19:24:30 -0700 Subject: [PATCH] Windows service is half working, --- NzbDrone.App.Test/CentralDispatchTests.cs | 17 ++++++----- NzbDrone.App.Test/NzbDrone.App.Test.csproj | 4 ++- NzbDrone.App.Test/ServiceControllerTests.cs | 11 +++++++ NzbDrone.App.Test/packages.config | 1 + NzbDrone/CentralDispatch.cs | 4 +++ NzbDrone/NzbDrone.csproj | 3 ++ NzbDrone/NzbDroneConsole.cs | 9 +++--- NzbDrone/NzbDroneService.cs | 33 ++++++++++++++++++--- NzbDrone/Providers/ConsoleProvider.cs | 8 +++-- NzbDrone/Providers/EnviromentProvider.cs | 12 ++++++-- NzbDrone/Providers/ServiceProvider.cs | 4 ++- NzbDrone/Router.cs | 9 ++++-- 12 files changed, 92 insertions(+), 23 deletions(-) diff --git a/NzbDrone.App.Test/CentralDispatchTests.cs b/NzbDrone.App.Test/CentralDispatchTests.cs index 67d1790c8..7cf1d74db 100644 --- a/NzbDrone.App.Test/CentralDispatchTests.cs +++ b/NzbDrone.App.Test/CentralDispatchTests.cs @@ -1,12 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using FluentAssertions; +using FluentAssertions; using NUnit.Framework; using Ninject; -using NzbDrone.Providers; namespace NzbDrone.App.Test { @@ -36,5 +30,14 @@ namespace NzbDrone.App.Test appServer.Should().NotBeNull(); } + [Test] + public void Kernel_should_resolve_same_ApplicationServer_instance() + { + var appServer1 = CentralDispatch.Kernel.Get(); + var appServer2 = CentralDispatch.Kernel.Get(); + + appServer1.Should().BeSameAs(appServer2); + } + } } diff --git a/NzbDrone.App.Test/NzbDrone.App.Test.csproj b/NzbDrone.App.Test/NzbDrone.App.Test.csproj index fe0aa62d4..32ddcc403 100644 --- a/NzbDrone.App.Test/NzbDrone.App.Test.csproj +++ b/NzbDrone.App.Test/NzbDrone.App.Test.csproj @@ -50,7 +50,9 @@ ..\packages\Moq.4.0.10827\lib\NET40\Moq.dll - + + ..\packages\Ninject.2.2.1.4\lib\net40-Full\Ninject.dll + ..\packages\NUnit.2.5.10.11092\lib\nunit.framework.dll diff --git a/NzbDrone.App.Test/ServiceControllerTests.cs b/NzbDrone.App.Test/ServiceControllerTests.cs index 9fcc1b33a..00534788d 100644 --- a/NzbDrone.App.Test/ServiceControllerTests.cs +++ b/NzbDrone.App.Test/ServiceControllerTests.cs @@ -46,5 +46,16 @@ namespace NzbDrone.App.Test serviceController.UnInstall(); serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse(); } + + [Test] + [Explicit] + public void UnInstallService() + { + var serviceController = new ServiceProvider(); + + //Act + serviceController.UnInstall(); + serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse(); + } } } diff --git a/NzbDrone.App.Test/packages.config b/NzbDrone.App.Test/packages.config index 0132851a6..d52a9cd18 100644 --- a/NzbDrone.App.Test/packages.config +++ b/NzbDrone.App.Test/packages.config @@ -4,6 +4,7 @@ + \ No newline at end of file diff --git a/NzbDrone/CentralDispatch.cs b/NzbDrone/CentralDispatch.cs index bc2df874f..10b96c2c2 100644 --- a/NzbDrone/CentralDispatch.cs +++ b/NzbDrone/CentralDispatch.cs @@ -18,6 +18,8 @@ namespace NzbDrone static CentralDispatch() { _kernel = new StandardKernel(); + BindKernel(); + InitilizeApp(); } public static ApplicationMode ApplicationMode { get; set; } @@ -33,6 +35,7 @@ namespace NzbDrone private static void BindKernel() { _kernel = new StandardKernel(); + _kernel.Bind().ToSelf().InSingletonScope(); _kernel.Bind().ToSelf().InSingletonScope(); _kernel.Bind().ToSelf().InSingletonScope(); _kernel.Bind().ToSelf().InSingletonScope(); @@ -42,6 +45,7 @@ namespace NzbDrone _kernel.Bind().ToSelf().InSingletonScope(); _kernel.Bind().ToSelf().InSingletonScope(); _kernel.Bind().ToSelf().InSingletonScope(); + } private static void InitilizeApp() diff --git a/NzbDrone/NzbDrone.csproj b/NzbDrone/NzbDrone.csproj index 329dde2c8..62dc61b76 100644 --- a/NzbDrone/NzbDrone.csproj +++ b/NzbDrone/NzbDrone.csproj @@ -53,6 +53,9 @@ NzbDrone.ico + + NzbDrone.NzbDroneConsole + True diff --git a/NzbDrone/NzbDroneConsole.cs b/NzbDrone/NzbDroneConsole.cs index a0f6f1422..f067a09b0 100644 --- a/NzbDrone/NzbDroneConsole.cs +++ b/NzbDrone/NzbDroneConsole.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Reflection; +using System.Threading; using NLog; using Ninject; using NzbDrone.Model; @@ -14,10 +16,12 @@ namespace NzbDrone private static readonly Logger Logger = LogManager.GetLogger("Host.Main"); - private static void Main(string[] args) + public static void Main(string[] args) { try { + //while (!Debugger.IsAttached) Thread.Sleep(100); + Console.WriteLine("Starting NzbDrone Console. Version " + Assembly.GetExecutingAssembly().GetName().Version); CentralDispatch.ApplicationMode = GetApplicationMode(args); @@ -29,9 +33,6 @@ namespace NzbDrone Console.WriteLine(e.ToString()); Logger.Fatal(e.ToString()); } - - Console.WriteLine("Press enter to exit."); - Console.ReadLine(); } public static ApplicationMode GetApplicationMode(IEnumerable args) diff --git a/NzbDrone/NzbDroneService.cs b/NzbDrone/NzbDroneService.cs index c2d41b092..c7394bd53 100644 --- a/NzbDrone/NzbDroneService.cs +++ b/NzbDrone/NzbDroneService.cs @@ -1,18 +1,43 @@ -using System.ServiceProcess; +using System; +using System.Diagnostics; +using System.ServiceProcess; +using System.Threading; +using NLog; using Ninject; namespace NzbDrone { - internal class NzbDroneService : ServiceBase + public class NzbDroneService : ServiceBase { + + private static readonly Logger Logger = LogManager.GetLogger("Host.CentralDispatch"); + protected override void OnStart(string[] args) { - CentralDispatch.Kernel.Get().Start(); + try + { + while (!Debugger.IsAttached) Thread.Sleep(100); + Debugger.Break(); + CentralDispatch.Kernel.Get().Start(); + } + catch (Exception e) + { + + Logger.Fatal("Failed to start Windows Service", e); + } + } protected override void OnStop() { - CentralDispatch.Kernel.Get().Stop(); + try + { + CentralDispatch.Kernel.Get().Stop(); + } + catch (Exception e) + { + Logger.Fatal("Failed to stop Windows Service", e); + } } } } \ No newline at end of file diff --git a/NzbDrone/Providers/ConsoleProvider.cs b/NzbDrone/Providers/ConsoleProvider.cs index e5b98643d..915f084a3 100644 --- a/NzbDrone/Providers/ConsoleProvider.cs +++ b/NzbDrone/Providers/ConsoleProvider.cs @@ -1,20 +1,24 @@ using System; +using NLog; namespace NzbDrone.Providers { public class ConsoleProvider { + private static readonly Logger Logger = LogManager.GetLogger("Host.ConsoleProvider"); + public virtual void WaitForClose() { while (true) { - System.Console.ReadLine(); + Console.ReadLine(); } } public virtual void PrintHelp() { - System.Console.WriteLine("Help"); + Logger.Info("Printing Help"); + Console.WriteLine("Help"); } } } \ No newline at end of file diff --git a/NzbDrone/Providers/EnviromentProvider.cs b/NzbDrone/Providers/EnviromentProvider.cs index 2659806bc..8e046029b 100644 --- a/NzbDrone/Providers/EnviromentProvider.cs +++ b/NzbDrone/Providers/EnviromentProvider.cs @@ -20,14 +20,22 @@ namespace NzbDrone.Providers { get { - var dir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory; + var dir = new FileInfo(Environment.CurrentDirectory).Directory; while (dir.GetDirectories("iisexpress").Length == 0) { - if (dir.Parent == null) throw new ApplicationException("Can't fine IISExpress folder."); + if (dir.Parent == null) break; dir = dir.Parent; } + dir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory; + + while (dir.GetDirectories("iisexpress").Length == 0) + { + if (dir.Parent == null) throw new ApplicationException("Can't fine IISExpress folder."); + dir = dir.Parent; + } + return dir.FullName; } } diff --git a/NzbDrone/Providers/ServiceProvider.cs b/NzbDrone/Providers/ServiceProvider.cs index 63d54bb2f..240cfa797 100644 --- a/NzbDrone/Providers/ServiceProvider.cs +++ b/NzbDrone/Providers/ServiceProvider.cs @@ -43,6 +43,8 @@ namespace NzbDrone.Providers serviceInstaller.DisplayName = NzbDroneServiceName; serviceInstaller.ServiceName = NzbDroneServiceName; serviceInstaller.StartType = ServiceStartMode.Automatic; + + serviceInstaller.Parent = installer; serviceInstaller.Install(new ListDictionary()); @@ -54,7 +56,7 @@ namespace NzbDrone.Providers { var serviceInstaller = new ServiceInstaller(); - var context = new InstallContext("install.log", null); + var context = new InstallContext("service_uninstall.log", null); serviceInstaller.Context = context; serviceInstaller.ServiceName = NzbDroneServiceName; serviceInstaller.Uninstall(null); diff --git a/NzbDrone/Router.cs b/NzbDrone/Router.cs index bb1065bc5..afda37861 100644 --- a/NzbDrone/Router.cs +++ b/NzbDrone/Router.cs @@ -2,13 +2,16 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using NLog; using NzbDrone.Model; using NzbDrone.Providers; namespace NzbDrone { - class Router + public class Router { + private static readonly Logger Logger = LogManager.GetLogger("Host.Router"); + private readonly ApplicationServer _applicationServer; private readonly ServiceProvider _serviceProvider; private readonly ConsoleProvider _consoleProvider; @@ -17,13 +20,15 @@ namespace NzbDrone { _applicationServer = applicationServer; _serviceProvider = serviceProvider; - _consoleProvider = consoleProvider; + _consoleProvider = consoleProvider; } public void Route() { + Logger.Info("Application mode: {0}", CentralDispatch.ApplicationMode); switch (CentralDispatch.ApplicationMode) { + case ApplicationMode.Console: { _applicationServer.Start();