2011-10-07 06:36:04 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2012-02-11 01:43:07 +00:00
|
|
|
|
using System.Net;
|
2011-10-07 06:36:04 +00:00
|
|
|
|
using System.Runtime.Remoting;
|
2012-02-11 01:43:07 +00:00
|
|
|
|
using System.Threading;
|
2011-10-07 06:36:04 +00:00
|
|
|
|
using NLog;
|
2011-10-23 05:26:43 +00:00
|
|
|
|
using NzbDrone.Common;
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Providers
|
|
|
|
|
{
|
|
|
|
|
public class MonitoringProvider
|
|
|
|
|
{
|
2012-02-11 01:43:07 +00:00
|
|
|
|
private static readonly Logger logger = LogManager.GetLogger("Host.MonitoringProvider");
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
2013-02-19 01:57:08 +00:00
|
|
|
|
private readonly IHostController _hostController;
|
2011-10-07 06:36:04 +00:00
|
|
|
|
private readonly ProcessProvider _processProvider;
|
2012-02-11 00:48:20 +00:00
|
|
|
|
private readonly HttpProvider _httpProvider;
|
2012-01-13 22:15:40 +00:00
|
|
|
|
private readonly ConfigFileProvider _configFileProvider;
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
|
|
|
|
private int _pingFailCounter;
|
|
|
|
|
private Timer _pingTimer;
|
2012-02-11 01:43:07 +00:00
|
|
|
|
private Timer _processPriorityCheckTimer;
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
2013-02-19 01:57:08 +00:00
|
|
|
|
public MonitoringProvider(ProcessProvider processProvider, IHostController hostController,
|
2012-02-11 00:48:20 +00:00
|
|
|
|
HttpProvider httpProvider, ConfigFileProvider configFileProvider)
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
|
|
|
|
_processProvider = processProvider;
|
2013-02-19 01:13:42 +00:00
|
|
|
|
_hostController = hostController;
|
2012-02-11 00:48:20 +00:00
|
|
|
|
_httpProvider = httpProvider;
|
2012-01-13 22:15:40 +00:00
|
|
|
|
_configFileProvider = configFileProvider;
|
2011-10-07 06:57:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MonitoringProvider()
|
|
|
|
|
{
|
2011-10-07 06:36:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2011-10-17 01:42:20 +00:00
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += ((s, e) => AppDomainException(e.ExceptionObject as Exception));
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += ProgramExited;
|
|
|
|
|
AppDomain.CurrentDomain.DomainUnload += ProgramExited;
|
2012-03-02 19:58:31 +00:00
|
|
|
|
|
2012-02-11 01:43:07 +00:00
|
|
|
|
_processPriorityCheckTimer = new Timer(EnsurePriority);
|
|
|
|
|
_processPriorityCheckTimer.Change(TimeSpan.FromSeconds(15), TimeSpan.FromMinutes(30));
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
2012-02-11 01:43:07 +00:00
|
|
|
|
_pingTimer = new Timer(PingServer);
|
|
|
|
|
_pingTimer.Change(TimeSpan.FromSeconds(5), TimeSpan.FromMinutes(1));
|
2011-10-07 06:36:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-11 01:43:07 +00:00
|
|
|
|
public virtual void EnsurePriority(object sender)
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
2012-03-02 19:58:31 +00:00
|
|
|
|
try
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
2012-03-02 19:58:31 +00:00
|
|
|
|
var currentProcess = _processProvider.GetCurrentProcess();
|
|
|
|
|
if (currentProcess.Priority != ProcessPriorityClass.Normal)
|
|
|
|
|
{
|
|
|
|
|
_processProvider.SetPriority(currentProcess.Id, ProcessPriorityClass.Normal);
|
|
|
|
|
}
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
2013-02-19 01:13:42 +00:00
|
|
|
|
var iisProcess = _processProvider.GetProcessById(_processProvider.GetCurrentProcess().Id);
|
2012-03-02 19:58:31 +00:00
|
|
|
|
if (iisProcess != null && iisProcess.Priority != ProcessPriorityClass.Normal &&
|
|
|
|
|
iisProcess.Priority != ProcessPriorityClass.AboveNormal)
|
|
|
|
|
{
|
|
|
|
|
_processProvider.SetPriority(iisProcess.Id, ProcessPriorityClass.Normal);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
2012-03-02 19:58:31 +00:00
|
|
|
|
logger.WarnException("Unable to verify priority", e);
|
2011-10-07 06:36:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-11 01:43:07 +00:00
|
|
|
|
public virtual void PingServer(object sender)
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
2013-02-19 01:13:42 +00:00
|
|
|
|
if (!_hostController.ServerStarted) return;
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-05-11 00:04:44 +00:00
|
|
|
|
ICredentials identity = CredentialCache.DefaultCredentials;
|
2013-02-19 01:13:42 +00:00
|
|
|
|
_httpProvider.DownloadString(_hostController.AppUrl, identity); //This should preload the home page, making the first load faster.
|
|
|
|
|
string response = _httpProvider.DownloadString(_hostController.AppUrl + "/health", identity);
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
|
|
|
|
if (!response.Contains("OK"))
|
|
|
|
|
{
|
|
|
|
|
throw new ServerException("Health services responded with an invalid response.");
|
|
|
|
|
}
|
2011-10-07 06:57:43 +00:00
|
|
|
|
|
2011-10-07 06:36:04 +00:00
|
|
|
|
if (_pingFailCounter > 0)
|
|
|
|
|
{
|
2012-02-11 01:43:07 +00:00
|
|
|
|
logger.Info("Application pool has been successfully recovered.");
|
2011-10-07 06:36:04 +00:00
|
|
|
|
}
|
2011-10-07 06:57:43 +00:00
|
|
|
|
|
2011-10-07 06:36:04 +00:00
|
|
|
|
_pingFailCounter = 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_pingFailCounter++;
|
2013-01-03 05:11:39 +00:00
|
|
|
|
logger.Error("Application pool is not responding. Count: {0} - {1}", _pingFailCounter, ex.Message);
|
|
|
|
|
if (_pingFailCounter >= 10)
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
2012-02-01 01:37:36 +00:00
|
|
|
|
_pingFailCounter = 0;
|
2013-02-19 01:13:42 +00:00
|
|
|
|
_hostController.RestartServer();
|
2011-10-07 06:36:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ProgramExited(object sender, EventArgs e)
|
|
|
|
|
{
|
2013-02-19 01:13:42 +00:00
|
|
|
|
_hostController.StopServer();
|
2011-10-07 06:36:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 01:42:20 +00:00
|
|
|
|
public static void AppDomainException(Exception excepion)
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
2011-10-15 00:59:24 +00:00
|
|
|
|
Console.WriteLine("EPIC FAIL: {0}", excepion);
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
2012-09-26 01:57:24 +00:00
|
|
|
|
|
2012-02-11 01:43:07 +00:00
|
|
|
|
logger.FatalException("EPIC FAIL: " + excepion.Message, excepion);
|
2011-10-07 06:36:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|