1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

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>
<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" />

View File

@ -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 @@ public bool IsConsole
{ {
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 @@ private static bool InternalIsProduction()
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;

View File

@ -21,23 +21,20 @@ public class NzbDroneServiceFactory : ServiceBase, INzbDroneServiceFactory, IHan
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;
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)
{ {
_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 @@ public void Start()
{ {
_browserService.LaunchWebUI(); _browserService.LaunchWebUI();
} }
_priorityMonitor.Start();
} }
protected override void OnStop() protected override void OnStop()

View File

@ -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" />

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 @@ private bool IsAlreadyRunning()
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))
.Select(c => c.Id)
.Except(new[] {currentId})
.ToList();
if (otherProcesses.Any())
{ {
_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>();
}
} }
} }
} }