Windows service seems to be fully working.

pull/6/head
Keivan Beigi 13 years ago
parent 6f3065d5ab
commit e90a3ae9de

@ -1,4 +1,5 @@
using AutoMoq; using System.ServiceProcess;
using AutoMoq;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -47,20 +48,23 @@ namespace NzbDrone.App.Test
var mocker = new AutoMoqer(MockBehavior.Strict); var mocker = new AutoMoqer(MockBehavior.Strict);
var serviceProviderMock = mocker.GetMock<ServiceProvider>(); var serviceProviderMock = mocker.GetMock<ServiceProvider>();
serviceProviderMock.Setup(c => c.Install()); serviceProviderMock.Setup(c => c.Install());
mocker.GetMock<EnviromentProvider>().SetupGet(c => c.IsRunningAsService).Returns(false); serviceProviderMock.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)).Returns(false);
mocker.GetMock<EnviromentProvider>().SetupGet(c => c.IsUserInteractive).Returns(true);
mocker.Resolve<Router>().Route(ApplicationMode.InstallService); mocker.Resolve<Router>().Route(ApplicationMode.InstallService);
serviceProviderMock.Verify(c => c.Install(), Times.Once()); serviceProviderMock.Verify(c => c.Install(), Times.Once());
} }
[Test] [Test]
public void Route_should_call_uninstall_service_when_application_mode_is_uninstall() public void Route_should_call_uninstall_service_when_application_mode_is_uninstall()
{ {
var mocker = new AutoMoqer(MockBehavior.Strict); var mocker = new AutoMoqer(MockBehavior.Strict);
var serviceProviderMock = mocker.GetMock<ServiceProvider>(); var serviceProviderMock = mocker.GetMock<ServiceProvider>();
serviceProviderMock.Setup(c => c.UnInstall()); serviceProviderMock.Setup(c => c.UnInstall());
mocker.GetMock<EnviromentProvider>().SetupGet(c => c.IsRunningAsService).Returns(false); mocker.GetMock<EnviromentProvider>().SetupGet(c => c.IsUserInteractive).Returns(true);
serviceProviderMock.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)).Returns(true);
mocker.Resolve<Router>().Route(ApplicationMode.UninstallService); mocker.Resolve<Router>().Route(ApplicationMode.UninstallService);
@ -75,7 +79,7 @@ namespace NzbDrone.App.Test
var appServerProvider = mocker.GetMock<ApplicationServer>(); var appServerProvider = mocker.GetMock<ApplicationServer>();
consoleProvider.Setup(c => c.WaitForClose()); consoleProvider.Setup(c => c.WaitForClose());
appServerProvider.Setup(c => c.Start()); appServerProvider.Setup(c => c.Start());
mocker.GetMock<EnviromentProvider>().SetupGet(c => c.IsRunningAsService).Returns(false); mocker.GetMock<EnviromentProvider>().SetupGet(c => c.IsUserInteractive).Returns(true);
mocker.Resolve<Router>().Route(ApplicationMode.Console); mocker.Resolve<Router>().Route(ApplicationMode.Console);
@ -91,14 +95,15 @@ namespace NzbDrone.App.Test
{ {
var mocker = new AutoMoqer(MockBehavior.Strict); var mocker = new AutoMoqer(MockBehavior.Strict);
var envMock = mocker.GetMock<EnviromentProvider>(); var envMock = mocker.GetMock<EnviromentProvider>();
var appServerMock = mocker.GetMock<ApplicationServer>(); var serviceProvider = mocker.GetMock<ServiceProvider>();
envMock.SetupGet(c => c.IsRunningAsService).Returns(true); envMock.SetupGet(c => c.IsUserInteractive).Returns(false);
appServerMock.Setup(c => c.StartService());
serviceProvider.Setup(c => c.Run(It.IsAny<ServiceBase>()));
mocker.Resolve<Router>().Route(applicationMode); mocker.Resolve<Router>().Route(applicationMode);
appServerMock.Verify(c => c.StartService(), Times.Once()); serviceProvider.Verify(c => c.Run(It.IsAny<ServiceBase>()), Times.Once());
} }
@ -108,7 +113,7 @@ namespace NzbDrone.App.Test
var mocker = new AutoMoqer(MockBehavior.Strict); var mocker = new AutoMoqer(MockBehavior.Strict);
var consoleMock = mocker.GetMock<ConsoleProvider>(); var consoleMock = mocker.GetMock<ConsoleProvider>();
var serviceMock = mocker.GetMock<ServiceProvider>(); var serviceMock = mocker.GetMock<ServiceProvider>();
mocker.GetMock<EnviromentProvider>().SetupGet(c => c.IsRunningAsService).Returns(false); mocker.GetMock<EnviromentProvider>().SetupGet(c => c.IsUserInteractive).Returns(true);
consoleMock.Setup(c => c.PrintServiceAlreadyExist()); consoleMock.Setup(c => c.PrintServiceAlreadyExist());
serviceMock.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)).Returns(true); serviceMock.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)).Returns(true);
@ -117,5 +122,21 @@ namespace NzbDrone.App.Test
mocker.VerifyAllMocks(); mocker.VerifyAllMocks();
} }
[Test]
public void show_error_on_uninstall_if_service_doesnt_exist()
{
var mocker = new AutoMoqer(MockBehavior.Strict);
var consoleMock = mocker.GetMock<ConsoleProvider>();
var serviceMock = mocker.GetMock<ServiceProvider>();
mocker.GetMock<EnviromentProvider>().SetupGet(c => c.IsUserInteractive).Returns(true);
consoleMock.Setup(c => c.PrintServiceDoestExist());
serviceMock.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)).Returns(false);
mocker.Resolve<Router>().Route(ApplicationMode.UninstallService);
mocker.VerifyAllMocks();
}
} }
} }

