1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00
Sonarr/NzbDrone.Core/CentralDispatch.cs

92 lines
2.8 KiB
C#
Raw Normal View History

2010-09-23 06:19:47 +03:00
using System;
using System.Diagnostics;
2011-06-18 04:46:22 +03:00
using System.Linq;
2013-01-03 04:09:13 +03:00
using Autofac;
2011-04-10 05:44:01 +03:00
using NLog;
2011-11-13 10:27:16 +03:00
using NzbDrone.Common;
using NzbDrone.Core.Instrumentation;
2012-01-25 06:09:49 +03:00
using NzbDrone.Core.Providers.Core;
using SignalR;
2010-09-23 06:19:47 +03:00
namespace NzbDrone.Core
{
2011-11-08 23:12:54 +03:00
public class CentralDispatch
2010-09-23 06:19:47 +03:00
{
private readonly Logger _logger;
private readonly EnvironmentProvider _environmentProvider;
2010-10-08 01:17:24 +03:00
2013-01-03 04:09:13 +03:00
public ContainerBuilder ContainerBuilder { get; private set; }
2011-11-08 23:12:54 +03:00
public CentralDispatch()
2011-04-10 05:44:01 +03:00
{
_logger = LogManager.GetCurrentClassLogger();
_environmentProvider = new EnvironmentProvider();
2012-01-26 04:29:55 +03:00
_logger.Debug("Initializing ContainerBuilder:");
2013-01-03 04:09:13 +03:00
ContainerBuilder = new ContainerBuilder();
2011-04-10 05:44:01 +03:00
2013-01-03 04:09:13 +03:00
}
2013-01-19 08:14:12 +03:00
private void RegisterReporting(IComponentContext container)
2013-01-03 04:09:13 +03:00
{
EnvironmentProvider.UGuid = container.Resolve<ConfigProvider>().UGuid;
ReportingService.RestProvider = container.Resolve<RestProvider>();
ReportingService.SetupExceptronDriver();
}
2012-07-13 00:48:09 +03:00
2011-11-08 23:12:54 +03:00
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
2011-04-10 05:44:01 +03:00
{
_logger.Info("Host has been terminated. Shutting down web server.");
2011-04-10 05:44:01 +03:00
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);
}
}
2013-01-03 06:04:42 +03:00
public IContainer BuildContainer()
2013-01-03 04:09:13 +03:00
{
_logger.Debug("Initializing Components");
2013-01-03 04:09:13 +03:00
ContainerBuilder.RegisterCoreServices();
var container = ContainerBuilder.Build();
2013-01-03 04:09:13 +03:00
container.Resolve<DatabaseTarget>().Register();
LogConfiguration.Reload();
RegisterReporting(container);
2013-01-03 04:09:13 +03:00
container.Resolve<WebTimer>().StartTimer(30);
//SignalR
GlobalHost.DependencyResolver = new AutofacSignalrDependencyResolver(container.BeginLifetimeScope("SignalR"));
return container;
}
private void ShutDown()
{
_logger.Info("Shutting down application...");
WebTimer.Stop();
Process.GetCurrentProcess().Kill();
}
2010-09-23 06:19:47 +03:00
}
}