Restart for Windows

pull/2/head
Mark McDowall 11 years ago
parent f69bb79077
commit aa1a76fe72

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

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

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

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

@ -1,4 +1,5 @@
using NzbDrone.Common; using System.IO;
using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes; using NzbDrone.Common.Processes;
using NzbDrone.Core.Lifecycle.Commands; using NzbDrone.Core.Lifecycle.Commands;
@ -12,42 +13,66 @@ namespace NzbDrone.Core.Lifecycle
{ {
private readonly IEventAggregator _eventAggregator; private readonly IEventAggregator _eventAggregator;
private readonly IRuntimeInfo _runtimeInfo; private readonly IRuntimeInfo _runtimeInfo;
private readonly IAppFolderInfo _appFolderInfo;
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private readonly IProcessProvider _processProvider; private readonly IProcessProvider _processProvider;
public LifestyleService(IEventAggregator eventAggregator, public LifestyleService(IEventAggregator eventAggregator,
IRuntimeInfo runtimeInfo, IRuntimeInfo runtimeInfo,
IAppFolderInfo appFolderInfo,
IServiceProvider serviceProvider, IServiceProvider serviceProvider,
IProcessProvider processProvider) IProcessProvider processProvider)
{ {
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
_runtimeInfo = runtimeInfo; _runtimeInfo = runtimeInfo;
_appFolderInfo = appFolderInfo;
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;
_processProvider = processProvider; _processProvider = processProvider;
} }
public void Execute(ShutdownCommand message) public void Execute(ShutdownCommand message)
{ {
_eventAggregator.PublishEvent(new ApplicationShutdownRequested());
if (_runtimeInfo.IsWindowsService) if (_runtimeInfo.IsWindowsService)
{ {
_serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME); _serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME);
} }
else
{
_eventAggregator.PublishEvent(new ApplicationShutdownRequested());
}
} }
public void Execute(RestartCommand message) public void Execute(RestartCommand message)
{ {
_eventAggregator.PublishEvent(new ApplicationShutdownRequested(true));
if (_runtimeInfo.IsWindowsService) if (_runtimeInfo.IsWindowsService)
{ {
_serviceProvider.Restart(ServiceProvider.NZBDRONE_SERVICE_NAME); _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");
}
}
}
} }
} }
} }

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

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

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

Loading…
Cancel
Save