Added some resilience in the GetCurrentProcess calls so it doesn't cause a Fatal exit.

This commit is contained in:
Taloth Saldono 2014-08-02 00:10:31 +02:00 committed by Mark McDowall
parent 10de8087f7
commit a62cd042de
7 changed files with 43 additions and 119 deletions

View File

@ -1,35 +0,0 @@
using System;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Common.Model;
using NzbDrone.Common.Processes;
using NzbDrone.Host;
using NzbDrone.Test.Common;
namespace NzbDrone.App.Test
{
[TestFixture]
public class MonitoringProviderTest : TestBase<PriorityMonitor>
{
[Test]
public void Ensure_priority_doesnt_fail_on_invalid_process_id()
{
Mocker.GetMock<IProcessProvider>().Setup(c => c.GetCurrentProcess())
.Returns(Builder<ProcessInfo>.CreateNew().Build());
Mocker.GetMock<IProcessProvider>().Setup(c => c.GetProcessById(It.IsAny<int>())).Returns((ProcessInfo)null);
Subject.EnsurePriority(null);
}
[Test]
public void Ensure_should_log_warn_exception_rather_than_throw()
{
Mocker.GetMock<IProcessProvider>().Setup(c => c.GetCurrentProcess()).Throws<InvalidOperationException>();
Subject.EnsurePriority(null);
ExceptionVerification.ExpectedWarns(1);
}
}
}

View File

@ -60,7 +60,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ContainerFixture.cs" />
<Compile Include="MonitoringProviderTest.cs" />
<Compile Include="NzbDroneProcessServiceFixture.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RouterTest.cs" />

View File

@ -12,7 +12,6 @@ namespace NzbDrone.Common.EnvironmentInfo
public abstract class RuntimeInfoBase : IRuntimeInfo
{
private readonly Logger _logger;
private static readonly string ProcessName = Process.GetCurrentProcess().ProcessName.ToLower();
public RuntimeInfoBase(IServiceProvider serviceProvider, Logger logger)
{
@ -73,10 +72,12 @@ namespace NzbDrone.Common.EnvironmentInfo
{
get
{
return (OsInfo.IsWindows &&
IsUserInteractive &&
ProcessName.Equals(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME, StringComparison.InvariantCultureIgnoreCase)) ||
OsInfo.IsMono;
if (OsInfo.IsWindows)
{
return IsUserInteractive && Process.GetCurrentProcess().ProcessName.Equals(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME, StringComparison.InvariantCultureIgnoreCase);
}
return true;
}
}
@ -93,11 +94,19 @@ namespace NzbDrone.Common.EnvironmentInfo
if (BuildInfo.IsDebug || Debugger.IsAttached) return false;
if (BuildInfo.Version.Revision > 10000) return false; //Official builds will never have such a high revision
var lowerProcessName = ProcessName.ToLower();
if (lowerProcessName.Contains("vshost")) return false;
if (lowerProcessName.Contains("nunit")) return false;
if (lowerProcessName.Contains("jetbrain")) return false;
if (lowerProcessName.Contains("resharper")) return false;
try
{
var lowerProcessName = Process.GetCurrentProcess().ProcessName.ToLower();
if (lowerProcessName.Contains("vshost")) return false;
if (lowerProcessName.Contains("nunit")) return false;
if (lowerProcessName.Contains("jetbrain")) return false;
if (lowerProcessName.Contains("resharper")) return false;
}
catch
{
}
string lowerCurrentDir = Directory.GetCurrentDirectory().ToLower();
if (lowerCurrentDir.Contains("teamcity")) return false;

View File

@ -21,23 +21,20 @@ namespace NzbDrone.Host
private readonly IConfigFileProvider _configFileProvider;
private readonly IRuntimeInfo _runtimeInfo;
private readonly IHostController _hostController;
private readonly PriorityMonitor _priorityMonitor;
private readonly IStartupContext _startupContext;
private readonly IBrowserService _browserService;
private readonly Logger _logger;
public NzbDroneServiceFactory(IConfigFileProvider configFileProvider,
public NzbDroneServiceFactory(IConfigFileProvider configFileProvider,
IHostController hostController,
IRuntimeInfo runtimeInfo,
PriorityMonitor priorityMonitor,
IStartupContext startupContext,
IBrowserService browserService,
IRuntimeInfo runtimeInfo,
IStartupContext startupContext,
IBrowserService browserService,
Logger logger)
{
_configFileProvider = configFileProvider;
_hostController = hostController;
_runtimeInfo = runtimeInfo;
_priorityMonitor = priorityMonitor;
_startupContext = startupContext;
_browserService = browserService;
_logger = logger;
@ -63,8 +60,6 @@ namespace NzbDrone.Host
{
_browserService.LaunchWebUI();
}
_priorityMonitor.Start();
}
protected override void OnStop()

View File

@ -116,7 +116,6 @@
<Compile Include="Owin\OwinTraceOutputFactory.cs" />
<Compile Include="Owin\PortInUseException.cs" />
<Compile Include="PlatformValidation.cs" />
<Compile Include="PriorityMonitor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Router.cs" />
<Compile Include="SingleInstancePolicy.cs" />

View File

@ -1,52 +0,0 @@
using System;
using System.Diagnostics;
using System.Threading;
using NLog;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
namespace NzbDrone.Host
{
public class PriorityMonitor
{
private readonly IProcessProvider _processProvider;
private readonly Logger _logger;
private Timer _processPriorityCheckTimer;
public PriorityMonitor(IProcessProvider processProvider, Logger logger)
{
_processProvider = processProvider;
_logger = logger;
}
public void Start()
{
_processPriorityCheckTimer = new Timer(EnsurePriority);
_processPriorityCheckTimer.Change(TimeSpan.FromSeconds(15), TimeSpan.FromMinutes(30));
}
public virtual void EnsurePriority(object sender)
{
try
{
if (_processProvider.GetCurrentProcessPriority() != ProcessPriorityClass.Normal)
{
_processProvider.SetPriority(_processProvider.GetCurrentProcess().Id, ProcessPriorityClass.Normal);
}
}
catch (Exception e)
{
if (OsInfo.IsMono)
{
_logger.TraceException("Unable to verify priority", e);
}
else
{
_logger.WarnException("Unable to verify priority", e);
}
}
}
}
}

View File

@ -52,19 +52,28 @@ namespace NzbDrone.Host
private List<int> GetOtherNzbDroneProcessIds()
{
var currentId = _processProvider.GetCurrentProcess().Id;
var otherProcesses = _processProvider.FindProcessByName(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME)
.Union(_processProvider.FindProcessByName(ProcessProvider.NZB_DRONE_PROCESS_NAME))
.Select(c => c.Id)
.Except(new[] {currentId})
.ToList();
if (otherProcesses.Any())
try
{
_logger.Info("{0} instance(s) of NzbDrone are running", otherProcesses.Count);
}
var currentId = _processProvider.GetCurrentProcess().Id;
return otherProcesses;
var otherProcesses = _processProvider.FindProcessByName(ProcessProvider.NZB_DRONE_CONSOLE_PROCESS_NAME)
.Union(_processProvider.FindProcessByName(ProcessProvider.NZB_DRONE_PROCESS_NAME))
.Select(c => c.Id)
.Except(new[] { currentId })
.ToList();
if (otherProcesses.Any())
{
_logger.Info("{0} instance(s) of NzbDrone are running", otherProcesses.Count);
}
return otherProcesses;
}
catch (Exception ex)
{
_logger.WarnException("Failed to check for multiple instances of NzbDrone.", ex);
return new List<int>();
}
}
}
}