using System; using System.Diagnostics; using System.Linq; using Ninject; using NLog; using NzbDrone.Core.Datastore; using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Providers; using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Providers.ExternalNotification; using NzbDrone.Core.Providers.Indexer; using NzbDrone.Core.Providers.Jobs; using PetaPoco; namespace NzbDrone.Core { public class CentralDispatch { private readonly Object KernelLock = new object(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public CentralDispatch() { InitializeApp(); } public StandardKernel Kernel { get; private set; } private void InitializeApp() { BindKernel(); Kernel.Get().Setup(); var mainConnectionString = Kernel.Get().MainConnectionString; MigrationsHelper.Run(mainConnectionString, true); LogConfiguration.RegisterDatabaseLogger(Kernel.Get()); Kernel.Get().SetupDefaultProfiles(); Kernel.Get().SetupDefault(); Kernel.Get().CreateDefaultConfigFile(); BindExternalNotifications(); BindIndexers(); BindJobs(); } private void BindKernel() { lock (KernelLock) { Logger.Debug("Binding Ninject's Kernel"); Kernel = new StandardKernel(); 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.Bind().ToSelf().InSingletonScope(); } } private void BindIndexers() { Kernel.Bind().To(); Kernel.Bind().To(); Kernel.Bind().To(); Kernel.Bind().To(); var indexers = Kernel.GetAll(); Kernel.Get().InitializeIndexers(indexers.ToList()); } private void BindJobs() { 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 BindExternalNotifications() { Kernel.Bind().To(); Kernel.Bind().To(); Kernel.Bind().To(); var notifiers = Kernel.GetAll(); Kernel.Get().InitializeNotifiers(notifiers.ToList()); } /// /// Forces IISExpress process to exit with the host application /// public void DedicateToHost() { try { var pid = Convert.ToInt32(Environment.GetEnvironmentVariable("NZBDRONE_PID")); 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.Fatal(e); } } private static void ShutDown() { Logger.Info("Shutting down application."); WebTimer.Stop(); Process.GetCurrentProcess().Kill(); } } }