@ -40,7 +40,7 @@ namespace NzbDrone.App.Test
var serviceController = new ServiceProvider(); var serviceController = new ServiceProvider();
//Act //Act
serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse(); serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse("Service already installed");
serviceController.Install(); serviceController.Install();
serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeTrue(); serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeTrue();
serviceController.UnInstall(); serviceController.UnInstall();

@ -1,6 +1,6 @@
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true"> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
<targets> <targets>
<target name="consoleTarget" xsi:type="Console" layout="${date} ${logger} - ${message} ${exception}" /> <target name="consoleTarget" xsi:type="Console" layout="${message} ${exception}" />
<target name="udpTarget" xsi:type="Chainsaw" address="udp://127.0.0.1:20480" <target name="udpTarget" xsi:type="Chainsaw" address="udp://127.0.0.1:20480"
includeCallSite="true" includeSourceInfo="true" includeNLogData="true" includeNDC="true" includeMDC="true"> includeCallSite="true" includeSourceInfo="true" includeNLogData="true" includeNDC="true" includeMDC="true">
<parameter name="exception" layout="${exception:format=ToString}" xsi:type="NLogViewerParameterInfo" /> <parameter name="exception" layout="${exception:format=ToString}" xsi:type="NLogViewerParameterInfo" />

