From a62cd042de786ee9c58a82cdf9b7feec79a102fc Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sat, 2 Aug 2014 00:10:31 +0200 Subject: [PATCH] Added some resilience in the GetCurrentProcess calls so it doesn't cause a Fatal exit. --- .../MonitoringProviderTest.cs | 35 ------------- .../NzbDrone.Host.Test.csproj | 1 - .../EnvironmentInfo/RuntimeInfoBase.cs | 29 +++++++---- src/NzbDrone.Host/ApplicationServer.cs | 13 ++--- src/NzbDrone.Host/NzbDrone.Host.csproj | 1 - src/NzbDrone.Host/PriorityMonitor.cs | 52 ------------------- src/NzbDrone.Host/SingleInstancePolicy.cs | 31 +++++++---- 7 files changed, 43 insertions(+), 119 deletions(-) delete mode 100644 src/NzbDrone.App.Test/MonitoringProviderTest.cs delete mode 100644 src/NzbDrone.Host/PriorityMonitor.cs diff --git a/src/NzbDrone.App.Test/MonitoringProviderTest.cs b/src/NzbDrone.App.Test/MonitoringProviderTest.cs deleted file mode 100644 index e980d2c46..000000000 --- a/src/NzbDrone.App.Test/MonitoringProviderTest.cs +++ /dev/null @@ -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 - { - [Test] - public void Ensure_priority_doesnt_fail_on_invalid_process_id() - { - Mocker.GetMock().Setup(c => c.GetCurrentProcess()) - .Returns(Builder.CreateNew().Build()); - - Mocker.GetMock().Setup(c => c.GetProcessById(It.IsAny())).Returns((ProcessInfo)null); - - Subject.EnsurePriority(null); - } - - [Test] - public void Ensure_should_log_warn_exception_rather_than_throw() - { - Mocker.GetMock().Setup(c => c.GetCurrentProcess()).Throws(); - Subject.EnsurePriority(null); - - ExceptionVerification.ExpectedWarns(1); - } - } -} diff --git a/src/NzbDrone.App.Test/NzbDrone.Host.Test.csproj b/src/NzbDrone.App.Test/NzbDrone.Host.Test.csproj index a7ddd84f0..7157fa439 100644 --- a/src/NzbDrone.App.Test/NzbDrone.Host.Test.csproj +++ b/src/NzbDrone.App.Test/NzbDrone.Host.Test.csproj @@ -60,7 +60,6 @@ - diff --git a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfoBase.cs b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfoBase.cs index 6f8de3f9a..5e92b46f0 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfoBase.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfoBase.cs @@ -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 @@ public bool IsConsole { 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 @@ private static bool InternalIsProduction() 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; diff --git a/src/NzbDrone.Host/ApplicationServer.cs b/src/NzbDrone.Host/ApplicationServer.cs index c76fcc75c..bf1eb19f1 100644 --- a/src/NzbDrone.Host/ApplicationServer.cs +++ b/src/NzbDrone.Host/ApplicationServer.cs @@ -21,23 +21,20 @@ public class NzbDroneServiceFactory : ServiceBase, INzbDroneServiceFactory, IHan 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 @@ public void Start() { _browserService.LaunchWebUI(); } - - _priorityMonitor.Start(); } protected override void OnStop() diff --git a/src/NzbDrone.Host/NzbDrone.Host.csproj b/src/NzbDrone.Host/NzbDrone.Host.csproj index 67d954511..f95e3ffa2 100644 --- a/src/NzbDrone.Host/NzbDrone.Host.csproj +++ b/src/NzbDrone.Host/NzbDrone.Host.csproj @@ -116,7 +116,6 @@ - diff --git a/src/NzbDrone.Host/PriorityMonitor.cs b/src/NzbDrone.Host/PriorityMonitor.cs deleted file mode 100644 index 2972024dd..000000000 --- a/src/NzbDrone.Host/PriorityMonitor.cs +++ /dev/null @@ -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); - } - } - } - } -} \ No newline at end of file diff --git a/src/NzbDrone.Host/SingleInstancePolicy.cs b/src/NzbDrone.Host/SingleInstancePolicy.cs index a1b489705..4e8d44790 100644 --- a/src/NzbDrone.Host/SingleInstancePolicy.cs +++ b/src/NzbDrone.Host/SingleInstancePolicy.cs @@ -52,19 +52,28 @@ private bool IsAlreadyRunning() private List 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(); + } } } } \ No newline at end of file