replaced IInitializable interface with ApplicationStartedEvent

pull/4/head
Keivan Beigi 12 years ago
parent 5d1026ded2
commit a8a64a42b5

@ -1,6 +1,4 @@
using System; using NLog;
using System.Collections.Generic;
using NLog;
using Nancy.Bootstrapper; using Nancy.Bootstrapper;
using Nancy.Conventions; using Nancy.Conventions;
using Nancy.Diagnostics; using Nancy.Diagnostics;
@ -8,10 +6,10 @@ using NzbDrone.Api.ErrorManagement;
using NzbDrone.Api.Extensions; using NzbDrone.Api.Extensions;
using NzbDrone.Api.Frontend; using NzbDrone.Api.Frontend;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.Eventing;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
using TinyIoC; using TinyIoC;
using ErrorPipeline = NzbDrone.Api.ErrorManagement.ErrorPipeline;
namespace NzbDrone.Api namespace NzbDrone.Api
{ {
@ -31,7 +29,8 @@ namespace NzbDrone.Api
_logger.Info("Starting NzbDrone API"); _logger.Info("Starting NzbDrone API");
AutomapperBootstraper.InitializeAutomapper(); AutomapperBootstraper.InitializeAutomapper();
RegisterReporting(container); RegisterReporting(container);
KickoffInitilizables(container);
container.Resolve<IEventAggregator>().Publish(new ApplicationStartedEvent());
ApplicationPipelines.OnError.AddItemToEndOfPipeline(container.Resolve<ErrorPipeline>().HandleException); ApplicationPipelines.OnError.AddItemToEndOfPipeline(container.Resolve<ErrorPipeline>().HandleException);
} }
@ -42,26 +41,6 @@ namespace NzbDrone.Api
ReportingService.RestProvider = container.Resolve<RestProvider>(); ReportingService.RestProvider = container.Resolve<RestProvider>();
} }
private void KickoffInitilizables(TinyIoCContainer container)
{
var initilizables = container.ResolveAll<IInitializable>();
foreach (var initializable in initilizables)
{
_logger.Debug("Initializing {0}", initializable.GetType().Name);
try
{
initializable.Init();
}
catch (Exception e)
{
_logger.FatalException("An error occurred while initializing " + initializable.GetType().Name, e);
throw;
}
}
}
protected override TinyIoCContainer GetApplicationContainer() protected override TinyIoCContainer GetApplicationContainer()
{ {
return _tinyIoCContainer; return _tinyIoCContainer;

@ -5,6 +5,7 @@ using FizzWare.NBuilder;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.Jobs; using NzbDrone.Core.Jobs;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.JobTests namespace NzbDrone.Core.Test.JobTests
@ -23,6 +24,12 @@ namespace NzbDrone.Core.Test.JobTests
} }
private void Initialize()
{
Subject.Handle(new ApplicationStartedEvent());
}
[Test] [Test]
public void Init_should_add_defintaions() public void Init_should_add_defintaions()
{ {
@ -30,7 +37,7 @@ namespace NzbDrone.Core.Test.JobTests
Mocker.SetConstant(baseFakeJobs); Mocker.SetConstant(baseFakeJobs);
Subject.Init(); Initialize();
Storage.All().Should().HaveCount(1); Storage.All().Should().HaveCount(1);
StoredModel.Interval.Should().Be((Int32)_fakeJob.DefaultInterval.TotalMinutes); StoredModel.Interval.Should().Be((Int32)_fakeJob.DefaultInterval.TotalMinutes);
@ -58,7 +65,7 @@ namespace NzbDrone.Core.Test.JobTests
AllStoredModels.Should().HaveCount(1); AllStoredModels.Should().HaveCount(1);
AllStoredModels.Should().Contain(c => c.Type == deletedJob.Type); AllStoredModels.Should().Contain(c => c.Type == deletedJob.Type);
Subject.Init(); Initialize();
//Make sure init has cleaned up the deleted job //Make sure init has cleaned up the deleted job
AllStoredModels.Should().HaveCount(1); AllStoredModels.Should().HaveCount(1);
@ -82,7 +89,7 @@ namespace NzbDrone.Core.Test.JobTests
AllStoredModels.Should().HaveCount(1); AllStoredModels.Should().HaveCount(1);
AllStoredModels.Should().Contain(c => c.Type == deletedJob.Type); AllStoredModels.Should().Contain(c => c.Type == deletedJob.Type);
Subject.Init(); Initialize();
//Make sure init has cleaned up the deleted job //Make sure init has cleaned up the deleted job
AllStoredModels.Should().HaveCount(1); AllStoredModels.Should().HaveCount(1);
@ -110,7 +117,7 @@ namespace NzbDrone.Core.Test.JobTests
IEnumerable<IJob> fakeJobs = new List<IJob> { newJob }; IEnumerable<IJob> fakeJobs = new List<IJob> { newJob };
Mocker.SetConstant(fakeJobs); Mocker.SetConstant(fakeJobs);
Subject.Init(); Initialize();
AllStoredModels.Should().HaveCount(1); AllStoredModels.Should().HaveCount(1);
@ -128,7 +135,7 @@ namespace NzbDrone.Core.Test.JobTests
IEnumerable<IJob> fakeJobs = new List<IJob> { _disabledJob }; IEnumerable<IJob> fakeJobs = new List<IJob> { _disabledJob };
Mocker.SetConstant(fakeJobs); Mocker.SetConstant(fakeJobs);
Subject.Init(); Initialize();
Storage.All().Should().HaveCount(1); Storage.All().Should().HaveCount(1);

@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Eventing;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
@ -14,7 +15,7 @@ namespace NzbDrone.Core.DataAugmentation.Scene
string GetCleanName(int tvdbId); string GetCleanName(int tvdbId);
} }
public class SceneMappingService : IInitializable, ISceneMappingService public class SceneMappingService : ISceneMappingService,IHandleAsync<ApplicationStartedEvent>
{ {
private readonly ISceneMappingRepository _repository; private readonly ISceneMappingRepository _repository;
private readonly ISceneMappingProxy _sceneMappingProxy; private readonly ISceneMappingProxy _sceneMappingProxy;
@ -84,7 +85,7 @@ namespace NzbDrone.Core.DataAugmentation.Scene
return mapping.CleanTitle; return mapping.CleanTitle;
} }
public void Init() public void HandleAsync(ApplicationStartedEvent message)
{ {
if (!_repository.HasItems()) if (!_repository.HasItems())
{ {

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Eventing;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
@ -14,7 +15,7 @@ namespace NzbDrone.Core.Indexers
IndexerDefinition Get(string name); IndexerDefinition Get(string name);
} }
public class IndexerService : IIndexerService, IInitializable public class IndexerService : IIndexerService, IHandle<ApplicationStartedEvent>
{ {
private readonly IIndexerRepository _indexerRepository; private readonly IIndexerRepository _indexerRepository;
private readonly Logger _logger; private readonly Logger _logger;
@ -28,27 +29,6 @@ namespace NzbDrone.Core.Indexers
_indexers = indexers.ToList(); _indexers = indexers.ToList();
} }
public void Init()
{
_logger.Debug("Initializing indexers. Count {0}", _indexers.Count);
var currentIndexers = All();
foreach (var feedProvider in _indexers)
{
IIndexerBase indexerLocal = feedProvider;
if (!currentIndexers.Exists(c => c.Name == indexerLocal.Name))
{
var settings = new IndexerDefinition
{
Enable = indexerLocal.EnabledByDefault,
Name = indexerLocal.Name.ToLower()
};
_indexerRepository.Insert(settings);
}
}
}
public List<IndexerDefinition> All() public List<IndexerDefinition> All()
{ {
@ -72,5 +52,27 @@ namespace NzbDrone.Core.Indexers
{ {
return _indexerRepository.Get(name); return _indexerRepository.Get(name);
} }
public void Handle(ApplicationStartedEvent message)
{
_logger.Debug("Initializing indexers. Count {0}", _indexers.Count);
var currentIndexers = All();
foreach (var feedProvider in _indexers)
{
IIndexerBase indexerLocal = feedProvider;
if (!currentIndexers.Exists(c => c.Name == indexerLocal.Name))
{
var settings = new IndexerDefinition
{
Enable = indexerLocal.EnabledByDefault,
Name = indexerLocal.Name.ToLower()
};
_indexerRepository.Insert(settings);
}
}
}
} }
} }

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using NLog; using NLog;
using NzbDrone.Common.Eventing;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
namespace NzbDrone.Core.Indexers.Newznab namespace NzbDrone.Core.Indexers.Newznab
@ -15,7 +16,7 @@ namespace NzbDrone.Core.Indexers.Newznab
void Update(NewznabDefinition definition); void Update(NewznabDefinition definition);
} }
public class NewznabService : INewznabService, IInitializable public class NewznabService : INewznabService, IHandle<ApplicationStartedEvent>
{ {
private readonly INewznabRepository _newznabRepository; private readonly INewznabRepository _newznabRepository;
private readonly Logger _logger; private readonly Logger _logger;
@ -74,7 +75,7 @@ namespace NzbDrone.Core.Indexers.Newznab
} }
public void Init() public void Handle(ApplicationStartedEvent message)
{ {
var newznabIndexers = new List<NewznabDefinition> var newznabIndexers = new List<NewznabDefinition>
{ {

@ -2,18 +2,19 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Eventing;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
namespace NzbDrone.Core.Jobs namespace NzbDrone.Core.Jobs
{ {
public interface IJobRepository : IInitializable, IBasicRepository<JobDefinition> public interface IJobRepository : IBasicRepository<JobDefinition>
{ {
IList<JobDefinition> GetPendingJobs(); IList<JobDefinition> GetPendingJobs();
JobDefinition GetDefinition(Type type); JobDefinition GetDefinition(Type type);
} }
public class JobRepository : BasicRepository<JobDefinition>, IJobRepository public class JobRepository : BasicRepository<JobDefinition>, IJobRepository, IHandle<ApplicationStartedEvent>
{ {
private readonly IEnumerable<IJob> _jobs; private readonly IEnumerable<IJob> _jobs;
private readonly Logger _logger; private readonly Logger _logger;
@ -36,7 +37,7 @@ namespace NzbDrone.Core.Jobs
return Query.Where(c => c.Enable == true && c.Interval != 2).ToList().Where(c => c.LastExecution < DateTime.Now.AddMinutes(-c.Interval)).ToList(); return Query.Where(c => c.Enable == true && c.Interval != 2).ToList().Where(c => c.LastExecution < DateTime.Now.AddMinutes(-c.Interval)).ToList();
} }
public void Init() public void Handle(ApplicationStartedEvent message)
{ {
var currentJobs = All().ToList(); var currentJobs = All().ToList();
_logger.Debug("Initializing jobs. Available: {0} Existing:{1}", _jobs.Count(), currentJobs.Count()); _logger.Debug("Initializing jobs. Available: {0} Existing:{1}", _jobs.Count(), currentJobs.Count());
@ -57,10 +58,10 @@ namespace NzbDrone.Core.Jobs
if (jobDefinition == null) if (jobDefinition == null)
{ {
jobDefinition = new JobDefinition jobDefinition = new JobDefinition
{ {
Type = job.GetType().ToString(), Type = job.GetType().ToString(),
LastExecution = DateTime.Now LastExecution = DateTime.Now
}; };
} }
jobDefinition.Enable = job.DefaultInterval.TotalSeconds > 0; jobDefinition.Enable = job.DefaultInterval.TotalSeconds > 0;

@ -1,9 +1,10 @@
using System.Timers; using System.Timers;
using NzbDrone.Common.Eventing;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
namespace NzbDrone.Core.Jobs namespace NzbDrone.Core.Jobs
{ {
public class JobTimer : IInitializable public class JobTimer : IHandle<ApplicationStartedEvent>
{ {
private readonly IJobController _jobController; private readonly IJobController _jobController;
private readonly Timer _timer; private readonly Timer _timer;
@ -15,12 +16,11 @@ namespace NzbDrone.Core.Jobs
} }
public void Init() public void Handle(ApplicationStartedEvent message)
{ {
_timer.Interval = 1000 * 30; _timer.Interval = 1000 * 30;
_timer.Elapsed += (o, args) => _jobController.EnqueueScheduled(); _timer.Elapsed += (o, args) => _jobController.EnqueueScheduled();
_timer.Start(); _timer.Start();
} }
} }
} }

@ -0,0 +1,10 @@
using NzbDrone.Common.Eventing;
namespace NzbDrone.Core.Lifecycle
{
public class ApplicationStartedEvent : IEvent
{
}
}

@ -1,7 +0,0 @@
namespace NzbDrone.Core.Lifecycle
{
public interface IInitializable
{
void Init();
}
}

@ -300,7 +300,7 @@
<Compile Include="Jobs\JobTimer.cs" /> <Compile Include="Jobs\JobTimer.cs" />
<Compile Include="Lifecycle\AppShutdownJob.cs" /> <Compile Include="Lifecycle\AppShutdownJob.cs" />
<Compile Include="Lifecycle\AppRestartJob.cs" /> <Compile Include="Lifecycle\AppRestartJob.cs" />
<Compile Include="Lifecycle\IInitializable.cs" /> <Compile Include="Lifecycle\ApplicationStartedEvent.cs" />
<Compile Include="MediaCover\MediaCover.cs" /> <Compile Include="MediaCover\MediaCover.cs" />
<Compile Include="MediaFiles\EpisodeFileMovingService.cs" /> <Compile Include="MediaFiles\EpisodeFileMovingService.cs" />
<Compile Include="MediaFiles\Events\EpisodeFileDeletedEvent.cs" /> <Compile Include="MediaFiles\Events\EpisodeFileDeletedEvent.cs" />

@ -1,9 +1,8 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Eventing;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Qualities;
namespace NzbDrone.Core.Qualities namespace NzbDrone.Core.Qualities
@ -17,7 +16,7 @@ namespace NzbDrone.Core.Qualities
QualityProfile Get(int id); QualityProfile Get(int id);
} }
public class QualityProfileService : IQualityProfileService, IInitializable public class QualityProfileService : IQualityProfileService, IHandle<ApplicationStartedEvent>
{ {
private readonly IQualityProfileRepository _qualityProfileRepository; private readonly IQualityProfileRepository _qualityProfileRepository;
private readonly Logger _logger; private readonly Logger _logger;
@ -54,6 +53,11 @@ namespace NzbDrone.Core.Qualities
} }
public void Init() public void Init()
{
}
public void Handle(ApplicationStartedEvent message)
{ {
if (All().Any()) return; if (All().Any()) return;

@ -1,8 +1,7 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using NLog; using NLog;
using NzbDrone.Common.Eventing;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
namespace NzbDrone.Core.Qualities namespace NzbDrone.Core.Qualities
@ -15,7 +14,7 @@ namespace NzbDrone.Core.Qualities
QualitySize Get(int qualityId); QualitySize Get(int qualityId);
} }
public class QualitySizeService : IQualitySizeService, IInitializable public class QualitySizeService : IQualitySizeService, IHandle<ApplicationStartedEvent>
{ {
private readonly IQualitySizeRepository _qualitySizeRepository; private readonly IQualitySizeRepository _qualitySizeRepository;
private readonly Logger _logger; private readonly Logger _logger;
@ -47,6 +46,11 @@ namespace NzbDrone.Core.Qualities
} }
public void Init() public void Init()
{
}
public void Handle(ApplicationStartedEvent message)
{ {
var existing = All(); var existing = All();
@ -54,15 +58,15 @@ namespace NzbDrone.Core.Qualities
foreach (var quality in Quality.All().Where(q => q.Id > 0)) foreach (var quality in Quality.All().Where(q => q.Id > 0))
{ {
if(!existing.Any(s => s.QualityId == quality.Id)) if (!existing.Any(s => s.QualityId == quality.Id))
{ {
_qualitySizeRepository.Insert(new QualitySize _qualitySizeRepository.Insert(new QualitySize
{ {
QualityId = quality.Id, QualityId = quality.Id,
Name = quality.Name, Name = quality.Name,
MinSize = 0, MinSize = 0,
MaxSize = 100 MaxSize = 100
}); });
} }
} }
} }

@ -2,6 +2,7 @@
<FileVersion>1</FileVersion> <FileVersion>1</FileVersion>
<AutoEnableOnStartup>False</AutoEnableOnStartup> <AutoEnableOnStartup>False</AutoEnableOnStartup>
<AllowParallelTestExecution>true</AllowParallelTestExecution> <AllowParallelTestExecution>true</AllowParallelTestExecution>
<AllowTestsToRunInParallelWithThemselves>true</AllowTestsToRunInParallelWithThemselves>
<FrameworkUtilisationTypeForNUnit>UseDynamicAnalysis</FrameworkUtilisationTypeForNUnit> <FrameworkUtilisationTypeForNUnit>UseDynamicAnalysis</FrameworkUtilisationTypeForNUnit>
<FrameworkUtilisationTypeForGallio>Disabled</FrameworkUtilisationTypeForGallio> <FrameworkUtilisationTypeForGallio>Disabled</FrameworkUtilisationTypeForGallio>
<FrameworkUtilisationTypeForMSpec>Disabled</FrameworkUtilisationTypeForMSpec> <FrameworkUtilisationTypeForMSpec>Disabled</FrameworkUtilisationTypeForMSpec>

@ -36,7 +36,6 @@ namespace NzbDrone
container.AutoRegisterImplementations<IIndexerBase>(); container.AutoRegisterImplementations<IIndexerBase>();
container.AutoRegisterImplementations<ExternalNotificationBase>(); container.AutoRegisterImplementations<ExternalNotificationBase>();
container.AutoRegisterMultipleImplementations<IInitializable>();
container.Register<IEventAggregator, EventAggregator>().AsSingleton(); container.Register<IEventAggregator, EventAggregator>().AsSingleton();
container.Register<INancyBootstrapper, TinyNancyBootstrapper>().AsSingleton(); container.Register<INancyBootstrapper, TinyNancyBootstrapper>().AsSingleton();

Loading…
Cancel
Save