2010-09-23 05:19:47 +02:00
|
|
|
|
using System;
|
2010-10-17 19:22:48 +02:00
|
|
|
|
using System.Diagnostics;
|
2011-11-14 01:22:18 +01:00
|
|
|
|
using System.IO;
|
2011-06-18 03:46:22 +02:00
|
|
|
|
using System.Linq;
|
2010-09-23 05:19:47 +02:00
|
|
|
|
using Ninject;
|
2011-04-10 04:44:01 +02:00
|
|
|
|
using NLog;
|
2011-11-13 08:27:16 +01:00
|
|
|
|
using NzbDrone.Common;
|
2011-05-24 01:29:14 +02:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
2010-10-24 09:46:58 +02:00
|
|
|
|
using NzbDrone.Core.Instrumentation;
|
2011-12-02 02:33:17 +01:00
|
|
|
|
using NzbDrone.Core.Jobs;
|
2010-09-28 06:25:41 +02:00
|
|
|
|
using NzbDrone.Core.Providers;
|
2011-04-29 08:06:13 +02:00
|
|
|
|
using NzbDrone.Core.Providers.ExternalNotification;
|
2011-04-19 02:12:06 +02:00
|
|
|
|
using NzbDrone.Core.Providers.Indexer;
|
2011-06-15 04:31:41 +02:00
|
|
|
|
using PetaPoco;
|
2010-09-23 05:19:47 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core
|
|
|
|
|
{
|
2011-11-08 21:12:54 +01:00
|
|
|
|
public class CentralDispatch
|
2010-09-23 05:19:47 +02:00
|
|
|
|
{
|
2010-10-10 21:00:07 +02:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2010-10-08 00:17:24 +02:00
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
public StandardKernel Kernel { get; private set; }
|
|
|
|
|
|
2011-11-08 21:12:54 +01:00
|
|
|
|
public CentralDispatch()
|
2011-04-10 04:44:01 +02:00
|
|
|
|
{
|
2011-11-14 01:22:18 +01:00
|
|
|
|
Logger.Debug("Initializing Kernel:");
|
|
|
|
|
Kernel = new StandardKernel();
|
2011-04-10 04:44:01 +02:00
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
InitDatabase();
|
2011-11-08 21:12:54 +01:00
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
InitQuality();
|
|
|
|
|
InitExternalNotifications();
|
|
|
|
|
InitIndexers();
|
|
|
|
|
InitJobs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitDatabase()
|
2011-05-24 01:29:14 +02:00
|
|
|
|
{
|
2011-11-14 01:22:18 +01:00
|
|
|
|
Logger.Info("Initializing Database...");
|
2011-05-27 04:12:28 +02:00
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
var appDataPath = new EnviromentProvider().GetAppDataPath();
|
|
|
|
|
if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath);
|
2011-10-12 04:24:43 +02:00
|
|
|
|
|
2011-11-14 01:22:18 +01: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-23 06:58:26 +01:00
|
|
|
|
Kernel.Bind<IDatabase>().ToMethod(c => connection.GetLogPetaPocoDb()).WhenInjectedInto<LogProvider>();
|
|
|
|
|
Kernel.Bind<LogDbContext>().ToMethod(c => connection.GetLogEfContext()).WhenInjectedInto<LogProvider>().InSingletonScope();
|
2011-05-27 04:12:28 +02:00
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
Kernel.Get<DatabaseTarget>().Register();
|
2011-11-13 19:16:31 +01:00
|
|
|
|
LogConfiguration.Reload();
|
2011-05-24 01:29:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
private void InitQuality()
|
2010-09-23 05:19:47 +02:00
|
|
|
|
{
|
2011-11-14 01:22:18 +01:00
|
|
|
|
Logger.Info("Initializing Quality...");
|
|
|
|
|
Kernel.Get<QualityProvider>().SetupDefaultProfiles();
|
|
|
|
|
Kernel.Get<QualityTypeProvider>().SetupDefault();
|
2010-09-23 05:19:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
private void InitIndexers()
|
2011-04-19 02:12:06 +02:00
|
|
|
|
{
|
2011-11-14 01:22:18 +01:00
|
|
|
|
Logger.Info("Initializing Indexers...");
|
2011-11-08 21:12:54 +01:00
|
|
|
|
Kernel.Bind<IndexerBase>().To<NzbsOrg>();
|
|
|
|
|
Kernel.Bind<IndexerBase>().To<NzbMatrix>();
|
|
|
|
|
Kernel.Bind<IndexerBase>().To<NzbsRUs>();
|
|
|
|
|
Kernel.Bind<IndexerBase>().To<Newzbin>();
|
2011-11-13 21:51:15 +01:00
|
|
|
|
Kernel.Bind<IndexerBase>().To<Newznab>();
|
2011-05-27 08:03:57 +02:00
|
|
|
|
|
2011-11-08 21:12:54 +01:00
|
|
|
|
var indexers = Kernel.GetAll<IndexerBase>();
|
|
|
|
|
Kernel.Get<IndexerProvider>().InitializeIndexers(indexers.ToList());
|
2011-04-19 02:12:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
private void InitJobs()
|
2011-04-20 04:17:28 +02:00
|
|
|
|
{
|
2011-11-14 01:22:18 +01:00
|
|
|
|
Logger.Info("Initializing Background Jobs...");
|
|
|
|
|
|
|
|
|
|
Kernel.Bind<JobProvider>().ToSelf().InSingletonScope();
|
|
|
|
|
|
2011-11-08 21:12:54 +01: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<RenameEpisodeJob>().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 03:54:09 +01:00
|
|
|
|
Kernel.Bind<IJob>().To<AppUpdateJob>().InSingletonScope();
|
2011-11-22 08:35:11 +01:00
|
|
|
|
Kernel.Bind<IJob>().To<TrimLogsJob>().InSingletonScope();
|
2011-11-24 02:09:09 +01:00
|
|
|
|
Kernel.Bind<IJob>().To<RecentBacklogSearchJob>().InSingletonScope();
|
2011-11-08 21:12:54 +01:00
|
|
|
|
|
|
|
|
|
Kernel.Get<JobProvider>().Initialize();
|
|
|
|
|
Kernel.Get<WebTimer>().StartTimer(30);
|
2011-04-20 04:17:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
private void InitExternalNotifications()
|
2011-04-29 08:06:13 +02:00
|
|
|
|
{
|
2011-11-14 01:22:18 +01:00
|
|
|
|
Logger.Info("Initializing External Notifications...");
|
2011-11-10 05:14:19 +01:00
|
|
|
|
Kernel.Bind<ExternalNotificationBase>().To<Xbmc>();
|
2011-11-08 21:12:54 +01:00
|
|
|
|
Kernel.Bind<ExternalNotificationBase>().To<Smtp>();
|
|
|
|
|
Kernel.Bind<ExternalNotificationBase>().To<Twitter>();
|
|
|
|
|
Kernel.Bind<ExternalNotificationBase>().To<Providers.ExternalNotification.Growl>();
|
2011-11-10 05:14:19 +01:00
|
|
|
|
Kernel.Bind<ExternalNotificationBase>().To<Prowl>();
|
2011-07-28 09:21:49 +02:00
|
|
|
|
|
2011-11-08 21:12:54 +01:00
|
|
|
|
var notifiers = Kernel.GetAll<ExternalNotificationBase>();
|
|
|
|
|
Kernel.Get<ExternalNotificationProvider>().InitializeNotifiers(notifiers.ToList());
|
2011-04-29 08:06:13 +02:00
|
|
|
|
}
|
2011-04-22 04:23:31 +02:00
|
|
|
|
|
2011-11-08 21:12:54 +01:00
|
|
|
|
public void DedicateToHost()
|
2010-10-17 19:22:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2011-11-13 21:31:02 +01:00
|
|
|
|
var pid = new EnviromentProvider().NzbDroneProcessIdFromEnviroment;
|
2011-06-13 05:45:22 +02:00
|
|
|
|
|
|
|
|
|
Logger.Debug("Attaching to parent process ({0}) for automatic termination.", pid);
|
|
|
|
|
|
|
|
|
|
var hostProcess = Process.GetProcessById(Convert.ToInt32(pid));
|
2010-10-17 19:22:48 +02:00
|
|
|
|
|
|
|
|
|
hostProcess.EnableRaisingEvents = true;
|
|
|
|
|
hostProcess.Exited += (delegate
|
2011-04-10 04:44:01 +02:00
|
|
|
|
{
|
|
|
|
|
Logger.Info("Host has been terminated. Shutting down web server.");
|
|
|
|
|
ShutDown();
|
|
|
|
|
});
|
2010-10-17 19:22:48 +02:00
|
|
|
|
|
2011-06-13 05:45:22 +02:00
|
|
|
|
Logger.Debug("Successfully Attached to host. Process [{0}]", hostProcess.ProcessName);
|
2010-10-17 19:22:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2011-11-13 21:42:12 +01:00
|
|
|
|
Logger.FatalException("An error has occurred while dedicating to host.", e);
|
2010-10-17 19:22:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ShutDown()
|
|
|
|
|
{
|
2011-11-14 01:22:18 +01:00
|
|
|
|
Logger.Info("Shutting down application...");
|
2011-11-07 07:26:21 +01:00
|
|
|
|
WebTimer.Stop();
|
2010-10-17 19:22:48 +02:00
|
|
|
|
Process.GetCurrentProcess().Kill();
|
|
|
|
|
}
|
2010-09-23 05:19:47 +02:00
|
|
|
|
}
|
2011-11-10 05:14:19 +01:00
|
|
|
|
}
|