More work on WindowsService, still broken

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

@ -77,7 +77,7 @@
<Compile Include="AutoMoq\Unity\AutoMockingBuilderStrategy.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingContainerExtension.cs" />
<Compile Include="CentralDispatchTests.cs" />
<Compile Include="ProgramTest.cs" />
<Compile Include="RouterTest.cs" />
<Compile Include="MonitoringProviderTest.cs" />
<Compile Include="ConfigProviderTest.cs" />
<Compile Include="IISProviderTest.cs" />

@ -1,43 +0,0 @@
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Model;
using NzbDrone.Providers;
namespace NzbDrone.App.Test
{
[TestFixture]
public class ProgramTest
{
[TestCase(null, ApplicationMode.Console)]
[TestCase("", ApplicationMode.Console)]
[TestCase("1", ApplicationMode.Help)]
[TestCase("ii", ApplicationMode.Help)]
[TestCase("uu", ApplicationMode.Help)]
[TestCase("i", ApplicationMode.InstallService)]
[TestCase("I", ApplicationMode.InstallService)]
[TestCase("/I", ApplicationMode.InstallService)]
[TestCase("/i", ApplicationMode.InstallService)]
[TestCase("-I", ApplicationMode.InstallService)]
[TestCase("-i", ApplicationMode.InstallService)]
[TestCase("u", ApplicationMode.UninstallService)]
[TestCase("U", ApplicationMode.UninstallService)]
[TestCase("/U", ApplicationMode.UninstallService)]
[TestCase("/u", ApplicationMode.UninstallService)]
[TestCase("-U", ApplicationMode.UninstallService)]
[TestCase("-u", ApplicationMode.UninstallService)]
public void GetApplicationMode_single_arg(string arg, ApplicationMode mode)
{
NzbDroneConsole.GetApplicationMode(new[] { arg }).Should().Be(mode);
}
[TestCase("", "", ApplicationMode.Console)]
[TestCase("", null, ApplicationMode.Console)]
[TestCase("i", "n", ApplicationMode.Help)]
public void GetApplicationMode_two_args(string a, string b, ApplicationMode mode)
{
NzbDroneConsole.GetApplicationMode(new[] { a, b }).Should().Be(mode);
}
}
}

@ -0,0 +1,121 @@
using AutoMoq;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Model;
using NzbDrone.Providers;
namespace NzbDrone.App.Test
{
[TestFixture]
public class RouterTest
{
[TestCase(null, ApplicationMode.Console)]
[TestCase("", ApplicationMode.Console)]
[TestCase("1", ApplicationMode.Help)]
[TestCase("ii", ApplicationMode.Help)]
[TestCase("uu", ApplicationMode.Help)]
[TestCase("i", ApplicationMode.InstallService)]
[TestCase("I", ApplicationMode.InstallService)]
[TestCase("/I", ApplicationMode.InstallService)]
[TestCase("/i", ApplicationMode.InstallService)]
[TestCase("-I", ApplicationMode.InstallService)]
[TestCase("-i", ApplicationMode.InstallService)]
[TestCase("u", ApplicationMode.UninstallService)]
[TestCase("U", ApplicationMode.UninstallService)]
[TestCase("/U", ApplicationMode.UninstallService)]
[TestCase("/u", ApplicationMode.UninstallService)]
[TestCase("-U", ApplicationMode.UninstallService)]
[TestCase("-u", ApplicationMode.UninstallService)]
public void GetApplicationMode_single_arg(string arg, ApplicationMode mode)
{
Router.GetApplicationMode(new[] { arg }).Should().Be(mode);
}
[TestCase("", "", ApplicationMode.Console)]
[TestCase("", null, ApplicationMode.Console)]
[TestCase("i", "n", ApplicationMode.Help)]
public void GetApplicationMode_two_args(string a, string b, ApplicationMode mode)
{
Router.GetApplicationMode(new[] { a, b }).Should().Be(mode);
}
[Test]
public void Route_should_call_install_service_when_application_mode_is_install()
{
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);
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.Resolve<Router>().Route(ApplicationMode.UninstallService);
serviceProviderMock.Verify(c => c.UnInstall(), Times.Once());
}
[Test]
public void Route_should_call_console_service_when_application_mode_is_console()
{
var mocker = new AutoMoqer(MockBehavior.Strict);
var consoleProvider = mocker.GetMock<ConsoleProvider>();
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.Resolve<Router>().Route(ApplicationMode.Console);
consoleProvider.Verify(c => c.WaitForClose(), Times.Once());
appServerProvider.Verify(c => c.Start(), Times.Once());
}
[TestCase(ApplicationMode.Console)]
[TestCase(ApplicationMode.InstallService)]
[TestCase(ApplicationMode.UninstallService)]
[TestCase(ApplicationMode.Help)]
public void Route_should_call_service_start_when_run_in_service_more(ApplicationMode applicationMode)
{
var mocker = new AutoMoqer(MockBehavior.Strict);
var envMock = mocker.GetMock<EnviromentProvider>();
var appServerMock = mocker.GetMock<ApplicationServer>();
envMock.SetupGet(c => c.IsRunningAsService).Returns(true);
appServerMock.Setup(c => c.StartService());
mocker.Resolve<Router>().Route(applicationMode);
appServerMock.Verify(c => c.StartService(), Times.Once());
}
[Test]
public void show_error_on_install_if_service_already_exist()
{
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);
consoleMock.Setup(c => c.PrintServiceAlreadyExist());
serviceMock.Setup(c => c.ServiceExist(ServiceProvider.NzbDroneServiceName)).Returns(true);
mocker.Resolve<Router>().Route(ApplicationMode.InstallService);
mocker.VerifyAllMocks();
}
}
}

