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