using System; using System.Collections.Generic; 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 NzbDrone.Core.Repository; 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 EnvironmentProvider _environmentProvider; public StandardKernel Kernel { get; private set; } public CentralDispatch() { _environmentProvider = new EnvironmentProvider(); 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 = _environmentProvider.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() { EnvironmentProvider.UGuid = Kernel.Get().UGuid; ReportingService.RestProvider = Kernel.Get(); ReportingService.SetupExceptronDriver(); var appId = AnalyticsProvider.DESKMETRICS_TEST_ID; if (EnvironmentProvider.IsProduction) appId = AnalyticsProvider.DESKMETRICS_PRODUCTION_ID; var deskMetricsClient = new DeskMetricsClient(Kernel.Get().UGuid.ToString(), appId, _environmentProvider.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(); Kernel.Bind().To(); Kernel.Bind().To(); Kernel.Bind().To(); var indexers = Kernel.GetAll(); Kernel.Get().InitializeIndexers(indexers.ToList()); var newznabIndexers = new List { new NewznabDefinition { Enable = false, Name = "Nzbs.org", Url = "https://nzbs.org", BuiltIn = true }, new NewznabDefinition { Enable = false, Name = "Nzb.su", Url = "https://nzb.su", BuiltIn = true } }; Kernel.Get().InitializeNewznabIndexers(newznabIndexers); } 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 = _environmentProvider.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(); } } }