1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-30 15:32:31 +01:00

replaced IInitializable interface with ApplicationStartedEvent

This commit is contained in:
Keivan Beigi 2013-04-09 17:47:04 -07:00
parent 5d1026ded2
commit a8a64a42b5
14 changed files with 92 additions and 90 deletions

View File

@ -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<IEventAggregator>().Publish(new ApplicationStartedEvent());
ApplicationPipelines.OnError.AddItemToEndOfPipeline(container.Resolve<ErrorPipeline>().HandleException);
}
@ -42,26 +41,6 @@ namespace NzbDrone.Api
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()
{
return _tinyIoCContainer;

View File

@ -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<IJob> fakeJobs = new List<IJob> { newJob };
Mocker.SetConstant(fakeJobs);
Subject.Init();
Initialize();
AllStoredModels.Should().HaveCount(1);
@ -128,7 +135,7 @@ namespace NzbDrone.Core.Test.JobTests
IEnumerable<IJob> fakeJobs = new List<IJob> { _disabledJob };
Mocker.SetConstant(fakeJobs);
Subject.Init();
Initialize();
Storage.All().Should().HaveCount(1);

View File

@ -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<ApplicationStartedEvent>
{
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())
{

View File

@ -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<ApplicationStartedEvent>
{
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<IndexerDefinition> 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);
}
}
}
}
}

View File

@ -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<ApplicationStartedEvent>
{
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<NewznabDefinition>
{

View File

@ -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<JobDefinition>
public interface IJobRepository : IBasicRepository<JobDefinition>
{
IList<JobDefinition> GetPendingJobs();
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 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;

View File

@ -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<ApplicationStartedEvent>
{
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();
}
}
}

View File

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

View File

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

View File

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

View File

@ -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<ApplicationStartedEvent>
{
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;

View File

@ -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<ApplicationStartedEvent>
{
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
});
}
}
}

View File

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

View File

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