1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00
Sonarr/NzbDrone.Core/CentralDispatch.cs

203 lines
8.5 KiB
C#
Raw Normal View History

2010-09-23 06:19:47 +03:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
2011-06-18 04:46:22 +03:00
using System.Linq;
2012-01-25 06:09:49 +03:00
using DeskMetrics;
2010-09-23 06:19:47 +03:00
using Ninject;
2011-04-10 05:44:01 +03:00
using NLog;
2011-11-13 10:27:16 +03:00
using NzbDrone.Common;
using NzbDrone.Core.Instrumentation;
2011-12-02 04:33:17 +03:00
using NzbDrone.Core.Jobs;
using NzbDrone.Core.Providers;
2012-01-25 06:09:49 +03:00
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Providers.ExternalNotification;
using NzbDrone.Core.Providers.Indexer;
2012-07-13 00:48:09 +03:00
using NzbDrone.Core.Providers.Metadata;
using NzbDrone.Core.Repository;
2011-06-15 05:31:41 +03:00
using PetaPoco;
using SignalR;
using SignalR.Hosting.AspNet;
using SignalR.Infrastructure;
using SignalR.Ninject;
using Connection = NzbDrone.Core.Datastore.Connection;
2012-07-13 00:48:09 +03:00
using Xbmc = NzbDrone.Core.Providers.ExternalNotification.Xbmc;
2010-09-23 06:19:47 +03:00
namespace NzbDrone.Core
{
2011-11-08 23:12:54 +03:00
public class CentralDispatch
2010-09-23 06:19:47 +03:00
{
2012-02-13 09:38:57 +03:00
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private readonly EnvironmentProvider _environmentProvider;
2010-10-08 01:17:24 +03:00
public StandardKernel Kernel { get; private set; }
2011-11-08 23:12:54 +03:00
public CentralDispatch()
2011-04-10 05:44:01 +03:00
{
_environmentProvider = new EnvironmentProvider();
2012-01-26 04:29:55 +03:00
2012-02-13 09:38:57 +03:00
logger.Debug("Initializing Kernel:");
Kernel = new StandardKernel();
2011-04-10 05:44:01 +03:00
var resolver = new NinjectDependencyResolver(Kernel);
AspNetHost.SetResolver(resolver);
InitDatabase();
InitReporting();
2011-11-08 23:12:54 +03:00
InitQuality();
InitExternalNotifications();
2012-07-13 00:48:09 +03:00
InitMetadataProviders();
InitIndexers();
2012-07-13 00:48:09 +03:00
InitJobs();
}
2012-01-25 06:09:49 +03:00
private void InitDatabase()
2011-05-24 02:29:14 +03:00
{
2012-02-13 09:38:57 +03:00
logger.Info("Initializing Database...");
var appDataPath = _environmentProvider.GetAppDataPath();
if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath);
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>();
Kernel.Bind<LogDbContext>().ToMethod(c => connection.GetLogEfContext()).WhenInjectedInto<LogProvider>().InSingletonScope();
Kernel.Get<DatabaseTarget>().Register();
2011-11-13 21:16:31 +03:00
LogConfiguration.Reload();
2011-05-24 02:29:14 +03:00
}
private void InitReporting()
2012-01-25 06:09:49 +03:00
{
EnvironmentProvider.UGuid = Kernel.Get<ConfigProvider>().UGuid;
ReportingService.RestProvider = Kernel.Get<RestProvider>();
2012-04-30 04:24:24 +03:00
ReportingService.SetupExceptronDriver();
2012-01-25 06:09:49 +03:00
}
private void InitQuality()
2010-09-23 06:19:47 +03:00
{
2012-02-13 09:38:57 +03:00
logger.Debug("Initializing Quality...");
Kernel.Get<QualityProvider>().SetupDefaultProfiles();
Kernel.Get<QualityTypeProvider>().SetupDefault();
2010-09-23 06:19:47 +03:00
}
private void InitIndexers()
{
2012-02-13 09:38:57 +03:00
logger.Debug("Initializing Indexers...");
2011-11-08 23:12:54 +03:00
Kernel.Bind<IndexerBase>().To<NzbMatrix>();
Kernel.Bind<IndexerBase>().To<NzbsRUs>();
Kernel.Bind<IndexerBase>().To<Newzbin>();
Kernel.Bind<IndexerBase>().To<Newznab>();
Kernel.Bind<IndexerBase>().To<Wombles>();
Kernel.Bind<IndexerBase>().To<FileSharingTalk>();
Kernel.Bind<IndexerBase>().To<NzbIndex>();
2012-04-15 02:54:54 +03:00
Kernel.Bind<IndexerBase>().To<NzbClub>();
2011-05-27 09:03:57 +03:00
2011-11-08 23:12:54 +03:00
var indexers = Kernel.GetAll<IndexerBase>();
Kernel.Get<IndexerProvider>().InitializeIndexers(indexers.ToList());
var newznabIndexers = new List<NewznabDefinition>
{
new NewznabDefinition { Enable = false, Name = "Nzbs.org", Url = "http://nzbs.org", BuiltIn = true },
new NewznabDefinition { Enable = false, Name = "Nzb.su", Url = "https://nzb.su", BuiltIn = true },
new NewznabDefinition { Enable = false, Name = "Dognzb.cr", Url = "https://dognzb.cr", BuiltIn = true }
};
Kernel.Get<NewznabProvider>().InitializeNewznabIndexers(newznabIndexers);
}
private void InitJobs()
2011-04-20 05:17:28 +03:00
{
2012-02-13 09:38:57 +03:00
logger.Debug("Initializing Background Jobs...");
Kernel.Bind<JobProvider>().ToSelf().InSingletonScope();
2011-11-08 23:12:54 +03:00
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<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();
2011-11-14 05:54:09 +03:00
Kernel.Bind<IJob>().To<AppUpdateJob>().InSingletonScope();
Kernel.Bind<IJob>().To<TrimLogsJob>().InSingletonScope();
Kernel.Bind<IJob>().To<RecentBacklogSearchJob>().InSingletonScope();
2012-06-13 10:18:10 +03:00
Kernel.Bind<IJob>().To<SearchHistoryCleanupJob>().InSingletonScope();
Kernel.Bind<IJob>().To<PastWeekBacklogSearchJob>().InSingletonScope();
2012-07-15 07:34:01 +03:00
Kernel.Bind<IJob>().To<RefreshEpisodeMetadata>().InSingletonScope();
2012-09-04 10:11:15 +03:00
Kernel.Bind<IJob>().To<CleanupRecycleBinJob>().InSingletonScope();
Kernel.Bind<IJob>().To<EmptyRecycleBinJob>().InSingletonScope();
Kernel.Bind<IJob>().To<XemUpdateJob>().InSingletonScope();
2012-02-13 10:49:53 +03:00
2011-11-08 23:12:54 +03:00
Kernel.Get<JobProvider>().Initialize();
Kernel.Get<WebTimer>().StartTimer(30);
2011-04-20 05:17:28 +03:00
}
private void InitExternalNotifications()
{
logger.Debug("Initializing External Notifications...");
Kernel.Bind<ExternalNotificationBase>().To<Xbmc>();
2011-11-08 23:12:54 +03:00
Kernel.Bind<ExternalNotificationBase>().To<Smtp>();
Kernel.Bind<ExternalNotificationBase>().To<Twitter>();
Kernel.Bind<ExternalNotificationBase>().To<Providers.ExternalNotification.Growl>();
Kernel.Bind<ExternalNotificationBase>().To<Prowl>();
Kernel.Bind<ExternalNotificationBase>().To<Plex>();
2011-11-08 23:12:54 +03:00
var notifiers = Kernel.GetAll<ExternalNotificationBase>();
Kernel.Get<ExternalNotificationProvider>().InitializeNotifiers(notifiers.ToList());
}
2012-07-13 00:48:09 +03:00
private void InitMetadataProviders()
{
logger.Debug("Initializing Metadata Providers...");
Kernel.Bind<MetadataBase>().To<Providers.Metadata.Xbmc>().InSingletonScope();
var providers = Kernel.GetAll<MetadataBase>();
Kernel.Get<MetadataProvider>().Initialize(providers.ToList());
}
2011-11-08 23:12:54 +03:00
public void DedicateToHost()
{
try
{
var pid = _environmentProvider.NzbDroneProcessIdFromEnviroment;
2012-02-13 09:38:57 +03:00
logger.Debug("Attaching to parent process ({0}) for automatic termination.", pid);
var hostProcess = Process.GetProcessById(Convert.ToInt32(pid));
hostProcess.EnableRaisingEvents = true;
hostProcess.Exited += (delegate
2011-04-10 05:44:01 +03:00
{
2012-02-13 09:38:57 +03:00
logger.Info("Host has been terminated. Shutting down web server.");
2011-04-10 05:44:01 +03:00
ShutDown();
});
2012-02-13 09:38:57 +03:00
logger.Debug("Successfully Attached to host. Process [{0}]", hostProcess.ProcessName);
}
catch (Exception e)
{
2012-02-13 09:38:57 +03:00
logger.FatalException("An error has occurred while dedicating to host.", e);
}
}
private static void ShutDown()
{
2012-02-13 09:38:57 +03:00
logger.Info("Shutting down application...");
WebTimer.Stop();
Process.GetCurrentProcess().Kill();
}
2010-09-23 06:19:47 +03:00
}
}