1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-12 11:15:43 +02:00

Service (work in progress)

This commit is contained in:
kay.one 2011-10-11 00:11:05 -07:00
parent 012fa88301
commit ba11b34099
10 changed files with 166 additions and 87 deletions

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using FluentAssertions;
using NUnit.Framework;
using Ninject;
using NzbDrone.Providers;
namespace NzbDrone.App.Test
{
[TestFixture]
public class CentralDispatchTests
{
[Test]
public void Kernel_can_get_kernel()
{
CentralDispatch.Kernel.Should().NotBeNull();
}
[Test]
public void Kernel_should_return_same_kernel()
{
var firstKernel = CentralDispatch.Kernel;
var secondKernel = CentralDispatch.Kernel;
firstKernel.Should().BeSameAs(secondKernel);
}
[Test]
public void Kernel_should_be_able_to_resolve_ApplicationServer()
{
var appServer = CentralDispatch.Kernel.Get<ApplicationServer>();
appServer.Should().NotBeNull();
}
}
}

View File

@ -49,6 +49,7 @@
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
<Reference Include="Ninject, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL" />
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.5.10.11092\lib\nunit.framework.dll</HintPath>
</Reference>
@ -72,6 +73,7 @@
<Compile Include="AutoMoq\AutoMoqerTest.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingBuilderStrategy.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingContainerExtension.cs" />
<Compile Include="CentralDispatchTests.cs" />
<Compile Include="ProgramTest.cs" />
<Compile Include="MonitoringProviderTest.cs" />
<Compile Include="ConfigProviderTest.cs" />

View File

@ -29,7 +29,7 @@ public class ProgramTest
[TestCase("-u", ApplicationMode.UninstallService)]
public void GetApplicationMode_single_arg(string arg, ApplicationMode mode)
{
Console.GetApplicationMode(new[] { arg }).Should().Be(mode);
NzbDroneConsole.GetApplicationMode(new[] { arg }).Should().Be(mode);
}
[TestCase("", "", ApplicationMode.Console)]
@ -37,7 +37,7 @@ public void GetApplicationMode_single_arg(string arg, ApplicationMode mode)
[TestCase("i", "n", ApplicationMode.Help)]
public void GetApplicationMode_two_args(string a, string b, ApplicationMode mode)
{
Console.GetApplicationMode(new[] { a, b }).Should().Be(mode);
NzbDroneConsole.GetApplicationMode(new[] { a, b }).Should().Be(mode);
}
}
}

View File

