From 786e38763aaf1590338be75475dec1cf709bcce3 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Fri, 10 Feb 2012 17:43:07 -0800 Subject: [PATCH] Fixed ping for servers with windows auth enabled. --- NzbDrone.App.Test/MonitoringProviderTest.cs | 2 +- NzbDrone.Common/HttpProvider.cs | 24 ++++++------ NzbDrone/NzbDrone.csproj | 2 - NzbDrone/Providers/MonitoringProvider.cs | 41 ++++++++++++--------- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/NzbDrone.App.Test/MonitoringProviderTest.cs b/NzbDrone.App.Test/MonitoringProviderTest.cs index ecc3aacd7..5e23e898d 100644 --- a/NzbDrone.App.Test/MonitoringProviderTest.cs +++ b/NzbDrone.App.Test/MonitoringProviderTest.cs @@ -29,7 +29,7 @@ namespace NzbDrone.App.Test //Act - subject.EnsurePriority(null, null); + subject.EnsurePriority(null); } diff --git a/NzbDrone.Common/HttpProvider.cs b/NzbDrone.Common/HttpProvider.cs index b93141597..68745ebe3 100644 --- a/NzbDrone.Common/HttpProvider.cs +++ b/NzbDrone.Common/HttpProvider.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; using System.IO; using System.Net; +using System.Security.Principal; using System.Text; using NLog; @@ -14,28 +15,23 @@ namespace NzbDrone.Common public virtual string DownloadString(string address) { - try - { - return new WebClient().DownloadString(address); - } - catch (Exception ex) - { - logger.TraceException(ex.Message, ex); - throw; - } + return DownloadString(address, null); } public virtual string DownloadString(string address, string username, string password) + { + return DownloadString(address, new NetworkCredential(username, password)); + } + + public virtual string DownloadString(string address, ICredentials identity) { try { - var webClient = new WebClient(); - webClient.Credentials = new NetworkCredential(username, password); - return webClient.DownloadString(address); + var client = new WebClient { Credentials = identity }; + return client.DownloadString(address); } catch (Exception ex) { - logger.Warn("Failed to get response from: {0}", address); logger.TraceException(ex.Message, ex); throw; } @@ -113,5 +109,7 @@ namespace NzbDrone.Common return responseFromServer.Replace(" ", " "); } + + } } \ No newline at end of file diff --git a/NzbDrone/NzbDrone.csproj b/NzbDrone/NzbDrone.csproj index edd2e841c..ff3a4098f 100644 --- a/NzbDrone/NzbDrone.csproj +++ b/NzbDrone/NzbDrone.csproj @@ -76,8 +76,6 @@ - - diff --git a/NzbDrone/Providers/MonitoringProvider.cs b/NzbDrone/Providers/MonitoringProvider.cs index aa51a6857..9a838cdf0 100644 --- a/NzbDrone/Providers/MonitoringProvider.cs +++ b/NzbDrone/Providers/MonitoringProvider.cs @@ -1,7 +1,8 @@ using System; using System.Diagnostics; +using System.Net; using System.Runtime.Remoting; -using System.Timers; +using System.Threading; using Exceptioneer.WindowsFormsClient; using NLog; using Ninject; @@ -12,7 +13,7 @@ namespace NzbDrone.Providers { public class MonitoringProvider { - private static readonly Logger Logger = LogManager.GetLogger("Host.MonitoringProvider"); + private static readonly Logger logger = LogManager.GetLogger("Host.MonitoringProvider"); private readonly IISProvider _iisProvider; private readonly ProcessProvider _processProvider; @@ -21,6 +22,7 @@ namespace NzbDrone.Providers private int _pingFailCounter; private Timer _pingTimer; + private Timer _processPriorityCheckTimer; [Inject] public MonitoringProvider(ProcessProvider processProvider, IISProvider iisProvider, @@ -42,18 +44,16 @@ namespace NzbDrone.Providers AppDomain.CurrentDomain.ProcessExit += ProgramExited; AppDomain.CurrentDomain.DomainUnload += ProgramExited; + + _processPriorityCheckTimer = new Timer(EnsurePriority); + _processPriorityCheckTimer.Change(TimeSpan.FromSeconds(15), TimeSpan.FromMinutes(30)); - var prioCheckTimer = new Timer(5000); - prioCheckTimer.Elapsed += EnsurePriority; - prioCheckTimer.Enabled = true; - - _pingTimer = new Timer(180000) { AutoReset = true }; - _pingTimer.Elapsed += (PingServer); - _pingTimer.Start(); + _pingTimer = new Timer(PingServer); + _pingTimer.Change(TimeSpan.FromSeconds(5), TimeSpan.FromMinutes(1)); } - public virtual void EnsurePriority(object sender, ElapsedEventArgs e) + public virtual void EnsurePriority(object sender) { var currentProcess = _processProvider.GetCurrentProcess(); if (currentProcess.Priority != ProcessPriorityClass.Normal) @@ -69,14 +69,21 @@ namespace NzbDrone.Providers } } - public virtual void PingServer(object sender, ElapsedEventArgs e) + public virtual void PingServer(object sender) { - if (!_iisProvider.ServerStarted || _configFileProvider.AuthenticationType == AuthenticationType.Windows) return; + if (!_iisProvider.ServerStarted) return; try { - _httpProvider.DownloadString(_iisProvider.AppUrl); //This should preload the home page, making the first load alot faster. - string response = _httpProvider.DownloadString(_iisProvider.AppUrl + "/health"); + ICredentials identity = null; + + if (_configFileProvider.AuthenticationType == AuthenticationType.Windows) + { + identity = CredentialCache.DefaultCredentials; + } + + _httpProvider.DownloadString(_iisProvider.AppUrl, identity); //This should preload the home page, making the first load faster. + string response = _httpProvider.DownloadString(_iisProvider.AppUrl + "/health", identity); if (!response.Contains("OK")) { @@ -85,7 +92,7 @@ namespace NzbDrone.Providers if (_pingFailCounter > 0) { - Logger.Info("Application pool has been successfully recovered."); + logger.Info("Application pool has been successfully recovered."); } _pingFailCounter = 0; @@ -93,7 +100,7 @@ namespace NzbDrone.Providers catch (Exception ex) { _pingFailCounter++; - Logger.ErrorException("Application pool is not responding. Count " + _pingFailCounter, ex); + logger.ErrorException("Application pool is not responding. Count " + _pingFailCounter, ex); if (_pingFailCounter > 4) { _pingFailCounter = 0; @@ -122,7 +129,7 @@ namespace NzbDrone.Providers }.Submit(); } - Logger.FatalException("EPIC FAIL: " + excepion.Message, excepion); + logger.FatalException("EPIC FAIL: " + excepion.Message, excepion); } } } \ No newline at end of file