CentralDispatch is no longer static.

This commit is contained in:
kay.one 2011-11-08 12:12:54 -08:00
parent ae9e941b30
commit ca7deedfb9
6 changed files with 82 additions and 86 deletions

View File

@ -1,11 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using FluentAssertions; using FluentAssertions;
using NLog;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.Instrumentation;
using NzbDrone.Core.Providers; using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Indexer; using NzbDrone.Core.Providers.Indexer;
using NzbDrone.Core.Providers.Jobs; using NzbDrone.Core.Providers.Jobs;
@ -21,10 +18,18 @@ namespace NzbDrone.Core.Test
readonly IList<Type> indexers = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(IndexerBase))).ToList(); readonly IList<Type> indexers = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(IndexerBase))).ToList();
readonly IList<Type> jobs = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IJob))).ToList(); readonly IList<Type> jobs = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IJob))).ToList();
private CentralDispatch centralDispatch;
[SetUp]
public void Setup()
{
centralDispatch = new CentralDispatch();
}
[Test] [Test]
public void InitAppTest() public void InitAppTest()
{ {
CentralDispatch.NinjectKernel.Should().NotBeNull(); centralDispatch.Kernel.Should().NotBeNull();
} }
[Test] [Test]
@ -37,7 +42,7 @@ namespace NzbDrone.Core.Test
foreach (var provider in providers) foreach (var provider in providers)
{ {
Console.WriteLine("Resolving " + provider.Name); Console.WriteLine("Resolving " + provider.Name);
CentralDispatch.NinjectKernel.Get(provider).Should().NotBeNull(); centralDispatch.Kernel.Get(provider).Should().NotBeNull();
} }
} }
@ -47,7 +52,7 @@ namespace NzbDrone.Core.Test
{ {
//Assert //Assert
var registeredJobs = CentralDispatch.NinjectKernel.GetAll<IJob>(); var registeredJobs = centralDispatch.Kernel.GetAll<IJob>();
jobs.Should().NotBeEmpty(); jobs.Should().NotBeEmpty();
@ -60,7 +65,7 @@ namespace NzbDrone.Core.Test
{ {
//Assert //Assert
var registeredIndexers = CentralDispatch.NinjectKernel.GetAll<IndexerBase>(); var registeredIndexers = centralDispatch.Kernel.GetAll<IndexerBase>();
indexers.Should().NotBeEmpty(); indexers.Should().NotBeEmpty();
@ -71,26 +76,26 @@ namespace NzbDrone.Core.Test
[Test] [Test]
public void jobs_are_initialized() public void jobs_are_initialized()
{ {
CentralDispatch.NinjectKernel.Get<JobProvider>().All().Should().HaveSameCount(jobs); centralDispatch.Kernel.Get<JobProvider>().All().Should().HaveSameCount(jobs);
} }
[Test] [Test]
public void indexers_are_initialized() public void indexers_are_initialized()
{ {
CentralDispatch.NinjectKernel.Get<IndexerProvider>().All().Should().HaveSameCount(indexers); centralDispatch.Kernel.Get<IndexerProvider>().All().Should().HaveSameCount(indexers);
} }
[Test] [Test]
public void quality_profile_initialized() public void quality_profile_initialized()
{ {
CentralDispatch.NinjectKernel.Get<QualityProvider>().All().Should().HaveCount(2); centralDispatch.Kernel.Get<QualityProvider>().All().Should().HaveCount(2);
} }
[Test] [Test]
public void JobProvider_should_be_singletone() public void JobProvider_should_be_singletone()
{ {
var first = CentralDispatch.NinjectKernel.Get<JobProvider>(); var first = centralDispatch.Kernel.Get<JobProvider>();
var second = CentralDispatch.NinjectKernel.Get<JobProvider>(); var second = centralDispatch.Kernel.Get<JobProvider>();
first.Should().BeSameAs(second); first.Should().BeSameAs(second);
} }

View File

@ -26,7 +26,7 @@ namespace NzbDrone.Core.Test.ProviderTests
private EpisodeParseResult parseResultSingle; private EpisodeParseResult parseResultSingle;
[SetUp] [SetUp]
public new void Setup() public void Setup()
{ {
parseResultMulti = new EpisodeParseResult() parseResultMulti = new EpisodeParseResult()
{ {

View File

@ -1,9 +1,6 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Web.Hosting;
using Ninject; using Ninject;
using NLog; using NLog;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
@ -17,111 +14,104 @@ using PetaPoco;
namespace NzbDrone.Core namespace NzbDrone.Core
{ {
public static class CentralDispatch public class CentralDispatch
{ {
private static StandardKernel _kernel; private readonly Object KernelLock = new object();
private static readonly Object KernelLock = new object();
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public static StandardKernel NinjectKernel public CentralDispatch()
{
get
{
if (_kernel == null)
{ {
InitializeApp(); InitializeApp();
} }
return _kernel;
}
}
public static void InitializeApp() public StandardKernel Kernel { get; private set; }
private void InitializeApp()
{ {
BindKernel(); BindKernel();
_kernel.Get<LogConfiguration>().Setup(); Kernel.Get<LogConfiguration>().Setup();
var mainConnectionString = _kernel.Get<Connection>().MainConnectionString; var mainConnectionString = Kernel.Get<Connection>().MainConnectionString;
MigrationsHelper.Run(mainConnectionString, true); MigrationsHelper.Run(mainConnectionString, true);
LogConfiguration.RegisterDatabaseLogger(_kernel.Get<DatabaseTarget>()); LogConfiguration.RegisterDatabaseLogger(Kernel.Get<DatabaseTarget>());
_kernel.Get<QualityProvider>().SetupDefaultProfiles(); Kernel.Get<QualityProvider>().SetupDefaultProfiles();
_kernel.Get<QualityTypeProvider>().SetupDefault(); Kernel.Get<QualityTypeProvider>().SetupDefault();
_kernel.Get<ConfigFileProvider>().CreateDefaultConfigFile(); Kernel.Get<ConfigFileProvider>().CreateDefaultConfigFile();
BindExternalNotifications(); BindExternalNotifications();
BindIndexers(); BindIndexers();
BindJobs(); BindJobs();
} }
private static void BindKernel() private void BindKernel()
{ {
lock (KernelLock) lock (KernelLock)
{ {
Logger.Debug("Binding Ninject's Kernel"); Logger.Debug("Binding Ninject's Kernel");
_kernel = new StandardKernel(); Kernel = new StandardKernel();
var connection = _kernel.Get<Connection>(); var connection = Kernel.Get<Connection>();
_kernel.Bind<IDatabase>().ToMethod(c => connection.GetMainPetaPocoDb()).InTransientScope(); Kernel.Bind<IDatabase>().ToMethod(c => connection.GetMainPetaPocoDb()).InTransientScope();
_kernel.Bind<IDatabase>().ToMethod(c => connection.GetLogPetaPocoDb(false)).WhenInjectedInto<DatabaseTarget>().InSingletonScope(); Kernel.Bind<IDatabase>().ToMethod(c => connection.GetLogPetaPocoDb(false)).WhenInjectedInto<DatabaseTarget>().InSingletonScope();
_kernel.Bind<IDatabase>().ToMethod(c => connection.GetLogPetaPocoDb()).WhenInjectedInto<LogProvider>().InSingletonScope(); Kernel.Bind<IDatabase>().ToMethod(c => connection.GetLogPetaPocoDb()).WhenInjectedInto<LogProvider>().InSingletonScope();
_kernel.Bind<JobProvider>().ToSelf().InSingletonScope(); Kernel.Bind<JobProvider>().ToSelf().InSingletonScope();
} }
} }
private static void BindIndexers() private void BindIndexers()
{ {
_kernel.Bind<IndexerBase>().To<NzbsOrg>(); Kernel.Bind<IndexerBase>().To<NzbsOrg>();
_kernel.Bind<IndexerBase>().To<NzbMatrix>(); Kernel.Bind<IndexerBase>().To<NzbMatrix>();
_kernel.Bind<IndexerBase>().To<NzbsRUs>(); Kernel.Bind<IndexerBase>().To<NzbsRUs>();
_kernel.Bind<IndexerBase>().To<Newzbin>(); Kernel.Bind<IndexerBase>().To<Newzbin>();
var indexers = _kernel.GetAll<IndexerBase>(); var indexers = Kernel.GetAll<IndexerBase>();
_kernel.Get<IndexerProvider>().InitializeIndexers(indexers.ToList()); Kernel.Get<IndexerProvider>().InitializeIndexers(indexers.ToList());
} }
private static void BindJobs() private void BindJobs()
{ {
_kernel.Bind<IJob>().To<RssSyncJob>().InSingletonScope(); Kernel.Bind<IJob>().To<RssSyncJob>().InSingletonScope();
_kernel.Bind<IJob>().To<ImportNewSeriesJob>().InSingletonScope(); Kernel.Bind<IJob>().To<ImportNewSeriesJob>().InSingletonScope();
_kernel.Bind<IJob>().To<UpdateInfoJob>().InSingletonScope(); Kernel.Bind<IJob>().To<UpdateInfoJob>().InSingletonScope();
_kernel.Bind<IJob>().To<DiskScanJob>().InSingletonScope(); Kernel.Bind<IJob>().To<DiskScanJob>().InSingletonScope();
_kernel.Bind<IJob>().To<DeleteSeriesJob>().InSingletonScope(); Kernel.Bind<IJob>().To<DeleteSeriesJob>().InSingletonScope();
_kernel.Bind<IJob>().To<EpisodeSearchJob>().InSingletonScope(); Kernel.Bind<IJob>().To<EpisodeSearchJob>().InSingletonScope();
_kernel.Bind<IJob>().To<RenameEpisodeJob>().InSingletonScope(); Kernel.Bind<IJob>().To<RenameEpisodeJob>().InSingletonScope();
_kernel.Bind<IJob>().To<PostDownloadScanJob>().InSingletonScope(); Kernel.Bind<IJob>().To<PostDownloadScanJob>().InSingletonScope();
_kernel.Bind<IJob>().To<UpdateSceneMappingsJob>().InSingletonScope(); Kernel.Bind<IJob>().To<UpdateSceneMappingsJob>().InSingletonScope();
_kernel.Bind<IJob>().To<SeasonSearchJob>().InSingletonScope(); Kernel.Bind<IJob>().To<SeasonSearchJob>().InSingletonScope();
_kernel.Bind<IJob>().To<RenameSeasonJob>().InSingletonScope(); Kernel.Bind<IJob>().To<RenameSeasonJob>().InSingletonScope();
_kernel.Bind<IJob>().To<SeriesSearchJob>().InSingletonScope(); Kernel.Bind<IJob>().To<SeriesSearchJob>().InSingletonScope();
_kernel.Bind<IJob>().To<RenameSeriesJob>().InSingletonScope(); Kernel.Bind<IJob>().To<RenameSeriesJob>().InSingletonScope();
_kernel.Bind<IJob>().To<BacklogSearchJob>().InSingletonScope(); Kernel.Bind<IJob>().To<BacklogSearchJob>().InSingletonScope();
_kernel.Bind<IJob>().To<BannerDownloadJob>().InSingletonScope(); Kernel.Bind<IJob>().To<BannerDownloadJob>().InSingletonScope();
_kernel.Bind<IJob>().To<ConvertEpisodeJob>().InSingletonScope(); Kernel.Bind<IJob>().To<ConvertEpisodeJob>().InSingletonScope();
_kernel.Get<JobProvider>().Initialize(); Kernel.Get<JobProvider>().Initialize();
_kernel.Get<WebTimer>().StartTimer(30); Kernel.Get<WebTimer>().StartTimer(30);
} }
private static void BindExternalNotifications() private void BindExternalNotifications()
{ {
_kernel.Bind<ExternalNotificationBase>().To<Xbmc>(); Kernel.Bind<ExternalNotificationBase>().To<Smtp>();
_kernel.Bind<ExternalNotificationBase>().To<Smtp>(); Kernel.Bind<ExternalNotificationBase>().To<Twitter>();
_kernel.Bind<ExternalNotificationBase>().To<Twitter>(); Kernel.Bind<ExternalNotificationBase>().To<Providers.ExternalNotification.Growl>();
_kernel.Bind<ExternalNotificationBase>().To<Providers.ExternalNotification.Growl>();
var notifiers = _kernel.GetAll<ExternalNotificationBase>(); var notifiers = Kernel.GetAll<ExternalNotificationBase>();
_kernel.Get<ExternalNotificationProvider>().InitializeNotifiers(notifiers.ToList()); Kernel.Get<ExternalNotificationProvider>().InitializeNotifiers(notifiers.ToList());
} }
/// <summary> /// <summary>
/// Forces IISExpress process to exit with the host application /// Forces IISExpress process to exit with the host application
/// </summary> /// </summary>
public static void DedicateToHost() public void DedicateToHost()
{ {
try try
{ {

View File

@ -10,10 +10,12 @@ namespace NzbDrone.Core.Instrumentation
public class LogConfiguration public class LogConfiguration
{ {
private readonly PathProvider _pathProvider; private readonly PathProvider _pathProvider;
private readonly DatabaseTarget _databaseTarget;
public LogConfiguration(PathProvider pathProvider) public LogConfiguration(PathProvider pathProvider, DatabaseTarget databaseTarget)
{ {
_pathProvider = pathProvider; _pathProvider = pathProvider;
_databaseTarget = databaseTarget;
} }
public void Setup() public void Setup()
@ -28,7 +30,7 @@ namespace NzbDrone.Core.Instrumentation
Common.LogConfiguration.RegisterConsoleLogger(LogLevel.Info, "NzbDrone.Web.MvcApplication"); Common.LogConfiguration.RegisterConsoleLogger(LogLevel.Info, "NzbDrone.Web.MvcApplication");
Common.LogConfiguration.RegisterConsoleLogger(LogLevel.Info, "NzbDrone.Core.CentralDispatch"); Common.LogConfiguration.RegisterConsoleLogger(LogLevel.Info, "NzbDrone.Core.CentralDispatch");
LogManager.ConfigurationReloaded += ((s, e) => RegisterDatabaseLogger(CentralDispatch.NinjectKernel.Get<DatabaseTarget>())); LogManager.ConfigurationReloaded += ((s, e) => RegisterDatabaseLogger(_databaseTarget));
} }
public static void RegisterDatabaseLogger(DatabaseTarget databaseTarget) public static void RegisterDatabaseLogger(DatabaseTarget databaseTarget)

View File

@ -9,8 +9,6 @@ namespace NzbDrone.Core.Providers.ExternalNotification
{ {
private readonly GrowlProvider _growlProvider; private readonly GrowlProvider _growlProvider;
private readonly Logger Logger = LogManager.GetCurrentClassLogger();
public Growl(ConfigProvider configProvider, GrowlProvider growlProvider) public Growl(ConfigProvider configProvider, GrowlProvider growlProvider)
: base(configProvider) : base(configProvider)
{ {
@ -41,7 +39,7 @@ namespace NzbDrone.Core.Providers.ExternalNotification
catch (Exception ex) catch (Exception ex)
{ {
Logger.WarnException(ex.Message, ex); _logger.WarnException(ex.Message, ex);
throw; throw;
} }
} }
@ -65,7 +63,7 @@ namespace NzbDrone.Core.Providers.ExternalNotification
catch (Exception ex) catch (Exception ex)
{ {
Logger.WarnException(ex.Message, ex); _logger.WarnException(ex.Message, ex);
throw; throw;
} }
} }

View File

@ -54,13 +54,14 @@ namespace NzbDrone.Web
protected override IKernel CreateKernel() protected override IKernel CreateKernel()
{ {
var kernel = CentralDispatch.NinjectKernel;
var dispatch = new CentralDispatch();
Logger.Info("NzbDrone Starting up."); Logger.Info("NzbDrone Starting up.");
CentralDispatch.DedicateToHost(); dispatch.DedicateToHost();
kernel.Load(Assembly.GetExecutingAssembly()); dispatch.Kernel.Load(Assembly.GetExecutingAssembly());
return kernel; return dispatch.Kernel;
} }