2010-09-23 05:19:47 +02:00
|
|
|
|
using System;
|
2010-10-17 19:22:48 +02:00
|
|
|
|
using System.Diagnostics;
|
2011-06-18 03:46:22 +02:00
|
|
|
|
using System.Linq;
|
2013-01-03 02:09:13 +01:00
|
|
|
|
using Autofac;
|
2011-04-10 04:44:01 +02:00
|
|
|
|
using NLog;
|
2011-11-13 08:27:16 +01:00
|
|
|
|
using NzbDrone.Common;
|
2010-10-24 09:46:58 +02:00
|
|
|
|
using NzbDrone.Core.Instrumentation;
|
2012-01-25 04:09:49 +01:00
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
2012-02-13 02:07:09 +01:00
|
|
|
|
using SignalR;
|
2010-09-23 05:19:47 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core
|
|
|
|
|
{
|
2011-11-08 21:12:54 +01:00
|
|
|
|
public class CentralDispatch
|
2010-09-23 05:19:47 +02:00
|
|
|
|
{
|
2013-01-19 05:46:43 +01:00
|
|
|
|
private readonly Logger _logger;
|
2012-03-07 03:59:43 +01:00
|
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2010-10-08 00:17:24 +02:00
|
|
|
|
|
2013-01-03 02:09:13 +01:00
|
|
|
|
public ContainerBuilder ContainerBuilder { get; private set; }
|
2011-11-14 01:22:18 +01:00
|
|
|
|
|
2011-11-08 21:12:54 +01:00
|
|
|
|
public CentralDispatch()
|
2011-04-10 04:44:01 +02:00
|
|
|
|
{
|
2013-01-19 05:46:43 +01:00
|
|
|
|
_logger = LogManager.GetCurrentClassLogger();
|
2012-03-07 03:59:43 +01:00
|
|
|
|
_environmentProvider = new EnvironmentProvider();
|
2012-01-26 02:29:55 +01:00
|
|
|
|
|
2013-01-19 05:46:43 +01:00
|
|
|
|
_logger.Debug("Initializing ContainerBuilder:");
|
2013-01-03 02:09:13 +01:00
|
|
|
|
ContainerBuilder = new ContainerBuilder();
|
2011-04-10 04:44:01 +02:00
|
|
|
|
|
2013-01-03 02:09:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-03 05:09:37 +01:00
|
|
|
|
private void RegisterReporting(IContainer container)
|
2013-01-03 02:09:13 +01:00
|
|
|
|
{
|
|
|
|
|
EnvironmentProvider.UGuid = container.Resolve<ConfigProvider>().UGuid;
|
|
|
|
|
ReportingService.RestProvider = container.Resolve<RestProvider>();
|
|
|
|
|
ReportingService.SetupExceptronDriver();
|
|
|
|
|
}
|
2012-07-12 23:48:09 +02:00
|
|
|
|
|
|
|
|
|
|
2011-11-08 21:12:54 +01:00
|
|
|
|
public void DedicateToHost()
|
2010-10-17 19:22:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-03-07 03:59:43 +01:00
|
|
|
|
var pid = _environmentProvider.NzbDroneProcessIdFromEnviroment;
|
2011-06-13 05:45:22 +02:00
|
|
|
|
|
2013-01-19 05:46:43 +01:00
|
|
|
|
_logger.Debug("Attaching to parent process ({0}) for automatic termination.", pid);
|
2011-06-13 05:45:22 +02:00
|
|
|
|
|
|
|
|
|
var hostProcess = Process.GetProcessById(Convert.ToInt32(pid));
|
2010-10-17 19:22:48 +02:00
|
|
|
|
|
|
|
|
|
hostProcess.EnableRaisingEvents = true;
|
|
|
|
|
hostProcess.Exited += (delegate
|
2011-04-10 04:44:01 +02:00
|
|
|
|
{
|
2013-01-19 05:46:43 +01:00
|
|
|
|
_logger.Info("Host has been terminated. Shutting down web server.");
|
2011-04-10 04:44:01 +02:00
|
|
|
|
ShutDown();
|
|
|
|
|
});
|
2010-10-17 19:22:48 +02:00
|
|
|
|
|
2013-01-19 05:46:43 +01:00
|
|
|
|
_logger.Debug("Successfully Attached to host. Process [{0}]", hostProcess.ProcessName);
|
2010-10-17 19:22:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2013-01-19 05:46:43 +01:00
|
|
|
|
_logger.FatalException("An error has occurred while dedicating to host.", e);
|
2010-10-17 19:22:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-03 04:04:42 +01:00
|
|
|
|
public IContainer BuildContainer()
|
2013-01-03 02:09:13 +01:00
|
|
|
|
{
|
2013-01-19 05:46:43 +01:00
|
|
|
|
_logger.Debug("Initializing Components");
|
2013-01-03 02:09:13 +01:00
|
|
|
|
|
2013-01-19 05:46:43 +01:00
|
|
|
|
ContainerBuilder.RegisterCoreServices();
|
|
|
|
|
|
|
|
|
|
var container = ContainerBuilder.Build();
|
2013-01-03 02:09:13 +01:00
|
|
|
|
|
|
|
|
|
container.Resolve<DatabaseTarget>().Register();
|
|
|
|
|
LogConfiguration.Reload();
|
|
|
|
|
|
2013-01-03 05:09:37 +01:00
|
|
|
|
RegisterReporting(container);
|
2013-01-03 02:09:13 +01:00
|
|
|
|
|
|
|
|
|
container.Resolve<WebTimer>().StartTimer(30);
|
|
|
|
|
|
|
|
|
|
//SignalR
|
|
|
|
|
GlobalHost.DependencyResolver = new AutofacSignalrDependencyResolver(container.BeginLifetimeScope("SignalR"));
|
|
|
|
|
|
|
|
|
|
return container;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 05:46:43 +01:00
|
|
|
|
private void ShutDown()
|
2010-10-17 19:22:48 +02:00
|
|
|
|
{
|
2013-01-19 05:46:43 +01:00
|
|
|
|
_logger.Info("Shutting down application...");
|
2011-11-07 07:26:21 +01:00
|
|
|
|
WebTimer.Stop();
|
2010-10-17 19:22:48 +02:00
|
|
|
|
Process.GetCurrentProcess().Kill();
|
|
|
|
|
}
|
2010-09-23 05:19:47 +02:00
|
|
|
|
}
|
2011-11-10 05:14:19 +01:00
|
|
|
|
}
|