Skip to content

Commit

Permalink
CentralDispatch is no longer static.
Browse files Browse the repository at this point in the history
  • Loading branch information
kayone committed Nov 8, 2011
1 parent ae9e941 commit ca7deed
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 87 deletions.
29 changes: 17 additions & 12 deletions NzbDrone.Core.Test/CentralDispatchFixture.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentAssertions;
using NLog;
using NUnit.Framework;
using NzbDrone.Core.Instrumentation;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Indexer;
using NzbDrone.Core.Providers.Jobs;
Expand All @@ -21,10 +18,18 @@ class CentralDispatchFixture : TestBase
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();

private CentralDispatch centralDispatch;

[SetUp]
public void Setup()
{
centralDispatch = new CentralDispatch();
}

[Test]
public void InitAppTest()
{
CentralDispatch.NinjectKernel.Should().NotBeNull();
centralDispatch.Kernel.Should().NotBeNull();
}

[Test]
Expand All @@ -37,7 +42,7 @@ public void Resolve_all_providers()
foreach (var provider in providers)
{
Console.WriteLine("Resolving " + provider.Name);
CentralDispatch.NinjectKernel.Get(provider).Should().NotBeNull();
centralDispatch.Kernel.Get(provider).Should().NotBeNull();
}
}

Expand All @@ -47,7 +52,7 @@ public void All_jobs_should_be_registered()
{
//Assert

var registeredJobs = CentralDispatch.NinjectKernel.GetAll<IJob>();
var registeredJobs = centralDispatch.Kernel.GetAll<IJob>();

jobs.Should().NotBeEmpty();

Expand All @@ -60,7 +65,7 @@ public void All_indexers_should_be_registered()
{
//Assert

var registeredIndexers = CentralDispatch.NinjectKernel.GetAll<IndexerBase>();
var registeredIndexers = centralDispatch.Kernel.GetAll<IndexerBase>();

indexers.Should().NotBeEmpty();

Expand All @@ -71,26 +76,26 @@ public void All_indexers_should_be_registered()
[Test]
public void jobs_are_initialized()
{
CentralDispatch.NinjectKernel.Get<JobProvider>().All().Should().HaveSameCount(jobs);
centralDispatch.Kernel.Get<JobProvider>().All().Should().HaveSameCount(jobs);
}

[Test]
public void indexers_are_initialized()
{
CentralDispatch.NinjectKernel.Get<IndexerProvider>().All().Should().HaveSameCount(indexers);
centralDispatch.Kernel.Get<IndexerProvider>().All().Should().HaveSameCount(indexers);
}

[Test]
public void quality_profile_initialized()
{
CentralDispatch.NinjectKernel.Get<QualityProvider>().All().Should().HaveCount(2);
centralDispatch.Kernel.Get<QualityProvider>().All().Should().HaveCount(2);
}

[Test]
public void JobProvider_should_be_singletone()
{
var first = CentralDispatch.NinjectKernel.Get<JobProvider>();
var second = CentralDispatch.NinjectKernel.Get<JobProvider>();
var first = centralDispatch.Kernel.Get<JobProvider>();
var second = centralDispatch.Kernel.Get<JobProvider>();

first.Should().BeSameAs(second);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class InventoryProvider_IsMonitoredTest : TestBase
private EpisodeParseResult parseResultSingle;

[SetUp]
public new void Setup()
public void Setup()
{
parseResultMulti = new EpisodeParseResult()
{
Expand Down
118 changes: 54 additions & 64 deletions NzbDrone.Core/CentralDispatch.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web.Hosting;
using Ninject;
using NLog;
using NzbDrone.Core.Datastore;
Expand All @@ -17,111 +14,104 @@

namespace NzbDrone.Core
{
public static class CentralDispatch
public class CentralDispatch
{
private static StandardKernel _kernel;
private static readonly Object KernelLock = new object();
private readonly Object KernelLock = new object();
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

public static StandardKernel NinjectKernel
public CentralDispatch()
{
get
{
if (_kernel == null)
{
InitializeApp();
}
return _kernel;
}
InitializeApp();
}

public static void InitializeApp()
public StandardKernel Kernel { get; private set; }

private void InitializeApp()
{
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);

LogConfiguration.RegisterDatabaseLogger(_kernel.Get<DatabaseTarget>());
LogConfiguration.RegisterDatabaseLogger(Kernel.Get<DatabaseTarget>());

_kernel.Get<QualityProvider>().SetupDefaultProfiles();
_kernel.Get<QualityTypeProvider>().SetupDefault();
_kernel.Get<ConfigFileProvider>().CreateDefaultConfigFile();
Kernel.Get<QualityProvider>().SetupDefaultProfiles();
Kernel.Get<QualityTypeProvider>().SetupDefault();
Kernel.Get<ConfigFileProvider>().CreateDefaultConfigFile();

BindExternalNotifications();
BindIndexers();
BindJobs();
}

private static void BindKernel()
private void BindKernel()
{
lock (KernelLock)
{
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.GetLogPetaPocoDb(false)).WhenInjectedInto<DatabaseTarget>().InSingletonScope();
_kernel.Bind<IDatabase>().ToMethod(c => connection.GetLogPetaPocoDb()).WhenInjectedInto<LogProvider>().InSingletonScope();
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()).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<NzbMatrix>();
_kernel.Bind<IndexerBase>().To<NzbsRUs>();
_kernel.Bind<IndexerBase>().To<Newzbin>();
Kernel.Bind<IndexerBase>().To<NzbsOrg>();
Kernel.Bind<IndexerBase>().To<NzbMatrix>();
Kernel.Bind<IndexerBase>().To<NzbsRUs>();
Kernel.Bind<IndexerBase>().To<Newzbin>();

var indexers = _kernel.GetAll<IndexerBase>();
_kernel.Get<IndexerProvider>().InitializeIndexers(indexers.ToList());
var indexers = Kernel.GetAll<IndexerBase>();
Kernel.Get<IndexerProvider>().InitializeIndexers(indexers.ToList());
}

private static void BindJobs()
private void BindJobs()
{
_kernel.Bind<IJob>().To<RssSyncJob>().InSingletonScope();
_kernel.Bind<IJob>().To<ImportNewSeriesJob>().InSingletonScope();
_kernel.Bind<IJob>().To<UpdateInfoJob>().InSingletonScope();
_kernel.Bind<IJob>().To<DiskScanJob>().InSingletonScope();
_kernel.Bind<IJob>().To<DeleteSeriesJob>().InSingletonScope();
_kernel.Bind<IJob>().To<EpisodeSearchJob>().InSingletonScope();
_kernel.Bind<IJob>().To<RenameEpisodeJob>().InSingletonScope();
_kernel.Bind<IJob>().To<PostDownloadScanJob>().InSingletonScope();
_kernel.Bind<IJob>().To<UpdateSceneMappingsJob>().InSingletonScope();
_kernel.Bind<IJob>().To<SeasonSearchJob>().InSingletonScope();
_kernel.Bind<IJob>().To<RenameSeasonJob>().InSingletonScope();
_kernel.Bind<IJob>().To<SeriesSearchJob>().InSingletonScope();
_kernel.Bind<IJob>().To<RenameSeriesJob>().InSingletonScope();
_kernel.Bind<IJob>().To<BacklogSearchJob>().InSingletonScope();
_kernel.Bind<IJob>().To<BannerDownloadJob>().InSingletonScope();
_kernel.Bind<IJob>().To<ConvertEpisodeJob>().InSingletonScope();

_kernel.Get<JobProvider>().Initialize();
_kernel.Get<WebTimer>().StartTimer(30);
Kernel.Bind<IJob>().To<RssSyncJob>().InSingletonScope();
Kernel.Bind<IJob>().To<ImportNewSeriesJob>().InSingletonScope();
Kernel.Bind<IJob>().To<UpdateInfoJob>().InSingletonScope();
Kernel.Bind<IJob>().To<DiskScanJob>().InSingletonScope();
Kernel.Bind<IJob>().To<DeleteSeriesJob>().InSingletonScope();
Kernel.Bind<IJob>().To<EpisodeSearchJob>().InSingletonScope();
Kernel.Bind<IJob>().To<RenameEpisodeJob>().InSingletonScope();
Kernel.Bind<IJob>().To<PostDownloadScanJob>().InSingletonScope();
Kernel.Bind<IJob>().To<UpdateSceneMappingsJob>().InSingletonScope();
Kernel.Bind<IJob>().To<SeasonSearchJob>().InSingletonScope();
Kernel.Bind<IJob>().To<RenameSeasonJob>().InSingletonScope();
Kernel.Bind<IJob>().To<SeriesSearchJob>().InSingletonScope();
Kernel.Bind<IJob>().To<RenameSeriesJob>().InSingletonScope();
Kernel.Bind<IJob>().To<BacklogSearchJob>().InSingletonScope();
Kernel.Bind<IJob>().To<BannerDownloadJob>().InSingletonScope();
Kernel.Bind<IJob>().To<ConvertEpisodeJob>().InSingletonScope();

Kernel.Get<JobProvider>().Initialize();
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<Twitter>();
_kernel.Bind<ExternalNotificationBase>().To<Providers.ExternalNotification.Growl>();
Kernel.Bind<ExternalNotificationBase>().To<Smtp>();
Kernel.Bind<ExternalNotificationBase>().To<Twitter>();
Kernel.Bind<ExternalNotificationBase>().To<Providers.ExternalNotification.Growl>();

var notifiers = _kernel.GetAll<ExternalNotificationBase>();
_kernel.Get<ExternalNotificationProvider>().InitializeNotifiers(notifiers.ToList());
var notifiers = Kernel.GetAll<ExternalNotificationBase>();
Kernel.Get<ExternalNotificationProvider>().InitializeNotifiers(notifiers.ToList());
}

/// <summary>
/// Forces IISExpress process to exit with the host application
/// </summary>
public static void DedicateToHost()
public void DedicateToHost()
{
try
{
Expand Down
6 changes: 4 additions & 2 deletions NzbDrone.Core/Instrumentation/LogConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ namespace NzbDrone.Core.Instrumentation
public class LogConfiguration
{
private readonly PathProvider _pathProvider;
private readonly DatabaseTarget _databaseTarget;

public LogConfiguration(PathProvider pathProvider)
public LogConfiguration(PathProvider pathProvider, DatabaseTarget databaseTarget)
{
_pathProvider = pathProvider;
_databaseTarget = databaseTarget;
}

public void Setup()
Expand All @@ -28,7 +30,7 @@ public void Setup()
Common.LogConfiguration.RegisterConsoleLogger(LogLevel.Info, "NzbDrone.Web.MvcApplication");
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)
Expand Down
6 changes: 2 additions & 4 deletions NzbDrone.Core/Providers/ExternalNotification/Growl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ public class Growl : ExternalNotificationBase
{
private readonly GrowlProvider _growlProvider;

private readonly Logger Logger = LogManager.GetCurrentClassLogger();

public Growl(ConfigProvider configProvider, GrowlProvider growlProvider)
: base(configProvider)
{
Expand Down Expand Up @@ -41,7 +39,7 @@ public override void OnGrab(string message)

catch (Exception ex)
{
Logger.WarnException(ex.Message, ex);
_logger.WarnException(ex.Message, ex);
throw;
}
}
Expand All @@ -65,7 +63,7 @@ public override void OnDownload(string message, Series series)

catch (Exception ex)
{
Logger.WarnException(ex.Message, ex);
_logger.WarnException(ex.Message, ex);
throw;
}
}
Expand Down
9 changes: 5 additions & 4 deletions NzbDrone.Web/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ protected override void OnApplicationStarted()

protected override IKernel CreateKernel()
{
var kernel = CentralDispatch.NinjectKernel;

var dispatch = new CentralDispatch();
Logger.Info("NzbDrone Starting up.");

CentralDispatch.DedicateToHost();
dispatch.DedicateToHost();

kernel.Load(Assembly.GetExecutingAssembly());
return kernel;
dispatch.Kernel.Load(Assembly.GetExecutingAssembly());
return dispatch.Kernel;
}


Expand Down

0 comments on commit ca7deed

Please sign in to comment.