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

pull/100/merge
Taloth Saldono 10 years ago committed by Mark McDowall
parent 10de8087f7
commit a62cd042de

@ -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);
}
}
}

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

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

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

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

@ -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);
}
}
}
}
}

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