Restart for Windows

This commit is contained in:
Mark McDowall 2014-01-13 17:35:16 -08:00
parent f69bb79077
commit aa1a76fe72
8 changed files with 53 additions and 25 deletions

View File

@ -4,10 +4,10 @@ using System.IO;
using System.Security.Principal;
using System.ServiceProcess;
using NLog;
using NzbDrone.Common.Processes;
namespace NzbDrone.Common.EnvironmentInfo
{
public interface IRuntimeInfo
{
bool IsUserInteractive { get; }
@ -67,7 +67,7 @@ namespace NzbDrone.Common.EnvironmentInfo
{
return (OsInfo.IsWindows &&
IsUserInteractive &&
ProcessName.Equals("NzbDrone.Console.exe", StringComparison.InvariantCultureIgnoreCase)) ||
ProcessName.Equals(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME, StringComparison.InvariantCultureIgnoreCase)) ||
OsInfo.IsLinux;
}
}

View File

@ -17,6 +17,7 @@ namespace NzbDrone.Common.EnvironmentInfo
internal const string INSTALL_SERVICE = "i";
internal const string UNINSTALL_SERVICE = "u";
public const string HELP = "?";
public const string TERMINATE = "terminateexisting";
public StartupContext(params string[] args)
{
@ -58,6 +59,5 @@ namespace NzbDrone.Common.EnvironmentInfo
return Flags.Contains(UNINSTALL_SERVICE);
}
}
}
}

View File

@ -1,9 +0,0 @@
using NzbDrone.Common.Messaging;
namespace NzbDrone.Core.Lifecycle
{
public class ApplicationRestartRequested : IEvent
{
}
}

View File

@ -4,6 +4,15 @@ namespace NzbDrone.Core.Lifecycle
{
public class ApplicationShutdownRequested : IEvent
{
public bool Restarting { get; set; }
public ApplicationShutdownRequested()
{
}
public ApplicationShutdownRequested(bool restarting)
{
Restarting = restarting;
}
}
}

View File

@ -1,4 +1,5 @@
using NzbDrone.Common;
using System.IO;
using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using NzbDrone.Core.Lifecycle.Commands;
@ -12,42 +13,66 @@ namespace NzbDrone.Core.Lifecycle
{
private readonly IEventAggregator _eventAggregator;
private readonly IRuntimeInfo _runtimeInfo;
private readonly IAppFolderInfo _appFolderInfo;
private readonly IServiceProvider _serviceProvider;
private readonly IProcessProvider _processProvider;
public LifestyleService(IEventAggregator eventAggregator,
IRuntimeInfo runtimeInfo,
IAppFolderInfo appFolderInfo,
IServiceProvider serviceProvider,
IProcessProvider processProvider)
{
_eventAggregator = eventAggregator;
_runtimeInfo = runtimeInfo;
_appFolderInfo = appFolderInfo;
_serviceProvider = serviceProvider;
_processProvider = processProvider;
}
public void Execute(ShutdownCommand message)
{
_eventAggregator.PublishEvent(new ApplicationShutdownRequested());
if (_runtimeInfo.IsWindowsService)
{
_serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
else
{
_eventAggregator.PublishEvent(new ApplicationShutdownRequested());
}
}
public void Execute(RestartCommand message)
{
_eventAggregator.PublishEvent(new ApplicationShutdownRequested(true));
if (_runtimeInfo.IsWindowsService)
{
_serviceProvider.Restart(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
_eventAggregator.PublishEvent(new ApplicationRestartRequested());
else
{
//TODO: move this to environment specific projects
if (OsInfo.IsWindows)
{
if (_runtimeInfo.IsConsole)
{
//Run console with switch
var path = Path.Combine(_appFolderInfo.StartUpFolder,
ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME + ".exe");
_processProvider.SpawnNewProcess(path, "--terminateexisting --nobrowser");
}
else
{
var path = Path.Combine(_appFolderInfo.StartUpFolder,
ProcessProvider.NZB_DRONE_PROCESS_NAME + ".exe");
_processProvider.Start(path, "--terminateexisting --nobrowser");
}
}
}
}
}
}

View File

@ -292,7 +292,6 @@
<Compile Include="MediaFiles\Commands\RescanSeriesCommand.cs" />
<Compile Include="Lifecycle\Commands\ShutdownCommand.cs" />
<Compile Include="Lifecycle\Commands\RestartCommand.cs" />
<Compile Include="Lifecycle\ApplicationRestartRequested.cs" />
<Compile Include="Lifecycle\LifestyleService.cs" />
<Compile Include="MediaFiles\Commands\RenameFilesCommand.cs" />
<Compile Include="MediaFiles\EpisodeFileMoveResult.cs" />

View File

@ -75,7 +75,7 @@ namespace NzbDrone.Host
public void Handle(ApplicationShutdownRequested message)
{
if (!_runtimeInfo.IsWindowsService)
if (!_runtimeInfo.IsWindowsService && !message.Restarting)
{
Shutdown();
}

View File

@ -36,7 +36,7 @@ namespace NzbDrone.Host
var appMode = GetApplicationMode(startupContext);
Start(appMode);
Start(appMode, startupContext);
if (startCallback != null)
{
@ -54,11 +54,11 @@ namespace NzbDrone.Host
}
}
private static void Start(ApplicationModes applicationModes)
private static void Start(ApplicationModes applicationModes, StartupContext startupContext)
{
if (!IsInUtilityMode(applicationModes))
{
EnsureSingleInstance(applicationModes == ApplicationModes.Service);
EnsureSingleInstance(applicationModes == ApplicationModes.Service, startupContext);
}
DbFactory.RegisterDatabase(_container);
@ -80,7 +80,7 @@ namespace NzbDrone.Host
}
}
private static void EnsureSingleInstance(bool isService)
private static void EnsureSingleInstance(bool isService, StartupContext startupContext)
{
var instancePolicy = _container.Resolve<ISingleInstancePolicy>();
@ -88,6 +88,10 @@ namespace NzbDrone.Host
{
instancePolicy.KillAllOtherInstance();
}
else if (startupContext.Flags.Contains(StartupContext.TERMINATE))
{
instancePolicy.KillAllOtherInstance();
}
else
{
instancePolicy.PreventStartIfAlreadyRunning();