using System; using System.Diagnostics; using System.IO; using System.Linq; using Ninject; using NLog; using NzbDrone.Common; using NzbDrone.Core.Datastore; using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Providers; using NzbDrone.Core.Providers.ExternalNotification; using NzbDrone.Core.Providers.Indexer; using NzbDrone.Core.Providers.Jobs; using PetaPoco; namespace NzbDrone.Core { public class CentralDispatch { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public StandardKernel Kernel { get; private set; } public CentralDispatch() { Logger.Debug("Initializing Kernel:"); Kernel = new StandardKernel(); InitDatabase(); InitQuality(); InitExternalNotifications(); InitIndexers(); InitJobs(); } private void InitDatabase() { Logger.Info("Initializing Database..."); var appDataPath = new 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().InSingletonScope(); Kernel.Get().Register(); LogConfiguration.Reload(); } 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.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 = new 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(); } } }