@ -1,5 +1,7 @@
using System; using System;
using System.Diagnostics;
using System.Net; using System.Net;
using System.Reflection;
using System.ServiceProcess; using System.ServiceProcess;
using System.Threading; using System.Threading;
using NLog; using NLog;
@ -17,19 +19,13 @@ namespace NzbDrone
private readonly EnviromentProvider _enviromentProvider; private readonly EnviromentProvider _enviromentProvider;
private readonly IISProvider _iisProvider; private readonly IISProvider _iisProvider;
private readonly ProcessProvider _processProvider; private readonly ProcessProvider _processProvider;
private readonly MonitoringProvider _monitoringProvider;
private readonly WebClient _webClient; private readonly WebClient _webClient;
public void IsRunningAsService()
{
Logger.Warn(base.Container);
Logger.Warn(base.ServiceName);
}
[Inject] [Inject]
public ApplicationServer(ConfigProvider configProvider, WebClient webClient, IISProvider iisProvider, public ApplicationServer(ConfigProvider configProvider, WebClient webClient, IISProvider iisProvider,
DebuggerProvider debuggerProvider, EnviromentProvider enviromentProvider, DebuggerProvider debuggerProvider, EnviromentProvider enviromentProvider,
ProcessProvider processProvider) ProcessProvider processProvider, MonitoringProvider monitoringProvider)
{ {
_configProvider = configProvider; _configProvider = configProvider;
_webClient = webClient; _webClient = webClient;
@ -37,6 +33,7 @@ namespace NzbDrone
_debuggerProvider = debuggerProvider; _debuggerProvider = debuggerProvider;
_enviromentProvider = enviromentProvider; _enviromentProvider = enviromentProvider;
_processProvider = processProvider; _processProvider = processProvider;
_monitoringProvider = monitoringProvider;
} }
public ApplicationServer() public ApplicationServer()
@ -44,10 +41,9 @@ namespace NzbDrone
} }
public virtual void StartService() protected override void OnStart(string[] args)
{ {
Start(); Start();
Run(this);
} }
public virtual void Start() public virtual void Start()
@ -80,14 +76,11 @@ namespace NzbDrone
Logger.ErrorException("Failed to load home page.", e); Logger.ErrorException("Failed to load home page.", e);
} }
} }
}
protected override void OnStop() _monitoringProvider.Start();
{
StopServer();
} }
public void StopServer() protected override void OnStop()
{ {
Logger.Info("Attempting to stop application."); Logger.Info("Attempting to stop application.");
_iisProvider.StopServer(); _iisProvider.StopServer();

@ -50,7 +50,7 @@ namespace NzbDrone
{ {
_kernel.Get<ConfigProvider>().ConfigureNlog(); _kernel.Get<ConfigProvider>().ConfigureNlog();
_kernel.Get<ConfigProvider>().CreateDefaultConfigFile(); _kernel.Get<ConfigProvider>().CreateDefaultConfigFile();
Logger.Info("Starting NZBDrone. Start-up Path:'{0}'", _kernel.Get<EnviromentProvider>().ApplicationPath); Logger.Info("Start-up Path:'{0}'", _kernel.Get<EnviromentProvider>().ApplicationPath);
Thread.CurrentThread.Name = "Host"; Thread.CurrentThread.Name = "Host";
} }
} }

@ -1,12 +1,12 @@
using System; using System;
using System.Diagnostics;
using System.Reflection;
using NLog; using NLog;
namespace NzbDrone.Providers namespace NzbDrone.Providers
{ {
public class ConsoleProvider public class ConsoleProvider
{ {
private static readonly Logger Logger = LogManager.GetLogger("Host.ConsoleProvider");
public virtual void WaitForClose() public virtual void WaitForClose()
{ {
while (true) while (true)
@ -17,13 +17,22 @@ namespace NzbDrone.Providers
public virtual void PrintHelp() public virtual void PrintHelp()
{ {
Logger.Info("Printing Help"); Console.WriteLine();
Console.WriteLine("Help"); Console.WriteLine(" Usage: {0} <command> ", Process.GetCurrentProcess().MainModule.ModuleName);
Console.WriteLine(" Commands:");
Console.WriteLine(" /i Install the application as a Windows Service ({0}).", ServiceProvider.NzbDroneServiceName);
Console.WriteLine(" /u Uninstall already installed Windows Service ({0}).", ServiceProvider.NzbDroneServiceName);
Console.WriteLine(" <No Arguments> Run application in console mode.");
} }
public virtual void PrintServiceAlreadyExist() public virtual void PrintServiceAlreadyExist()
{ {
Console.WriteLine("A service with the same name ({0}) already exists. Aborting installation", ServiceProvider.NzbDroneServiceName); Console.WriteLine("A service with the same name ({0}) already exists. Aborting installation", ServiceProvider.NzbDroneServiceName);
} }
public virtual void PrintServiceDoestExist()
{
Console.WriteLine("Can't find service ({0})", ServiceProvider.NzbDroneServiceName);
}
} }
} }

@ -16,37 +16,23 @@ namespace NzbDrone.Providers
get { return Environment.UserInteractive; } get { return Environment.UserInteractive; }
} }
public virtual bool IsRunningAsService
{
get
{
try
{
Console.Write("");
return false;
}
catch (Exception)
{
return true;
}
}
}
public virtual string ApplicationPath public virtual string ApplicationPath
{ {
get get
{ {
var dir = new FileInfo(Environment.CurrentDirectory).Directory; var dir = new FileInfo(Environment.CurrentDirectory).Directory;
while (dir.GetDirectories("iisexpress").Length == 0) while (!ContainsIIS(dir))
{ {
if (dir.Parent == null) break; if (dir.Parent == null) break;
dir = dir.Parent; dir = dir.Parent;
} }
if (ContainsIIS(dir)) return dir.FullName;
dir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory; dir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;
while (dir.GetDirectories("iisexpress").Length == 0) while (!ContainsIIS(dir))
{ {
if (dir.Parent == null) throw new ApplicationException("Can't fine IISExpress folder."); if (dir.Parent == null) throw new ApplicationException("Can't fine IISExpress folder.");
dir = dir.Parent; dir = dir.Parent;
@ -55,5 +41,10 @@ namespace NzbDrone.Providers
return dir.FullName; return dir.FullName;
} }
} }
private static bool ContainsIIS(DirectoryInfo dir)
{
return dir.GetDirectories("iisexpress").Length != 0;
}
} }
} }

