diff --git a/NzbDrone.Api/NancyBootstrapper.cs b/NzbDrone.Api/NancyBootstrapper.cs index 40388308f..e51e211a5 100644 --- a/NzbDrone.Api/NancyBootstrapper.cs +++ b/NzbDrone.Api/NancyBootstrapper.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using NLog; +using NLog; using Nancy.Bootstrapper; using Nancy.Conventions; using Nancy.Diagnostics; @@ -8,10 +6,10 @@ using NzbDrone.Api.ErrorManagement; using NzbDrone.Api.Extensions; using NzbDrone.Api.Frontend; using NzbDrone.Common; +using NzbDrone.Common.Eventing; using NzbDrone.Core.Configuration; using NzbDrone.Core.Lifecycle; using TinyIoC; -using ErrorPipeline = NzbDrone.Api.ErrorManagement.ErrorPipeline; namespace NzbDrone.Api { @@ -31,7 +29,8 @@ namespace NzbDrone.Api _logger.Info("Starting NzbDrone API"); AutomapperBootstraper.InitializeAutomapper(); RegisterReporting(container); - KickoffInitilizables(container); + + container.Resolve().Publish(new ApplicationStartedEvent()); ApplicationPipelines.OnError.AddItemToEndOfPipeline(container.Resolve().HandleException); } @@ -42,26 +41,6 @@ namespace NzbDrone.Api ReportingService.RestProvider = container.Resolve(); } - private void KickoffInitilizables(TinyIoCContainer container) - { - var initilizables = container.ResolveAll(); - - 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() { return _tinyIoCContainer; diff --git a/NzbDrone.Core.Test/JobTests/JobRepositoryFixture.cs b/NzbDrone.Core.Test/JobTests/JobRepositoryFixture.cs index ad111ac6e..c6e615da4 100644 --- a/NzbDrone.Core.Test/JobTests/JobRepositoryFixture.cs +++ b/NzbDrone.Core.Test/JobTests/JobRepositoryFixture.cs @@ -5,6 +5,7 @@ using FizzWare.NBuilder; using FluentAssertions; using NUnit.Framework; using NzbDrone.Core.Jobs; +using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Test.Framework; namespace NzbDrone.Core.Test.JobTests @@ -23,6 +24,12 @@ namespace NzbDrone.Core.Test.JobTests } + private void Initialize() + { + Subject.Handle(new ApplicationStartedEvent()); + } + + [Test] public void Init_should_add_defintaions() { @@ -30,7 +37,7 @@ namespace NzbDrone.Core.Test.JobTests Mocker.SetConstant(baseFakeJobs); - Subject.Init(); + Initialize(); Storage.All().Should().HaveCount(1); StoredModel.Interval.Should().Be((Int32)_fakeJob.DefaultInterval.TotalMinutes); @@ -58,7 +65,7 @@ namespace NzbDrone.Core.Test.JobTests AllStoredModels.Should().HaveCount(1); AllStoredModels.Should().Contain(c => c.Type == deletedJob.Type); - Subject.Init(); + Initialize(); //Make sure init has cleaned up the deleted job AllStoredModels.Should().HaveCount(1); @@ -82,7 +89,7 @@ namespace NzbDrone.Core.Test.JobTests AllStoredModels.Should().HaveCount(1); AllStoredModels.Should().Contain(c => c.Type == deletedJob.Type); - Subject.Init(); + Initialize(); //Make sure init has cleaned up the deleted job AllStoredModels.Should().HaveCount(1); @@ -110,7 +117,7 @@ namespace NzbDrone.Core.Test.JobTests IEnumerable fakeJobs = new List { newJob }; Mocker.SetConstant(fakeJobs); - Subject.Init(); + Initialize(); AllStoredModels.Should().HaveCount(1); @@ -128,7 +135,7 @@ namespace NzbDrone.Core.Test.JobTests IEnumerable fakeJobs = new List { _disabledJob }; Mocker.SetConstant(fakeJobs); - Subject.Init(); + Initialize(); Storage.All().Should().HaveCount(1); diff --git a/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs b/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs index 24de07b36..0ef166b31 100644 --- a/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs +++ b/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using NLog; +using NzbDrone.Common.Eventing; using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Tv; @@ -14,7 +15,7 @@ namespace NzbDrone.Core.DataAugmentation.Scene string GetCleanName(int tvdbId); } - public class SceneMappingService : IInitializable, ISceneMappingService + public class SceneMappingService : ISceneMappingService,IHandleAsync { private readonly ISceneMappingRepository _repository; private readonly ISceneMappingProxy _sceneMappingProxy; @@ -84,7 +85,7 @@ namespace NzbDrone.Core.DataAugmentation.Scene return mapping.CleanTitle; } - public void Init() + public void HandleAsync(ApplicationStartedEvent message) { if (!_repository.HasItems()) { diff --git a/NzbDrone.Core/Indexers/IndexerService.cs b/NzbDrone.Core/Indexers/IndexerService.cs index 2d86e6201..452ddc82e 100644 --- a/NzbDrone.Core/Indexers/IndexerService.cs +++ b/NzbDrone.Core/Indexers/IndexerService.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Common.Eventing; using NzbDrone.Core.Lifecycle; @@ -14,7 +15,7 @@ namespace NzbDrone.Core.Indexers IndexerDefinition Get(string name); } - public class IndexerService : IIndexerService, IInitializable + public class IndexerService : IIndexerService, IHandle { private readonly IIndexerRepository _indexerRepository; private readonly Logger _logger; @@ -28,27 +29,6 @@ namespace NzbDrone.Core.Indexers _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 All() { @@ -72,5 +52,27 @@ namespace NzbDrone.Core.Indexers { 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); + } + } + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Indexers/Newznab/NewznabService.cs b/NzbDrone.Core/Indexers/Newznab/NewznabService.cs index 5f387ca25..1cbcdd828 100644 --- a/NzbDrone.Core/Indexers/Newznab/NewznabService.cs +++ b/NzbDrone.Core/Indexers/Newznab/NewznabService.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using NLog; +using NzbDrone.Common.Eventing; using NzbDrone.Core.Lifecycle; namespace NzbDrone.Core.Indexers.Newznab @@ -15,7 +16,7 @@ namespace NzbDrone.Core.Indexers.Newznab void Update(NewznabDefinition definition); } - public class NewznabService : INewznabService, IInitializable + public class NewznabService : INewznabService, IHandle { private readonly INewznabRepository _newznabRepository; 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 { diff --git a/NzbDrone.Core/Jobs/JobRepository.cs b/NzbDrone.Core/Jobs/JobRepository.cs index 66c268528..a2234ed56 100644 --- a/NzbDrone.Core/Jobs/JobRepository.cs +++ b/NzbDrone.Core/Jobs/JobRepository.cs @@ -2,18 +2,19 @@ using System; using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Common.Eventing; using NzbDrone.Core.Datastore; using NzbDrone.Core.Lifecycle; namespace NzbDrone.Core.Jobs { - public interface IJobRepository : IInitializable, IBasicRepository + public interface IJobRepository : IBasicRepository { IList GetPendingJobs(); JobDefinition GetDefinition(Type type); } - public class JobRepository : BasicRepository, IJobRepository + public class JobRepository : BasicRepository, IJobRepository, IHandle { private readonly IEnumerable _jobs; 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(); } - public void Init() + public void Handle(ApplicationStartedEvent message) { var currentJobs = All().ToList(); _logger.Debug("Initializing jobs. Available: {0} Existing:{1}", _jobs.Count(), currentJobs.Count()); @@ -57,10 +58,10 @@ namespace NzbDrone.Core.Jobs if (jobDefinition == null) { jobDefinition = new JobDefinition - { - Type = job.GetType().ToString(), - LastExecution = DateTime.Now - }; + { + Type = job.GetType().ToString(), + LastExecution = DateTime.Now + }; } jobDefinition.Enable = job.DefaultInterval.TotalSeconds > 0; diff --git a/NzbDrone.Core/Jobs/JobTimer.cs b/NzbDrone.Core/Jobs/JobTimer.cs index 4c39388d3..6300cbb3a 100644 --- a/NzbDrone.Core/Jobs/JobTimer.cs +++ b/NzbDrone.Core/Jobs/JobTimer.cs @@ -1,9 +1,10 @@ using System.Timers; +using NzbDrone.Common.Eventing; using NzbDrone.Core.Lifecycle; namespace NzbDrone.Core.Jobs { - public class JobTimer : IInitializable + public class JobTimer : IHandle { private readonly IJobController _jobController; 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.Elapsed += (o, args) => _jobController.EnqueueScheduled(); _timer.Start(); } - } } \ No newline at end of file diff --git a/NzbDrone.Core/Lifecycle/ApplicationStartedEvent.cs b/NzbDrone.Core/Lifecycle/ApplicationStartedEvent.cs new file mode 100644 index 000000000..9ace92305 --- /dev/null +++ b/NzbDrone.Core/Lifecycle/ApplicationStartedEvent.cs @@ -0,0 +1,10 @@ +using NzbDrone.Common.Eventing; + +namespace NzbDrone.Core.Lifecycle +{ + public class ApplicationStartedEvent : IEvent + { + + } + +} \ No newline at end of file diff --git a/NzbDrone.Core/Lifecycle/IInitializable.cs b/NzbDrone.Core/Lifecycle/IInitializable.cs deleted file mode 100644 index ef32c13ef..000000000 --- a/NzbDrone.Core/Lifecycle/IInitializable.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace NzbDrone.Core.Lifecycle -{ - public interface IInitializable - { - void Init(); - } -} \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index dbbd5a3dc..acd85a4c7 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -300,7 +300,7 @@ - + diff --git a/NzbDrone.Core/Qualities/QualityProfileService.cs b/NzbDrone.Core/Qualities/QualityProfileService.cs index b7b429db0..08cede134 100644 --- a/NzbDrone.Core/Qualities/QualityProfileService.cs +++ b/NzbDrone.Core/Qualities/QualityProfileService.cs @@ -1,9 +1,8 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Common.Eventing; using NzbDrone.Core.Lifecycle; -using NzbDrone.Core.Qualities; namespace NzbDrone.Core.Qualities @@ -17,7 +16,7 @@ namespace NzbDrone.Core.Qualities QualityProfile Get(int id); } - public class QualityProfileService : IQualityProfileService, IInitializable + public class QualityProfileService : IQualityProfileService, IHandle { private readonly IQualityProfileRepository _qualityProfileRepository; private readonly Logger _logger; @@ -54,6 +53,11 @@ namespace NzbDrone.Core.Qualities } public void Init() + { + + } + + public void Handle(ApplicationStartedEvent message) { if (All().Any()) return; diff --git a/NzbDrone.Core/Qualities/QualitySizeService.cs b/NzbDrone.Core/Qualities/QualitySizeService.cs index 18bee04c8..fe31a8314 100644 --- a/NzbDrone.Core/Qualities/QualitySizeService.cs +++ b/NzbDrone.Core/Qualities/QualitySizeService.cs @@ -1,8 +1,7 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; using NLog; +using NzbDrone.Common.Eventing; using NzbDrone.Core.Lifecycle; namespace NzbDrone.Core.Qualities @@ -15,7 +14,7 @@ namespace NzbDrone.Core.Qualities QualitySize Get(int qualityId); } - public class QualitySizeService : IQualitySizeService, IInitializable + public class QualitySizeService : IQualitySizeService, IHandle { private readonly IQualitySizeRepository _qualitySizeRepository; private readonly Logger _logger; @@ -47,6 +46,11 @@ namespace NzbDrone.Core.Qualities } public void Init() + { + + } + + public void Handle(ApplicationStartedEvent message) { var existing = All(); @@ -54,15 +58,15 @@ namespace NzbDrone.Core.Qualities 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 - { - QualityId = quality.Id, - Name = quality.Name, - MinSize = 0, - MaxSize = 100 - }); + { + QualityId = quality.Id, + Name = quality.Name, + MinSize = 0, + MaxSize = 100 + }); } } } diff --git a/NzbDrone.ncrunchsolution b/NzbDrone.ncrunchsolution index 6cb47a29a..444b34b1d 100644 --- a/NzbDrone.ncrunchsolution +++ b/NzbDrone.ncrunchsolution @@ -2,6 +2,7 @@ 1 False true + true UseDynamicAnalysis Disabled Disabled diff --git a/NzbDrone/CommonContainerExtentions.cs b/NzbDrone/CommonContainerExtentions.cs index ae46a3a78..954d3907a 100644 --- a/NzbDrone/CommonContainerExtentions.cs +++ b/NzbDrone/CommonContainerExtentions.cs @@ -36,7 +36,6 @@ namespace NzbDrone container.AutoRegisterImplementations(); container.AutoRegisterImplementations(); - container.AutoRegisterMultipleImplementations(); container.Register().AsSingleton(); container.Register().AsSingleton();