2014-02-04 07:11:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.ServiceProcess;
|
2011-10-07 03:37:41 +00:00
|
|
|
|
using NLog;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2014-02-04 07:11:36 +00:00
|
|
|
|
using NzbDrone.Common.Processes;
|
2013-05-23 05:12:01 +00:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2014-01-13 01:14:57 +00:00
|
|
|
|
using NzbDrone.Core.Lifecycle;
|
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-08-07 05:32:22 +00:00
|
|
|
|
using NzbDrone.Host.Owin;
|
2011-10-07 03:37:41 +00:00
|
|
|
|
|
2013-08-07 05:32:22 +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;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
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
|
|
|
|
|
2014-08-01 22:10:31 +00:00
|
|
|
|
public NzbDroneServiceFactory(IConfigFileProvider configFileProvider,
|
2014-02-04 07:11:36 +00:00
|
|
|
|
IHostController hostController,
|
2014-08-01 22:10:31 +00:00
|
|
|
|
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;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
_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
|
|
|
|
}
|
2011-10-14 01:22:51 +00:00
|
|
|
|
|
2011-10-15 00:41:09 +00:00
|
|
|
|
protected override void OnStart(string[] args)
|
2011-10-14 01:22:51 +00:00
|
|
|
|
{
|
|
|
|
|
Start();
|
2011-10-09 02:16:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-16 04:52:41 +00:00
|
|
|
|
public void Start()
|
2011-10-07 03:37:41 +00:00
|
|
|
|
{
|
2014-03-09 05:28:40 +00:00
|
|
|
|
if (OsInfo.IsMono)
|
2014-02-04 07:11:36 +00:00
|
|
|
|
{
|
2014-03-10 05:35:50 +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
|
|
|
|
}
|
2011-10-14 01:22:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-15 00:41:09 +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
|
|
|
|
{
|
2014-03-10 05:35:50 +00:00
|
|
|
|
if (!_runtimeInfo.IsWindowsService)
|
2014-02-04 07:11:36 +00:00
|
|
|
|
{
|
2014-03-10 05:35:50 +00:00
|
|
|
|
if (message.Restarting)
|
|
|
|
|
{
|
|
|
|
|
_runtimeInfo.RestartPending = true;
|
|
|
|
|
}
|
2014-02-04 07:11:36 +00:00
|
|
|
|
|
2014-03-10 05:35:50 +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
|
|
|
|
}
|