mirror of
https://github.com/Radarr/Radarr.git
synced 2024-09-11 12:02:35 +02:00
Service kills other instances on start.
This commit is contained in:
parent
1e6817220a
commit
d42a63a480
@ -34,7 +34,7 @@ public void should_continue_if_only_instance()
|
||||
});
|
||||
|
||||
|
||||
Subject.EnforceSingleInstance();
|
||||
Subject.PreventStartIfAlreadyRunning();
|
||||
|
||||
|
||||
Mocker.GetMock<IBrowserService>().Verify(c => c.LaunchWebUI(), Times.Never());
|
||||
@ -59,7 +59,7 @@ public void should_enforce_if_another_console_is_running()
|
||||
|
||||
|
||||
|
||||
Assert.Throws<TerminateApplicationException>(() => Subject.EnforceSingleInstance());
|
||||
Assert.Throws<TerminateApplicationException>(() => Subject.PreventStartIfAlreadyRunning());
|
||||
Mocker.GetMock<IBrowserService>().Verify(c => c.LaunchWebUI(), Times.Once());
|
||||
ExceptionVerification.ExpectedWarns(1);
|
||||
}
|
||||
@ -83,7 +83,7 @@ public void should_return_false_if_another_gui_is_running()
|
||||
|
||||
|
||||
|
||||
Assert.Throws<TerminateApplicationException>(() => Subject.EnforceSingleInstance());
|
||||
Assert.Throws<TerminateApplicationException>(() => Subject.PreventStartIfAlreadyRunning());
|
||||
Mocker.GetMock<IBrowserService>().Verify(c => c.LaunchWebUI(), Times.Once());
|
||||
ExceptionVerification.ExpectedWarns(1);
|
||||
}
|
||||
|
@ -24,9 +24,9 @@ public void PrintHelp()
|
||||
Console.WriteLine();
|
||||
Console.WriteLine(" Usage: {0} <command> ", Process.GetCurrentProcess().MainModule.ModuleName);
|
||||
Console.WriteLine(" Commands:");
|
||||
Console.WriteLine(" /{0} Install the application as a Windows Service ({1}).", StartupArguments.INSTALL_SERVICE, ServiceProvider.NZBDRONE_SERVICE_NAME);
|
||||
Console.WriteLine(" /{0} Uninstall already installed Windows Service ({1}).", StartupArguments.UNINSTALL_SERVICE, ServiceProvider.NZBDRONE_SERVICE_NAME);
|
||||
Console.WriteLine(" /{0} Don't open NzbDrone in a browser", StartupArguments.NO_BROWSER);
|
||||
Console.WriteLine(" /{0} Install the application as a Windows Service ({1}).", StartupContext.INSTALL_SERVICE, ServiceProvider.NZBDRONE_SERVICE_NAME);
|
||||
Console.WriteLine(" /{0} Uninstall already installed Windows Service ({1}).", StartupContext.UNINSTALL_SERVICE, ServiceProvider.NZBDRONE_SERVICE_NAME);
|
||||
Console.WriteLine(" /{0} Don't open NzbDrone in a browser", StartupContext.NO_BROWSER);
|
||||
Console.WriteLine(" <No Arguments> Run application in console mode.");
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ public interface IProcessProvider
|
||||
void WaitForExit(Process process);
|
||||
void SetPriority(int processId, ProcessPriorityClass priority);
|
||||
void KillAll(string processName);
|
||||
void Kill(int processId);
|
||||
bool Exists(string processName);
|
||||
ProcessPriorityClass GetCurrentProcessPriority();
|
||||
Process Start(string path, string args = null, Action<string> onOutputDataReceived = null, Action<string> onErrorDataReceived = null);
|
||||
@ -254,7 +255,7 @@ private static string GetExeFileName(Process process)
|
||||
return process.Modules.Cast<ProcessModule>().FirstOrDefault(module => module.ModuleName.ToLower().EndsWith(".exe")).FileName;
|
||||
}
|
||||
|
||||
private void Kill(int processId)
|
||||
public void Kill(int processId)
|
||||
{
|
||||
var process = Process.GetProcesses().FirstOrDefault(p => p.Id == processId);
|
||||
|
||||
|
@ -47,7 +47,7 @@ public static void Start(StartupContext startupContext, IUserAlert userAlert, Ac
|
||||
}
|
||||
catch (TerminateApplicationException e)
|
||||
{
|
||||
Logger.Info("Application has been terminated. Reason " + e.Reason);
|
||||
Logger.Info(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,7 +56,7 @@ private static void Start(ApplicationModes applicationModes)
|
||||
{
|
||||
if (!IsInUtilityMode(applicationModes))
|
||||
{
|
||||
EnsureSingleInstance();
|
||||
EnsureSingleInstance(applicationModes == ApplicationModes.Service);
|
||||
}
|
||||
|
||||
DbFactory.RegisterDatabase(_container);
|
||||
@ -78,9 +78,18 @@ private static void SpinToExit(ApplicationModes applicationModes)
|
||||
}
|
||||
}
|
||||
|
||||
private static void EnsureSingleInstance()
|
||||
private static void EnsureSingleInstance(bool isService)
|
||||
{
|
||||
_container.Resolve<ISingleInstancePolicy>().EnforceSingleInstance();
|
||||
var instancePolicy = _container.Resolve<ISingleInstancePolicy>();
|
||||
|
||||
if (isService)
|
||||
{
|
||||
instancePolicy.KillAllOtherInstance();
|
||||
}
|
||||
else
|
||||
{
|
||||
instancePolicy.PreventStartIfAlreadyRunning();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,4 +1,6 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Processes;
|
||||
|
||||
@ -6,7 +8,8 @@ namespace NzbDrone.Host
|
||||
{
|
||||
public interface ISingleInstancePolicy
|
||||
{
|
||||
void EnforceSingleInstance();
|
||||
void PreventStartIfAlreadyRunning();
|
||||
void KillAllOtherInstance();
|
||||
}
|
||||
|
||||
public class SingleInstancePolicy : ISingleInstancePolicy
|
||||
@ -22,7 +25,7 @@ public SingleInstancePolicy(IProcessProvider processProvider, IBrowserService br
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void EnforceSingleInstance()
|
||||
public void PreventStartIfAlreadyRunning()
|
||||
{
|
||||
if (IsAlreadyRunning())
|
||||
{
|
||||
@ -32,16 +35,34 @@ public void EnforceSingleInstance()
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsAlreadyRunning()
|
||||
public void KillAllOtherInstance()
|
||||
{
|
||||
var currentId = _processProvider.GetCurrentProcess().Id;
|
||||
var consoleIds = _processProvider.FindProcessByName(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME).Select(c => c.Id);
|
||||
var guiIds = _processProvider.FindProcessByName(ProcessProvider.NZB_DRONE_PROCESS_NAME).Select(c => c.Id);
|
||||
|
||||
var otherProcesses = consoleIds.Union(guiIds).Except(new[] { currentId });
|
||||
|
||||
return otherProcesses.Any();
|
||||
foreach (var processId in GetOtherNzbDroneProcessIds())
|
||||
{
|
||||
_processProvider.Kill(processId);
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsAlreadyRunning()
|
||||
{
|
||||
return GetOtherNzbDroneProcessIds().Any();
|
||||
}
|
||||
|
||||
private List<int> GetOtherNzbDroneProcessIds()
|
||||
{
|
||||
var currentId = _processProvider.GetCurrentProcess().Id;
|
||||
var consoleIds = _processProvider.FindProcessByName(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME)
|
||||
.Select(c => c.Id);
|
||||
var winformIds = _processProvider.FindProcessByName(ProcessProvider.NZB_DRONE_PROCESS_NAME).Select(c => c.Id);
|
||||
|
||||
var otherProcesses = consoleIds.Union(winformIds).Except(new[] { currentId }).ToList();
|
||||
|
||||
if (otherProcesses.Any())
|
||||
{
|
||||
_logger.Info("{0} instance(s) of NzbDrone are running", otherProcesses.Count);
|
||||
}
|
||||
|
||||
return otherProcesses;
|
||||
}
|
||||
}
|
||||
}
|
@ -5,10 +5,8 @@ namespace NzbDrone.Host
|
||||
public class TerminateApplicationException : ApplicationException
|
||||
{
|
||||
public TerminateApplicationException(string reason)
|
||||
: base("Application is being terminated. Reason : " + reason)
|
||||
{
|
||||
Reason = reason;
|
||||
}
|
||||
|
||||
public string Reason { get; private set; }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user