using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using Autofac; using Autofac.Core; using DeskMetrics; 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.Providers.Metadata; using NzbDrone.Core.Repository; using PetaPoco; using SignalR; using SignalR.Hosting.AspNet; using SignalR.Infrastructure; using Connection = NzbDrone.Core.Datastore.Connection; using Xbmc = NzbDrone.Core.Providers.ExternalNotification.Xbmc; namespace NzbDrone.Core { public class CentralDispatch { private static readonly Logger logger = LogManager.GetCurrentClassLogger(); private readonly EnvironmentProvider _environmentProvider; public ContainerBuilder ContainerBuilder { get; private set; } public CentralDispatch() { _environmentProvider = new EnvironmentProvider(); logger.Debug("Initializing ContainerBuilder:"); ContainerBuilder = new ContainerBuilder(); ContainerBuilder.RegisterAssemblyTypes(typeof(DiskProvider).Assembly).SingleInstance(); ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly).SingleInstance(); ContainerBuilder.RegisterType(); InitDatabase(); RegisterExternalNotifications(); RegisterMetadataProviders(); RegisterIndexers(); RegisterJobs(); } private void InitDatabase() { logger.Info("Registering Database..."); var appDataPath = _environmentProvider.GetAppDataPath(); if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath); ContainerBuilder.Register(c => c.Resolve().GetMainPetaPocoDb()) .As(); ContainerBuilder.Register(c => c.Resolve().GetLogPetaPocoDb(false)) .SingleInstance() .Named("DatabaseTarget"); ContainerBuilder.Register(c => c.Resolve().GetLogPetaPocoDb()) .Named("LogProvider"); ContainerBuilder.Register(c => c.Resolve().GetLogEfContext()) .As() .SingleInstance(); ContainerBuilder.RegisterType().WithParameter(ResolvedParameter.ForNamed("DatabaseTarget")); ContainerBuilder.RegisterType().WithParameter(ResolvedParameter.ForNamed("LogProvider")); } private void RegisterIndexers() { logger.Debug("Registering Indexers..."); ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) .Where(t => t.BaseType == typeof(IndexerBase)) .As(); } private void RegisterJobs() { logger.Debug("Registering Background Jobs..."); ContainerBuilder.RegisterType().SingleInstance(); ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) .Where(t => t.GetInterfaces().Contains(typeof(IJob))) .As() .SingleInstance(); } private void RegisterExternalNotifications() { logger.Debug("Registering External Notifications..."); ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) .Where(t => t.BaseType == typeof(ExternalNotificationBase)) .As(); //ContainerBuilder.RegisterType().As().SingleInstance(); //ContainerBuilder.RegisterType().As().SingleInstance(); //ContainerBuilder.RegisterType().As().SingleInstance(); //ContainerBuilder.RegisterType().As().SingleInstance(); //ContainerBuilder.RegisterType().As().SingleInstance(); //ContainerBuilder.RegisterType().As().SingleInstance(); } private void RegisterMetadataProviders() { logger.Debug("Registering Metadata Providers..."); ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) .Where(t => t.IsSubclassOf(typeof(MetadataBase))) .As(); //ContainerBuilder.RegisterType().As().SingleInstance(); } private void RegisterReporting(IContainer container) { EnvironmentProvider.UGuid = container.Resolve().UGuid; ReportingService.RestProvider = container.Resolve(); ReportingService.SetupExceptronDriver(); } private void RegisterQuality(IContainer container) { logger.Debug("Initializing Quality..."); container.Resolve().SetupDefaultProfiles(); container.Resolve().SetupDefault(); } 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); } } public IContainer BuildContainer() { var container = ContainerBuilder.Build(); logger.Debug("Initializing Components"); container.Resolve().Register(); LogConfiguration.Reload(); RegisterReporting(container); RegisterQuality(container); var indexers = container.Resolve>(); container.Resolve().InitializeIndexers(indexers.ToList()); var newznabIndexers = new List { new NewznabDefinition { Enable = false, Name = "Nzbs.org", Url = "http://nzbs.org", BuiltIn = true }, new NewznabDefinition { Enable = false, Name = "Nzb.su", Url = "https://nzb.su", BuiltIn = true }, new NewznabDefinition { Enable = false, Name = "Dognzb.cr", Url = "https://dognzb.cr", BuiltIn = true } }; container.Resolve().InitializeNewznabIndexers(newznabIndexers); container.Resolve().Initialize(); container.Resolve().StartTimer(30); var notifiers = container.Resolve>(); container.Resolve().InitializeNotifiers(notifiers.ToList()); var providers = container.Resolve>(); container.Resolve().Initialize(providers.ToList()); //SignalR GlobalHost.DependencyResolver = new AutofacSignalrDependencyResolver(container.BeginLifetimeScope("SignalR")); return container; } private static void ShutDown() { logger.Info("Shutting down application..."); WebTimer.Stop(); Process.GetCurrentProcess().Kill(); } } }