2013-11-26 07:08:12 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2013-11-26 02:46:12 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common.Processes;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Host
|
|
|
|
|
{
|
|
|
|
|
public interface ISingleInstancePolicy
|
|
|
|
|
{
|
2013-11-26 07:08:12 +00:00
|
|
|
|
void PreventStartIfAlreadyRunning();
|
|
|
|
|
void KillAllOtherInstance();
|
2013-11-26 02:46:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SingleInstancePolicy : ISingleInstancePolicy
|
|
|
|
|
{
|
|
|
|
|
private readonly IProcessProvider _processProvider;
|
|
|
|
|
private readonly IBrowserService _browserService;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2014-02-10 09:49:06 +00:00
|
|
|
|
public SingleInstancePolicy(IProcessProvider processProvider,
|
|
|
|
|
IBrowserService browserService,
|
|
|
|
|
Logger logger)
|
2013-11-26 02:46:12 +00:00
|
|
|
|
{
|
|
|
|
|
_processProvider = processProvider;
|
|
|
|
|
_browserService = browserService;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-26 07:08:12 +00:00
|
|
|
|
public void PreventStartIfAlreadyRunning()
|
2013-11-26 02:46:12 +00:00
|
|
|
|
{
|
|
|
|
|
if (IsAlreadyRunning())
|
|
|
|
|
{
|
2017-03-30 03:49:38 +00:00
|
|
|
|
_logger.Warn("Another instance of Lidarr is already running.");
|
2013-11-26 02:46:12 +00:00
|
|
|
|
_browserService.LaunchWebUI();
|
|
|
|
|
throw new TerminateApplicationException("Another instance is already running");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-26 07:08:12 +00:00
|
|
|
|
public void KillAllOtherInstance()
|
|
|
|
|
{
|
|
|
|
|
foreach (var processId in GetOtherNzbDroneProcessIds())
|
|
|
|
|
{
|
|
|
|
|
_processProvider.Kill(processId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-26 02:46:12 +00:00
|
|
|
|
private bool IsAlreadyRunning()
|
2013-11-26 07:08:12 +00:00
|
|
|
|
{
|
|
|
|
|
return GetOtherNzbDroneProcessIds().Any();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<int> GetOtherNzbDroneProcessIds()
|
2013-11-26 02:46:12 +00:00
|
|
|
|
{
|
2014-08-01 22:10:31 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var currentId = _processProvider.GetCurrentProcess().Id;
|
|
|
|
|
|
2017-09-27 02:06:05 +00:00
|
|
|
|
var otherProcesses = _processProvider.FindProcessByName(ProcessProvider.LIDARR_CONSOLE_PROCESS_NAME)
|
|
|
|
|
.Union(_processProvider.FindProcessByName(ProcessProvider.LIDARR_PROCESS_NAME))
|
2014-08-01 22:10:31 +00:00
|
|
|
|
.Select(c => c.Id)
|
|
|
|
|
.Except(new[] { currentId })
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (otherProcesses.Any())
|
|
|
|
|
{
|
2017-03-30 03:49:38 +00:00
|
|
|
|
_logger.Info("{0} instance(s) of Lidarr are running", otherProcesses.Count);
|
2014-08-01 22:10:31 +00:00
|
|
|
|
}
|
2013-11-26 02:46:12 +00:00
|
|
|
|
|
2014-08-01 22:10:31 +00:00
|
|
|
|
return otherProcesses;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2013-11-26 07:08:12 +00:00
|
|
|
|
{
|
2017-03-30 03:49:38 +00:00
|
|
|
|
_logger.Warn(ex, "Failed to check for multiple instances of Lidarr.");
|
2014-08-01 22:10:31 +00:00
|
|
|
|
return new List<int>();
|
2013-11-26 07:08:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-26 02:46:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|