@ -0,0 +1,36 @@
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;
namespace NzbDrone
{
public static class AppMain
{
private static readonly Logger Logger = LogManager.GetLogger("Host.Main");
public static void Main(string[] args)
{
try
{
Console.WriteLine("Starting NzbDrone Console. Version " + Assembly.GetExecutingAssembly().GetName().Version);
CentralDispatch.Kernel.Get<Router>().Route(args);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Logger.Fatal(e.ToString());
}
}
}
}

@ -1,5 +1,6 @@
using System;
using System.Net;
using System.ServiceProcess;
using System.Threading;
using NLog;
using Ninject;
@ -7,7 +8,7 @@ using NzbDrone.Providers;
namespace NzbDrone
{
public class ApplicationServer
public class ApplicationServer : ServiceBase
{
private static readonly Logger Logger = LogManager.GetLogger("Host.App");
@ -18,6 +19,13 @@ namespace NzbDrone
private readonly ProcessProvider _processProvider;
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,
@ -33,6 +41,13 @@ namespace NzbDrone
public ApplicationServer()
{
}
public virtual void StartService()
{
Start();
Run(this);
}
public virtual void Start()
@ -67,7 +82,12 @@ namespace NzbDrone
}
}
public virtual void Stop()
protected override void OnStop()
{
StopServer();
}
public void StopServer()
{
Logger.Info("Attempting to stop application.");
_iisProvider.StopServer();

@ -22,8 +22,6 @@ namespace NzbDrone
InitilizeApp();
}
public static ApplicationMode ApplicationMode { get; set; }
public static StandardKernel Kernel
{
get

@ -54,7 +54,7 @@
<ApplicationIcon>NzbDrone.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<StartupObject>NzbDrone.NzbDroneConsole</StartupObject>
<StartupObject>NzbDrone.AppMain</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="Accessibility">
@ -88,7 +88,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ApplicationServer.cs" />
<Compile Include="ApplicationServer.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="CentralDispatch.cs" />
<Compile Include="Model\ApplicationMode.cs" />
<Compile Include="Model\AuthenticationType.cs" />
@ -96,13 +98,10 @@
<Compile Include="Providers\ConsoleProvider.cs" />
<Compile Include="Providers\DebuggerProvider.cs" />
<Compile Include="Providers\EnviromentProvider.cs" />
<Compile Include="NzbDroneService.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ProcessAttacher.cs" />
<Compile Include="Providers\ConfigProvider.cs" />
<Compile Include="Providers\IISProvider.cs" />
<Compile Include="NzbDroneConsole.cs" />
<Compile Include="AppMain.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\MonitoringProvider.cs" />
<Compile Include="Providers\ProcessProvider.cs" />

@ -1,54 +0,0 @@
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;
namespace NzbDrone
{
public static class NzbDroneConsole
{
private static readonly Logger Logger = LogManager.GetLogger("Host.Main");
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);
CentralDispatch.Kernel.Get<Router>().Route();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Logger.Fatal(e.ToString());
}
}
public static ApplicationMode GetApplicationMode(IEnumerable<string> args)
{
if (args == null) return ApplicationMode.Console;
var cleanArgs = args.Where(c => c != null && !String.IsNullOrWhiteSpace(c)).ToList();
if (cleanArgs.Count == 0) return ApplicationMode.Console;
if (cleanArgs.Count != 1) return ApplicationMode.Help;
var arg = cleanArgs.First().Trim('/', '\\', '-').ToLower();
if (arg == "i") return ApplicationMode.InstallService;
if (arg == "u") return ApplicationMode.UninstallService;
return ApplicationMode.Help;
}
}
}