@ -7,7 +7,7 @@
namespace NzbDrone
{
public class Application
public class ApplicationServer
{
private static readonly Logger Logger = LogManager.GetLogger("Host.App");
@ -19,7 +19,7 @@ public class Application
private readonly WebClient _webClient;
[Inject]
public Application(ConfigProvider configProvider, WebClient webClient, IISProvider iisProvider,
public ApplicationServer(ConfigProvider configProvider, WebClient webClient, IISProvider iisProvider,
DebuggerProvider debuggerProvider, EnviromentProvider enviromentProvider,
ProcessProvider processProvider)
{
@ -29,14 +29,9 @@ public Application(ConfigProvider configProvider, WebClient webClient, IISProvid
_debuggerProvider = debuggerProvider;
_enviromentProvider = enviromentProvider;
_processProvider = processProvider;
_configProvider.ConfigureNlog();
_configProvider.CreateDefaultConfigFile();
Logger.Info("Starting NZBDrone. Start-up Path:'{0}'", _enviromentProvider.ApplicationPath);
Thread.CurrentThread.Name = "Host";
}
public Application()
public ApplicationServer()
{
}

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Ninject;
using NzbDrone.Model;
using NzbDrone.Providers;
namespace NzbDrone
{
public static class CentralDispatch
{
private static StandardKernel _kernel;
static CentralDispatch()
{
_kernel = new StandardKernel();
}
public static ApplicationMode ApplicationMode { get; set; }
public static StandardKernel Kernel
{
get
{
return _kernel;
}
}
private static void BindKernel()
{
_kernel = new StandardKernel();
_kernel.Bind<ConfigProvider>().ToSelf().InSingletonScope();
_kernel.Bind<ConsoleProvider>().ToSelf().InSingletonScope();
_kernel.Bind<DebuggerProvider>().ToSelf().InSingletonScope();
_kernel.Bind<EnviromentProvider>().ToSelf().InSingletonScope();
_kernel.Bind<IISProvider>().ToSelf().InSingletonScope();
_kernel.Bind<MonitoringProvider>().ToSelf().InSingletonScope();
_kernel.Bind<ProcessProvider>().ToSelf().InSingletonScope();
_kernel.Bind<ServiceProvider>().ToSelf().InSingletonScope();
_kernel.Bind<WebClientProvider>().ToSelf().InSingletonScope();
}
private static void InitilizeApp()
{
_kernel.Get<ConfigProvider>().ConfigureNlog();
_kernel.Get<ConfigProvider>().CreateDefaultConfigFile();
Logger.Info("Starting NZBDrone. Start-up Path:'{0}'", _kernel.Get<EnviromentProvider>().ApplicationPath);
Thread.CurrentThread.Name = "Host";
}
}
}

View File

@ -1,63 +0,0 @@
using System;
using System.Linq;
using System.Reflection;
using NLog;
using Ninject;
using NzbDrone.Model;
using NzbDrone.Providers;
namespace NzbDrone
{
public static class Console
{
private static readonly StandardKernel Kernel = new StandardKernel();
private static readonly Logger Logger = LogManager.GetLogger("Host.Main");
private static void Main(string[] args)
{
try
{
System.Console.WriteLine("Starting NzbDrone Console. Version " + Assembly.GetExecutingAssembly().GetName().Version);
Kernel.Bind<ConfigProvider>().ToSelf().InSingletonScope();
Kernel.Bind<ConsoleProvider>().ToSelf().InSingletonScope();
Kernel.Bind<DebuggerProvider>().ToSelf().InSingletonScope();
Kernel.Bind<EnviromentProvider>().ToSelf().InSingletonScope();
Kernel.Bind<IISProvider>().ToSelf().InSingletonScope();
Kernel.Bind<MonitoringProvider>().ToSelf().InSingletonScope();
Kernel.Bind<ProcessProvider>().ToSelf().InSingletonScope();
Kernel.Bind<ServiceProvider>().ToSelf().InSingletonScope();
Kernel.Bind<WebClientProvider>().ToSelf().InSingletonScope();
Kernel.Bind<ApplicationMode>().ToConstant(GetApplicationMode(args));
Kernel.Get<Router>().Route();
}
catch (Exception e)
{
System.Console.WriteLine(e.ToString());
Logger.Fatal(e.ToString());
}
System.Console.WriteLine("Press enter to exit.");
System.Console.ReadLine();
}
public static ApplicationMode GetApplicationMode(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;
}
}
}

View File

@ -85,7 +85,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Application.cs" />
<Compile Include="ApplicationServer.cs" />
<Compile Include="CentralDispatch.cs" />
<Compile Include="Model\ApplicationMode.cs" />
<Compile Include="Model\AuthenticationType.cs" />
<Compile Include="Model\ProcessInfo.cs" />
@ -98,7 +99,7 @@
<Compile Include="ProcessAttacher.cs" />
<Compile Include="Providers\ConfigProvider.cs" />
<Compile Include="Providers\IISProvider.cs" />
<Compile Include="Console.cs" />
<Compile Include="NzbDroneConsole.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\MonitoringProvider.cs" />
<Compile Include="Providers\ProcessProvider.cs" />

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NLog;
using Ninject;
using NzbDrone.Model;
namespace NzbDrone
{
public static class NzbDroneConsole
{
private static readonly Logger Logger = LogManager.GetLogger("Host.Main");
private static void Main(string[] args)
{
try
{
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());
}
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
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;
}
}
}

View File

@ -1,4 +1,5 @@
using System.ServiceProcess;
using Ninject;
namespace NzbDrone
{
@ -6,12 +7,12 @@ internal class NzbDroneService : ServiceBase
{
protected override void OnStart(string[] args)
{
base.OnStart(args);
CentralDispatch.Kernel.Get<ApplicationServer>().Start();
}
protected override void OnStop()
{
base.OnStop();
}
CentralDispatch.Kernel.Get<ApplicationServer>().Stop();
}
}
}

View File

@ -9,27 +9,24 @@ namespace NzbDrone
{
class Router
{
private readonly Application _application;
private readonly ApplicationServer _applicationServer;
private readonly ServiceProvider _serviceProvider;
private readonly ConsoleProvider _consoleProvider;
private readonly ApplicationMode _applicationMode;
public Router(Application application, ServiceProvider serviceProvider, ConsoleProvider consoleProvider, ApplicationMode applicationMode)
public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider, ConsoleProvider consoleProvider)
{
_application = application;
_applicationServer = applicationServer;
_serviceProvider = serviceProvider;
_consoleProvider = consoleProvider;
_applicationMode = applicationMode;
_consoleProvider = consoleProvider;
}
public void Route()
{
switch (_applicationMode)
switch (CentralDispatch.ApplicationMode)
{
case ApplicationMode.Console:
{
_application.Start();
_applicationServer.Start();
_consoleProvider.WaitForClose();
break;
}