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.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;
@ -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> 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]
@ -37,7 +42,7 @@ namespace NzbDrone.Core.Test
foreach (var provider in providers)
{
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
var registeredJobs = CentralDispatch.NinjectKernel.GetAll<IJob>();
var registeredJobs = centralDispatch.Kernel.GetAll<IJob>();
jobs.Should().NotBeEmpty();
@ -60,7 +65,7 @@ namespace NzbDrone.Core.Test
{
//Assert
var registeredIndexers = CentralDispatch.NinjectKernel.GetAll<IndexerBase>();
var registeredIndexers = centralDispatch.Kernel.GetAll<IndexerBase>();
indexers.Should().NotBeEmpty();
@ -71,26 +76,26 @@ namespace NzbDrone.Core.Test
[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);
}

View File

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

View File

@ -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;
@ -17,111 +14,104 @@ using PetaPoco;
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
{
get
{
if (_kernel == null)
public CentralDispatch()
{
InitializeApp();
}
return _kernel;
}
}
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.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.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
{

View File

@ -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()
@ -28,7 +30,7 @@ namespace NzbDrone.Core.Instrumentation
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)

View File

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

View File

@ -54,13 +54,14 @@ namespace NzbDrone.Web
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;
}