@ -1,43 +0,0 @@
using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using NLog;
using Ninject;
namespace NzbDrone
{
public class NzbDroneService : ServiceBase
{
private static readonly Logger Logger = LogManager.GetLogger("Host.CentralDispatch");
protected override void OnStart(string[] args)
{
try
{
while (!Debugger.IsAttached) Thread.Sleep(100);
Debugger.Break();
CentralDispatch.Kernel.Get<ApplicationServer>().Start();
}
catch (Exception e)
{
Logger.Fatal("Failed to start Windows Service", e);
}
}
protected override void OnStop()
{
try
{
CentralDispatch.Kernel.Get<ApplicationServer>().Stop();
}
catch (Exception e)
{
Logger.Fatal("Failed to stop Windows Service", e);
}
}
}
}

@ -20,5 +20,10 @@ namespace NzbDrone.Providers
Logger.Info("Printing Help");
Console.WriteLine("Help");
}
public virtual void PrintServiceAlreadyExist()
{
Console.WriteLine("A service with the same name ({0}) already exists. Aborting installation", ServiceProvider.NzbDroneServiceName);
}
}
}

@ -16,6 +16,22 @@ 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
@ -29,13 +45,13 @@ namespace NzbDrone.Providers
}
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;
}
}

@ -15,7 +15,9 @@ namespace NzbDrone.Providers
private static readonly Logger Logger = LogManager.GetLogger("Host.ServiceManager");
public bool ServiceExist(string name)
public virtual bool ServiceExist(string name)
{
return
ServiceController.GetServices().Any(
@ -36,15 +38,15 @@ namespace NzbDrone.Providers
var serviceInstaller = new ServiceInstaller();
String[] cmdline = {@"/assemblypath=" + Assembly.GetExecutingAssembly().Location};
String[] cmdline = { @"/assemblypath=" + Assembly.GetExecutingAssembly().Location };
var context = new InstallContext("service_install.log", cmdline);
serviceInstaller.Context = context;
serviceInstaller.DisplayName = NzbDroneServiceName;
serviceInstaller.ServiceName = NzbDroneServiceName;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.Parent = installer;
serviceInstaller.Install(new ListDictionary());

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using NLog;
using NzbDrone.Model;
using NzbDrone.Providers;
@ -15,42 +17,89 @@ namespace NzbDrone
private readonly ApplicationServer _applicationServer;
private readonly ServiceProvider _serviceProvider;
private readonly ConsoleProvider _consoleProvider;
private readonly EnviromentProvider _enviromentProvider;
public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider, ConsoleProvider consoleProvider)
public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider, ConsoleProvider consoleProvider, EnviromentProvider enviromentProvider)
{
_applicationServer = applicationServer;
_serviceProvider = serviceProvider;
_consoleProvider = consoleProvider;
_enviromentProvider = enviromentProvider;
}
public void Route()
public void Route(IEnumerable<string> args)
{
Logger.Info("Application mode: {0}", CentralDispatch.ApplicationMode);
switch (CentralDispatch.ApplicationMode)
Route(GetApplicationMode(args));
}
public void Route(ApplicationMode applicationMode)
{
Logger.Info("Application mode: {0}", applicationMode);
_applicationServer.IsRunningAsService();
while (!Debugger.IsAttached)
{
Thread.Sleep(1000);
}
if (_enviromentProvider.IsRunningAsService)
{
_applicationServer.StartService();
case ApplicationMode.Console:
{
_applicationServer.Start();
_consoleProvider.WaitForClose();
break;
}
case ApplicationMode.InstallService:
{
_serviceProvider.Install();
break;
}
case ApplicationMode.UninstallService:
{
_serviceProvider.UnInstall();
break;
}
default:
{
_consoleProvider.PrintHelp();
break;
}
}
else
{
switch (applicationMode)
{
case ApplicationMode.Console:
{
_applicationServer.Start();
_consoleProvider.WaitForClose();
break;
}
case ApplicationMode.InstallService:
{
if (_serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName))
{
_consoleProvider.PrintServiceAlreadyExist();
}
else
{
_serviceProvider.Install();
}
break;
}
case ApplicationMode.UninstallService:
{
_serviceProvider.UnInstall();
break;
}
default:
{
_consoleProvider.PrintHelp();
break;
}
}
}
}
public static ApplicationMode GetApplicationMode(IEnumerable<string> args)
{
if (args == null) return ApplicationMode.Console;
var cleanArgs = args.Where(c => c != null && !String.IsNullOrWhiteSpace(c)).ToList();
if (cleanArgs.Count == 0) return ApplicationMode.Console;
if (cleanArgs.Count != 1) return ApplicationMode.Help;
var arg = cleanArgs.First().Trim('/', '\\', '-').ToLower();
if (arg == "i") return ApplicationMode.InstallService;
if (arg == "u") return ApplicationMode.UninstallService;
return ApplicationMode.Help;
}
}
}

Loading…
Cancel
Save