using System; using System.Diagnostics; using System.IO; using System.Linq; using DeskMetrics; using Ninject; using NLog; using NzbDrone.Common; 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; using SignalR; using SignalR.Hosting.AspNet; using SignalR.Infrastructure; using SignalR.Ninject; using Connection = NzbDrone.Core.Datastore.Connection; 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(); var resolver = new NinjectDependencyResolver(Kernel); AspNetHost.SetResolver(resolver); InitDatabase(); InitReporting(); 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 InitReporting() { EnviromentProvider.UGuid = Kernel.Get().UGuid; ReportingService.RestProvider = Kernel.Get(); var appId = AnalyticsProvider.DESKMETRICS_TEST_ID; if (EnviromentProvider.IsProduction) appId = AnalyticsProvider.DESKMETRICS_PRODUCTION_ID; var deskMetricsClient = new DeskMetricsClient(Kernel.Get().UGuid.ToString(), appId, _enviromentProvider.Version); Kernel.Bind().ToConstant(deskMetricsClient); Kernel.Get().Checkpoint(); } private void InitQuality() { logger.Debug("Initializing Quality..."); Kernel.Get().SetupDefaultProfiles(); Kernel.Get().SetupDefault(); } private void InitIndexers() { logger.Debug("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.Debug("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.Get().Initialize(); Kernel.Get().StartTimer(30); } private void InitExternalNotifications() { logger.Debug("Initializing External Notifications..."); Kernel.Bind().To(); 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(); } } }