@ -15,7 +15,7 @@ namespace NzbDrone.Providers
private static readonly Logger Logger = LogManager.GetLogger("Host.ServiceManager"); private static readonly Logger Logger = LogManager.GetLogger("Host.ServiceManager");
public virtual bool ServiceExist(string name) public virtual bool ServiceExist(string name)
{ {
@ -32,7 +32,7 @@ namespace NzbDrone.Providers
var installer = new ServiceProcessInstaller var installer = new ServiceProcessInstaller
{ {
Account = ServiceAccount.NetworkService Account = ServiceAccount.LocalSystem
}; };
var serviceInstaller = new ServiceInstaller(); var serviceInstaller = new ServiceInstaller();
@ -44,9 +44,9 @@ namespace NzbDrone.Providers
serviceInstaller.Context = context; serviceInstaller.Context = context;
serviceInstaller.DisplayName = NzbDroneServiceName; serviceInstaller.DisplayName = NzbDroneServiceName;
serviceInstaller.ServiceName = NzbDroneServiceName; serviceInstaller.ServiceName = NzbDroneServiceName;
serviceInstaller.Description = "NzbDrone Application Server";
serviceInstaller.StartType = ServiceStartMode.Automatic; serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.Parent = installer; serviceInstaller.Parent = installer;
serviceInstaller.Install(new ListDictionary()); serviceInstaller.Install(new ListDictionary());
@ -63,5 +63,11 @@ namespace NzbDrone.Providers
serviceInstaller.ServiceName = NzbDroneServiceName; serviceInstaller.ServiceName = NzbDroneServiceName;
serviceInstaller.Uninstall(null); serviceInstaller.Uninstall(null);
} }
public virtual void Run(ServiceBase service)
{
ServiceBase.Run(service);
}
} }
} }

@ -36,18 +36,9 @@ namespace NzbDrone
{ {
Logger.Info("Application mode: {0}", applicationMode); Logger.Info("Application mode: {0}", applicationMode);
_applicationServer.IsRunningAsService(); if (!_enviromentProvider.IsUserInteractive)
while (!Debugger.IsAttached)
{
Thread.Sleep(1000);
}
if (_enviromentProvider.IsRunningAsService)
{ {
_applicationServer.StartService(); _serviceProvider.Run(_applicationServer);
} }
else else
{ {
@ -74,7 +65,15 @@ namespace NzbDrone
} }
case ApplicationMode.UninstallService: case ApplicationMode.UninstallService:
{ {
_serviceProvider.UnInstall(); if (!_serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName))
{
_consoleProvider.PrintServiceDoestExist();
}
else
{
_serviceProvider.UnInstall();
}
break; break;
} }
default: default:

Loading…
Cancel
Save