mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-12-14 11:23:42 +02:00
215 lines
8.6 KiB
C#
215 lines
8.6 KiB
C#
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<EnvironmentProvider>();
|
|
|
|
InitDatabase();
|
|
InitExternalNotifications();
|
|
InitMetadataProviders();
|
|
InitIndexers();
|
|
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.Register(c => c.Resolve<Connection>().GetLogEfContext())
|
|
.As<LogDbContext>()
|
|
.SingleInstance();
|
|
|
|
ContainerBuilder.RegisterType<DatabaseTarget>().WithParameter(ResolvedParameter.ForNamed<IDatabase>("DatabaseTarget"));
|
|
ContainerBuilder.RegisterType<LogProvider>().WithParameter(ResolvedParameter.ForNamed<IDatabase>("LogProvider"));
|
|
}
|
|
|
|
private void InitIndexers()
|
|
{
|
|
logger.Debug("Registering Indexers...");
|
|
|
|
ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly)
|
|
.Where(t => t.BaseType == typeof(IndexerBase))
|
|
.As<IndexerBase>();
|
|
|
|
//ContainerBuilder.Bind<IndexerBase>().To<NzbsRUs>();
|
|
//ContainerBuilder.Bind<IndexerBase>().To<Newznab>();
|
|
//ContainerBuilder.Bind<IndexerBase>().To<Wombles>();
|
|
//ContainerBuilder.Bind<IndexerBase>().To<FileSharingTalk>();
|
|
//ContainerBuilder.Bind<IndexerBase>().To<NzbIndex>();
|
|
//ContainerBuilder.Bind<IndexerBase>().To<NzbClub>();
|
|
//ContainerBuilder.Bind<IndexerBase>().To<Omgwtfnzbs>();
|
|
//ContainerBuilder.Bind<IndexerBase>().To<Nzbx>();
|
|
}
|
|
|
|
private void RegisterJobs()
|
|
{
|
|
logger.Debug("Registering Background Jobs...");
|
|
|
|
ContainerBuilder.RegisterType<JobProvider>().SingleInstance();
|
|
|
|
ContainerBuilder.RegisterType<Xbmc>().As<ExternalNotificationBase>().SingleInstance();
|
|
|
|
ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly)
|
|
.Where(t => t.GetInterfaces().Contains(typeof(IJob)))
|
|
.As<IJob>()
|
|
.SingleInstance();
|
|
}
|
|
|
|
private void InitExternalNotifications()
|
|
{
|
|
logger.Debug("Registering External Notifications...");
|
|
ContainerBuilder.RegisterType<Xbmc>().As<ExternalNotificationBase>().SingleInstance();
|
|
ContainerBuilder.RegisterType<Smtp>().As<ExternalNotificationBase>().SingleInstance();
|
|
ContainerBuilder.RegisterType<Twitter>().As<ExternalNotificationBase>().SingleInstance();
|
|
ContainerBuilder.RegisterType<Providers.ExternalNotification.Growl>().As<ExternalNotificationBase>().SingleInstance();
|
|
ContainerBuilder.RegisterType<Prowl>().As<ExternalNotificationBase>().SingleInstance();
|
|
ContainerBuilder.RegisterType<Plex>().As<ExternalNotificationBase>().SingleInstance();
|
|
}
|
|
|
|
private void InitMetadataProviders()
|
|
{
|
|
logger.Debug("Registering Metadata Providers...");
|
|
|
|
ContainerBuilder.RegisterType<Providers.Metadata.Xbmc>().As<MetadataBase>().SingleInstance();
|
|
}
|
|
|
|
private void InitReporting(IContainer container)
|
|
{
|
|
EnvironmentProvider.UGuid = container.Resolve<ConfigProvider>().UGuid;
|
|
ReportingService.RestProvider = container.Resolve<RestProvider>();
|
|
ReportingService.SetupExceptronDriver();
|
|
}
|
|
|
|
private void InitQuality(IContainer container)
|
|
{
|
|
logger.Debug("Initializing Quality...");
|
|
container.Resolve<QualityProvider>().SetupDefaultProfiles();
|
|
container.Resolve<QualityTypeProvider>().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 Build()
|
|
{
|
|
var container = ContainerBuilder.Build();
|
|
|
|
logger.Debug("Initializing Components");
|
|
|
|
container.Resolve<DatabaseTarget>().Register();
|
|
LogConfiguration.Reload();
|
|
|
|
InitReporting(container);
|
|
InitQuality(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()
|
|
{
|
|
logger.Info("Shutting down application...");
|
|
WebTimer.Stop();
|
|
Process.GetCurrentProcess().Kill();
|
|
}
|
|
}
|
|
}
|