Windows service seems to be fully working.

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

@ -1,4 +1,5 @@
using AutoMoq;
using System.ServiceProcess;
using AutoMoq;
using FluentAssertions;
using Moq;
using NUnit.Framework;
@ -47,20 +48,23 @@ namespace NzbDrone.App.Test
var mocker = new AutoMoqer(MockBehavior.Strict);
var serviceProviderMock = mocker.GetMock<ServiceProvider>();
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);
serviceProviderMock.Verify(c => c.Install(), Times.Once());
}
[Test]
public void Route_should_call_uninstall_service_when_application_mode_is_uninstall()
{
var mocker = new AutoMoqer(MockBehavior.Strict);
var serviceProviderMock = mocker.GetMock<ServiceProvider>();
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);
@ -75,7 +79,7 @@ namespace NzbDrone.App.Test
var appServerProvider = mocker.GetMock<ApplicationServer>();
consoleProvider.Setup(c => c.WaitForClose());
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);
@ -91,14 +95,15 @@ namespace NzbDrone.App.Test
{
var mocker = new AutoMoqer(MockBehavior.Strict);
var envMock = mocker.GetMock<EnviromentProvider>();
var appServerMock = mocker.GetMock<ApplicationServer>();
var serviceProvider = mocker.GetMock<ServiceProvider>();
envMock.SetupGet(c => c.IsUserInteractive).Returns(false);
envMock.SetupGet(c => c.IsRunningAsService).Returns(true);
appServerMock.Setup(c => c.StartService());
serviceProvider.Setup(c => c.Run(It.IsAny<ServiceBase>()));
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 consoleMock = mocker.GetMock<ConsoleProvider>();
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());
serviceMock.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)).Returns(true);
@ -117,5 +122,21 @@ namespace NzbDrone.App.Test
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();
//Act
serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse();
serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeFalse("Service already installed");
serviceController.Install();
serviceController.ServiceExist(ServiceProvider.NzbDroneServiceName).Should().BeTrue();
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">
<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"
includeCallSite="true" includeSourceInfo="true" includeNLogData="true" includeNDC="true" includeMDC="true">
<parameter name="exception" layout="${exception:format=ToString}" xsi:type="NLogViewerParameterInfo" />

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

@ -50,7 +50,7 @@ namespace NzbDrone
{
_kernel.Get<ConfigProvider>().ConfigureNlog();
_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";
}
}

@ -1,12 +1,12 @@
using System;
using System.Diagnostics;
using System.Reflection;
using NLog;
namespace NzbDrone.Providers
{
public class ConsoleProvider
{
private static readonly Logger Logger = LogManager.GetLogger("Host.ConsoleProvider");
public virtual void WaitForClose()
{
while (true)
@ -17,13 +17,22 @@ namespace NzbDrone.Providers
public virtual void PrintHelp()
{
Logger.Info("Printing Help");
Console.WriteLine("Help");
Console.WriteLine();
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()
{
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; }
}
public virtual bool IsRunningAsService
{
get
{
try
{
Console.Write("");
return false;
}
catch (Exception)
{
return true;
}
}
}
public virtual string ApplicationPath
{
get
{
var dir = new FileInfo(Environment.CurrentDirectory).Directory;
while (dir.GetDirectories("iisexpress").Length == 0)
while (!ContainsIIS(dir))
{
if (dir.Parent == null) break;
dir = dir.Parent;
}
if (ContainsIIS(dir)) return dir.FullName;
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.");
dir = dir.Parent;
@ -55,5 +41,10 @@ namespace NzbDrone.Providers
return dir.FullName;
}
}
private static bool ContainsIIS(DirectoryInfo dir)
{
return dir.GetDirectories("iisexpress").Length != 0;
}
}
}

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

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

Loading…
Cancel
Save