using System; using System.Diagnostics; using System.IO; using System.Linq; using DeskMetrics; using Ninject; using NLog; using NzbDrone.Common; using NzbDrone.Core.Datastore; using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Jobs; using NzbDrone.Core.Providers; using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Providers.ExternalNotification; using NzbDrone.Core.Providers.Indexer; using PetaPoco; namespace NzbDrone.Core { public class CentralDispatch { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly EnviromentProvider _enviromentProvider; public StandardKernel Kernel { get; private set; } public CentralDispatch() { _enviromentProvider = new EnviromentProvider(); Logger.Debug("Initializing Kernel:"); Kernel = new StandardKernel(); InitDatabase(); InitAnalytics(); InitQuality(); InitExternalNotifications(); InitIndexers(); InitJobs(); } private void InitDatabase() { Logger.Info("Initializing Database..."); var appDataPath = _enviromentProvider.GetAppDataPath(); if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath); var connection = Kernel.Get(); Kernel.Bind().ToMethod(c => connection.GetMainPetaPocoDb()).InTransientScope(); Kernel.Bind().ToMethod(c => connection.GetLogPetaPocoDb(false)).WhenInjectedInto().InSingletonScope(); Kernel.Bind().ToMethod(c => connection.GetLogPetaPocoDb()).WhenInjectedInto(); Kernel.Bind().ToMethod(c => connection.GetLogEfContext()).WhenInjectedInto().InSingletonScope(); Kernel.Get().Register(); LogConfiguration.Reload(); } private void InitAnalytics() { var appId = AnalyticsProvider.DESKMETRICS_TEST_ID; if (EnviromentProvider.IsProduction) appId = AnalyticsProvider.DESKMETRICS_PRODUCTION_ID; var deskMetricsClient = new DeskMetricsClient(Kernel.Get().UGuid, appId, _enviromentProvider.Version); Kernel.Bind().ToConstant(deskMetricsClient); Kernel.Get().Checkpoint(); } private void InitQuality() { Logger.Info("Initializing Quality..."); Kernel.Get().SetupDefaultProfiles(); Kernel.Get().SetupDefault(); } private void InitIndexers() { Logger.Info("Initializing Indexers..."); Kernel.Bind().To(); Kernel.Bind().To(); Kernel.Bind().To(); Kernel.Bind().To(); Kernel.Bind().To(); var indexers = Kernel.GetAll(); Kernel.Get().InitializeIndexers(indexers.ToList()); } private void InitJobs() { Logger.Info("Initializing Background Jobs..."); Kernel.Bind().ToSelf().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Get().Initialize(); Kernel.Get().StartTimer(30); } private void InitExternalNotifications() { Logger.Info("Initializing External Notifications..."); Kernel.Bind().To(); Kernel.Bind().To(); Kernel.Bind().To(); Kernel.Bind().To(); Kernel.Bind().To(); var notifiers = Kernel.GetAll(); Kernel.Get().InitializeNotifiers(notifiers.ToList()); } public void DedicateToHost() { try { var pid = _enviromentProvider.NzbDroneProcessIdFromEnviroment; Logger.Debug("Attaching to parent process ({0}) for automatic termination.", pid); var hostProcess = Process.GetProcessById(Convert.ToInt32(pid)); hostProcess.EnableRaisingEvents = true; hostProcess.Exited += (delegate { Logger.Info("Host has been terminated. Shutting down web server."); ShutDown(); }); Logger.Debug("Successfully Attached to host. Process [{0}]", hostProcess.ProcessName); } catch (Exception e) { Logger.FatalException("An error has occurred while dedicating to host.", e); } } private static void ShutDown() { Logger.Info("Shutting down application..."); WebTimer.Stop(); Process.GetCurrentProcess().Kill(); } } }