2010-09-22 20:19:47 -07:00
|
|
|
using System;
|
2012-05-03 15:08:35 -07:00
|
|
|
using System.Collections.Generic;
|
2010-10-17 10:22:48 -07:00
|
|
|
using System.Diagnostics;
|
2011-11-13 16:22:18 -08:00
|
|
|
using System.IO;
|
2011-06-17 18:46:22 -07:00
|
|
|
using System.Linq;
|
2013-01-02 17:09:13 -08:00
|
|
|
using Autofac;
|
|
|
|
using Autofac.Core;
|
2011-04-09 19:44:01 -07:00
|
|
|
using NLog;
|
2011-11-12 23:27:16 -08:00
|
|
|
using NzbDrone.Common;
|
2010-10-24 00:46:58 -07:00
|
|
|
using NzbDrone.Core.Instrumentation;
|
2011-12-01 17:33:17 -08:00
|
|
|
using NzbDrone.Core.Jobs;
|
2010-09-27 21:25:41 -07:00
|
|
|
using NzbDrone.Core.Providers;
|
2012-01-24 19:09:49 -08:00
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-04-28 23:06:13 -07:00
|
|
|
using NzbDrone.Core.Providers.ExternalNotification;
|
2011-04-18 17:12:06 -07:00
|
|
|
using NzbDrone.Core.Providers.Indexer;
|
2012-07-12 14:48:09 -07:00
|
|
|
using NzbDrone.Core.Providers.Metadata;
|
2012-05-03 15:08:35 -07:00
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-14 19:31:41 -07:00
|
|
|
using PetaPoco;
|
2012-02-12 17:07:09 -08:00
|
|
|
using SignalR;
|
|
|
|
using Connection = NzbDrone.Core.Datastore.Connection;
|
2010-09-22 20:19:47 -07:00
|
|
|
|
|
|
|
namespace NzbDrone.Core
|
|
|
|
{
|
2011-11-08 12:12:54 -08:00
|
|
|
public class CentralDispatch
|
2010-09-22 20:19:47 -07:00
|
|
|
{
|
2012-02-12 22:38:57 -08:00
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2012-03-06 18:59:43 -08:00
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2010-10-07 15:17:24 -07:00
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
public ContainerBuilder ContainerBuilder { get; private set; }
|
2011-11-13 16:22:18 -08:00
|
|
|
|
2011-11-08 12:12:54 -08:00
|
|
|
public CentralDispatch()
|
2011-04-09 19:44:01 -07:00
|
|
|
{
|
2012-03-06 18:59:43 -08:00
|
|
|
_environmentProvider = new EnvironmentProvider();
|
2012-01-25 17:29:55 -08:00
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
logger.Debug("Initializing ContainerBuilder:");
|
|
|
|
ContainerBuilder = new ContainerBuilder();
|
2011-04-09 19:44:01 -07:00
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
ContainerBuilder.RegisterAssemblyTypes(typeof(DiskProvider).Assembly).SingleInstance();
|
|
|
|
ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly).SingleInstance();
|
|
|
|
ContainerBuilder.RegisterType<EnvironmentProvider>();
|
2012-02-12 17:07:09 -08:00
|
|
|
|
2011-11-13 16:22:18 -08:00
|
|
|
InitDatabase();
|
2013-01-02 20:09:37 -08:00
|
|
|
RegisterExternalNotifications();
|
|
|
|
RegisterMetadataProviders();
|
|
|
|
RegisterIndexers();
|
2013-01-02 17:09:13 -08:00
|
|
|
RegisterJobs();
|
2011-11-13 16:22:18 -08:00
|
|
|
}
|
2012-01-24 19:09:49 -08:00
|
|
|
|
2011-11-13 16:22:18 -08:00
|
|
|
private void InitDatabase()
|
2011-05-23 16:29:14 -07:00
|
|
|
{
|
2013-01-02 17:09:13 -08:00
|
|
|
logger.Info("Registering Database...");
|
2011-05-26 19:12:28 -07:00
|
|
|
|
2012-03-06 18:59:43 -08:00
|
|
|
var appDataPath = _environmentProvider.GetAppDataPath();
|
2011-11-13 16:22:18 -08:00
|
|
|
if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath);
|
2011-10-11 19:24:43 -07:00
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
ContainerBuilder.Register(c => c.Resolve<Connection>().GetMainPetaPocoDb())
|
|
|
|
.As<IDatabase>();
|
2011-05-26 19:12:28 -07:00
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
ContainerBuilder.Register(c => c.Resolve<Connection>().GetLogPetaPocoDb(false))
|
|
|
|
.SingleInstance()
|
|
|
|
.Named<IDatabase>("DatabaseTarget");
|
2011-05-23 16:29:14 -07:00
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
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"));
|
2012-01-24 19:09:49 -08:00
|
|
|
}
|
|
|
|
|
2013-01-02 20:09:37 -08:00
|
|
|
private void RegisterIndexers()
|
2010-09-22 20:19:47 -07:00
|
|
|
{
|
2013-01-02 17:09:13 -08:00
|
|
|
logger.Debug("Registering Indexers...");
|
|
|
|
|
|
|
|
ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly)
|
|
|
|
.Where(t => t.BaseType == typeof(IndexerBase))
|
|
|
|
.As<IndexerBase>();
|
2010-09-22 20:19:47 -07:00
|
|
|
}
|
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
private void RegisterJobs()
|
2011-04-18 17:12:06 -07:00
|
|
|
{
|
2013-01-02 17:09:13 -08:00
|
|
|
logger.Debug("Registering Background Jobs...");
|
2012-05-03 15:08:35 -07:00
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
ContainerBuilder.RegisterType<JobProvider>().SingleInstance();
|
2012-05-03 15:08:35 -07:00
|
|
|
|
2013-01-02 17:09:13 -08:00
|
|
|
ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly)
|
|
|
|
.Where(t => t.GetInterfaces().Contains(typeof(IJob)))
|
|
|
|
.As<IJob>()
|
|
|
|
.SingleInstance();
|
2011-04-19 19:17:28 -07:00
|
|
|
}
|
|
|
|
|
2013-01-02 20:09:37 -08:00
|
|
|
private void RegisterExternalNotifications()
|
2011-04-28 23:06:13 -07:00
|
|
|
{
|
2013-01-02 17:09:13 -08:00
|
|
|
logger.Debug("Registering External Notifications...");
|
2013-01-02 20:09:37 -08:00
|
|
|
|
|
|
|
ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly)
|
|
|
|
.Where(t => t.BaseType == typeof(ExternalNotificationBase))
|
2013-01-02 21:11:39 -08:00
|
|
|
.As<ExternalNotificationBase>();
|
2011-04-28 23:06:13 -07:00
|
|
|
}
|
2011-04-21 19:23:31 -07:00
|
|
|
|
2013-01-02 20:09:37 -08:00
|
|
|
private void RegisterMetadataProviders()
|
2012-07-12 14:48:09 -07:00
|
|
|
{
|
2013-01-02 17:09:13 -08:00
|
|
|
logger.Debug("Registering Metadata Providers...");
|
2012-07-12 14:48:09 -07:00
|
|
|
|
2013-01-02 20:09:37 -08:00
|
|
|
ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly)
|
|
|
|
.Where(t => t.IsSubclassOf(typeof(MetadataBase)))
|
|
|
|
.As<MetadataBase>();
|
2013-01-02 17:09:13 -08:00
|
|
|
}
|
|
|
|
|
2013-01-02 20:09:37 -08:00
|
|
|
private void RegisterReporting(IContainer container)
|
2013-01-02 17:09:13 -08:00
|
|
|
{
|
|
|
|
EnvironmentProvider.UGuid = container.Resolve<ConfigProvider>().UGuid;
|
|
|
|
ReportingService.RestProvider = container.Resolve<RestProvider>();
|
|
|
|
ReportingService.SetupExceptronDriver();
|
|
|
|
}
|
2012-07-12 14:48:09 -07:00
|
|
|
|
2013-01-02 20:09:37 -08:00
|
|
|
private void RegisterQuality(IContainer container)
|
2013-01-02 17:09:13 -08:00
|
|
|
{
|
|
|
|
logger.Debug("Initializing Quality...");
|
|
|
|
container.Resolve<QualityProvider>().SetupDefaultProfiles();
|
|
|
|
container.Resolve<QualityTypeProvider>().SetupDefault();
|
2012-07-12 14:48:09 -07:00
|
|
|
}
|
|
|
|
|
2011-11-08 12:12:54 -08:00
|
|
|
public void DedicateToHost()
|
2010-10-17 10:22:48 -07:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-03-06 18:59:43 -08:00
|
|
|
var pid = _environmentProvider.NzbDroneProcessIdFromEnviroment;
|
2011-06-12 20:45:22 -07:00
|
|
|
|
2012-02-12 22:38:57 -08:00
|
|
|
logger.Debug("Attaching to parent process ({0}) for automatic termination.", pid);
|
2011-06-12 20:45:22 -07:00
|
|
|
|
|
|
|
var hostProcess = Process.GetProcessById(Convert.ToInt32(pid));
|
2010-10-17 10:22:48 -07:00
|
|
|
|
|
|
|
hostProcess.EnableRaisingEvents = true;
|
|
|
|
hostProcess.Exited += (delegate
|
2011-04-09 19:44:01 -07:00
|
|
|
{
|
2012-02-12 22:38:57 -08:00
|
|
|
logger.Info("Host has been terminated. Shutting down web server.");
|
2011-04-09 19:44:01 -07:00
|
|
|
ShutDown();
|
|
|
|
});
|
2010-10-17 10:22:48 -07:00
|
|
|
|
2012-02-12 22:38:57 -08:00
|
|
|
logger.Debug("Successfully Attached to host. Process [{0}]", hostProcess.ProcessName);
|
2010-10-17 10:22:48 -07:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2012-02-12 22:38:57 -08:00
|
|
|
logger.FatalException("An error has occurred while dedicating to host.", e);
|
2010-10-17 10:22:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-02 19:04:42 -08:00
|
|
|
public IContainer BuildContainer()
|
2013-01-02 17:09:13 -08:00
|
|
|
{
|
|
|
|
var container = ContainerBuilder.Build();
|
|
|
|
|
|
|
|
logger.Debug("Initializing Components");
|
|
|
|
|
|
|
|
container.Resolve<DatabaseTarget>().Register();
|
|
|
|
LogConfiguration.Reload();
|
|
|
|
|
2013-01-02 20:09:37 -08:00
|
|
|
RegisterReporting(container);
|
|
|
|
RegisterQuality(container);
|
2013-01-02 17:09:13 -08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-10-17 10:22:48 -07:00
|
|
|
private static void ShutDown()
|
|
|
|
{
|
2012-02-12 22:38:57 -08:00
|
|
|
logger.Info("Shutting down application...");
|
2011-11-06 22:26:21 -08:00
|
|
|
WebTimer.Stop();
|
2010-10-17 10:22:48 -07:00
|
|
|
Process.GetCurrentProcess().Kill();
|
|
|
|
}
|
2010-09-22 20:19:47 -07:00
|
|
|
}
|
2011-11-09 20:14:19 -08:00
|
|
|
}
|