You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Radarr/NzbDrone.Core/CentralDispatch.cs

195 lines
7.4 KiB

14 years ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Autofac;
using Autofac.Core;
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;
14 years ago
using PetaPoco;
using SignalR;
using Connection = NzbDrone.Core.Datastore.Connection;
14 years ago
namespace NzbDrone.Core
{
public class CentralDispatch
14 years ago
{
13 years ago
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<EnvironmentProvider>();
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<Connection>().GetMainPetaPocoDb())
.As<IDatabase>();
ContainerBuilder.Register(c => c.Resolve<Connection>().GetLogPetaPocoDb(false))
.SingleInstance()
.Named<IDatabase>("DatabaseTarget");
ContainerBuilder.Register(c => c.Resolve<Connection>().GetLogPetaPocoDb())
.Named<IDatabase>("LogProvider");
ContainerBuilder.RegisterType<DatabaseTarget>().WithParameter(ResolvedParameter.ForNamed<IDatabase>("DatabaseTarget"));
ContainerBuilder.RegisterType<LogProvider>().WithParameter(ResolvedParameter.ForNamed<IDatabase>("LogProvider"));
}
private void RegisterIndexers()
14 years ago
{
logger.Debug("Registering Indexers...");
ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly)
.Where(t => t.BaseType == typeof(IndexerBase))
.As<IndexerBase>();
14 years ago
}
private void RegisterJobs()
{
logger.Debug("Registering Background Jobs...");
ContainerBuilder.RegisterType<JobProvider>().SingleInstance();
ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly)
.Where(t => t.GetInterfaces().Contains(typeof(IJob)))
.As<IJob>()
.SingleInstance();
}
private void RegisterExternalNotifications()
{
logger.Debug("Registering External Notifications...");
ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly)
.Where(t => t.BaseType == typeof(ExternalNotificationBase))
.As<ExternalNotificationBase>();
}
private void RegisterMetadataProviders()
{
logger.Debug("Registering Metadata Providers...");
ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly)
.Where(t => t.IsSubclassOf(typeof(MetadataBase)))
.As<MetadataBase>();
}
private void RegisterReporting(IContainer container)
{
EnvironmentProvider.UGuid = container.Resolve<ConfigProvider>().UGuid;
ReportingService.RestProvider = container.Resolve<RestProvider>();
ReportingService.SetupExceptronDriver();
}
private void RegisterQuality(IContainer container)
{
logger.Debug("Initializing Quality...");
container.Resolve<QualityProvider>().SetupDefaultProfiles();
container.Resolve<QualityTypeProvider>().SetupDefault();
}
public void DedicateToHost()
{
try
{
var pid = _environmentProvider.NzbDroneProcessIdFromEnviroment;
13 years ago
logger.Debug("Attaching to parent process ({0}) for automatic termination.", pid);
var hostProcess = Process.GetProcessById(Convert.ToInt32(pid));
hostProcess.EnableRaisingEvents = true;
hostProcess.Exited += (delegate
{
13 years ago
logger.Info("Host has been terminated. Shutting down web server.");
ShutDown();
});
13 years ago
logger.Debug("Successfully Attached to host. Process [{0}]", hostProcess.ProcessName);
}
catch (Exception e)
{
13 years ago
logger.FatalException("An error has occurred while dedicating to host.", e);
}
}
12 years ago
public IContainer BuildContainer()
{
var container = ContainerBuilder.Build();
logger.Debug("Initializing Components");
container.Resolve<DatabaseTarget>().Register();
LogConfiguration.Reload();
RegisterReporting(container);
RegisterQuality(container);
var indexers = container.Resolve<IEnumerable<IndexerBase>>();
container.Resolve<IndexerProvider>().InitializeIndexers(indexers.ToList());
var newznabIndexers = new List<NewznabDefinition>
{
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<NewznabProvider>().InitializeNewznabIndexers(newznabIndexers);
container.Resolve<JobProvider>().Initialize();
container.Resolve<WebTimer>().StartTimer(30);
var notifiers = container.Resolve<IEnumerable<ExternalNotificationBase>>();
container.Resolve<ExternalNotificationProvider>().InitializeNotifiers(notifiers.ToList());
var providers = container.Resolve<IEnumerable<MetadataBase>>();
container.Resolve<MetadataProvider>().Initialize(providers.ToList());
//SignalR
GlobalHost.DependencyResolver = new AutofacSignalrDependencyResolver(container.BeginLifetimeScope("SignalR"));
return container;
}
private static void ShutDown()
{
13 years ago
logger.Info("Shutting down application...");
WebTimer.Stop();
Process.GetCurrentProcess().Kill();
}
14 years ago
}
}