sonarr-repo-only/src/NzbDrone.Host/ApplicationServer.cs

97 lines
2.9 KiB
C#
Raw Normal View History

2014-02-04 07:11:36 +00:00
using System;
using System.ServiceProcess;
2011-10-07 03:37:41 +00:00
using NLog;
using NzbDrone.Common.EnvironmentInfo;
2014-02-04 07:11:36 +00:00
using NzbDrone.Common.Processes;
using NzbDrone.Core.Configuration;
2014-01-13 01:14:57 +00:00
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Host.Owin;
2011-10-07 03:37:41 +00:00
namespace NzbDrone.Host
2011-10-07 03:37:41 +00:00
{
2013-04-16 04:52:41 +00:00
public interface INzbDroneServiceFactory
2011-10-07 03:37:41 +00:00
{
2013-04-16 04:52:41 +00:00
ServiceBase Build();
void Start();
}
2011-10-07 03:37:41 +00:00
2014-01-13 01:14:57 +00:00
public class NzbDroneServiceFactory : ServiceBase, INzbDroneServiceFactory, IHandle<ApplicationShutdownRequested>
2013-04-16 04:52:41 +00:00
{
2013-05-10 23:53:50 +00:00
private readonly IConfigFileProvider _configFileProvider;
private readonly IRuntimeInfo _runtimeInfo;
2013-02-19 01:57:08 +00:00
private readonly IHostController _hostController;
2013-11-26 06:53:36 +00:00
private readonly IStartupContext _startupContext;
2013-11-26 02:46:12 +00:00
private readonly IBrowserService _browserService;
2013-04-16 04:52:41 +00:00
private readonly Logger _logger;
2011-10-07 03:37:41 +00:00
public NzbDroneServiceFactory(IConfigFileProvider configFileProvider,
2014-02-04 07:11:36 +00:00
IHostController hostController,
IRuntimeInfo runtimeInfo,
IStartupContext startupContext,
IBrowserService browserService,
2014-02-04 07:11:36 +00:00
Logger logger)
2011-10-07 03:37:41 +00:00
{
2011-11-13 07:27:16 +00:00
_configFileProvider = configFileProvider;
2013-02-19 01:13:42 +00:00
_hostController = hostController;
_runtimeInfo = runtimeInfo;
2013-11-26 06:53:36 +00:00
_startupContext = startupContext;
2013-11-26 02:46:12 +00:00
_browserService = browserService;
2013-04-16 04:52:41 +00:00
_logger = logger;
2011-10-07 03:37:41 +00:00
}
protected override void OnStart(string[] args)
{
Start();
}
2013-04-16 04:52:41 +00:00
public void Start()
2011-10-07 03:37:41 +00:00
{
if (OsInfo.IsMono)
2014-02-04 07:11:36 +00:00
{
Console.CancelKeyPress += (sender, eventArgs) => LogManager.Configuration = null;
2014-02-04 07:11:36 +00:00
}
2014-01-13 01:14:57 +00:00
_runtimeInfo.IsRunning = true;
2013-02-19 01:13:42 +00:00
_hostController.StartServer();
2011-10-07 03:37:41 +00:00
2013-11-26 06:53:36 +00:00
if (!_startupContext.Flags.Contains(StartupContext.NO_BROWSER)
&& _configFileProvider.LaunchBrowser)
2011-10-07 03:37:41 +00:00
{
2013-11-26 02:46:12 +00:00
_browserService.LaunchWebUI();
2011-10-07 03:37:41 +00:00
}
}
protected override void OnStop()
2014-01-13 01:14:57 +00:00
{
Shutdown();
}
public ServiceBase Build()
{
return this;
}
private void Shutdown()
2011-10-07 03:37:41 +00:00
{
2013-04-16 04:52:41 +00:00
_logger.Info("Attempting to stop application.");
2013-02-19 01:13:42 +00:00
_hostController.StopServer();
2013-04-16 04:52:41 +00:00
_logger.Info("Application has finished stop routine.");
2014-01-13 01:14:57 +00:00
_runtimeInfo.IsRunning = false;
2013-04-16 04:52:41 +00:00
}
2014-01-13 01:14:57 +00:00
public void Handle(ApplicationShutdownRequested message)
2013-04-16 04:52:41 +00:00
{
if (!_runtimeInfo.IsWindowsService)
2014-02-04 07:11:36 +00:00
{
if (message.Restarting)
{
_runtimeInfo.RestartPending = true;
}
2014-02-04 07:11:36 +00:00
LogManager.Configuration = null;
2014-01-13 01:14:57 +00:00
Shutdown();
}
2011-10-07 03:37:41 +00:00
}
}
2011-10-07 06:57:43 +00:00
}