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

126 lines
4.3 KiB
C#
Raw Normal View History

2011-10-07 09:36:04 +03:00
using System;
using System.Diagnostics;
using System.Net;
2011-10-07 09:36:04 +03:00
using System.Runtime.Remoting;
using System.Threading;
2011-10-07 09:36:04 +03:00
using NLog;
using Ninject;
using NzbDrone.Common;
using NzbDrone.Common.Model;
2011-10-07 09:36:04 +03:00
namespace NzbDrone.Providers
{
public class MonitoringProvider
{
private static readonly Logger logger = LogManager.GetLogger("Host.MonitoringProvider");
2011-10-07 09:36:04 +03:00
private readonly IISProvider _iisProvider;
private readonly ProcessProvider _processProvider;
2012-02-11 03:48:20 +03:00
private readonly HttpProvider _httpProvider;
private readonly ConfigFileProvider _configFileProvider;
2011-10-07 09:36:04 +03:00
private int _pingFailCounter;
private Timer _pingTimer;
private Timer _processPriorityCheckTimer;
2011-10-07 09:36:04 +03:00
[Inject]
2011-10-07 09:57:43 +03:00
public MonitoringProvider(ProcessProvider processProvider, IISProvider iisProvider,
2012-02-11 03:48:20 +03:00
HttpProvider httpProvider, ConfigFileProvider configFileProvider)
2011-10-07 09:36:04 +03:00
{
_processProvider = processProvider;
_iisProvider = iisProvider;
2012-02-11 03:48:20 +03:00
_httpProvider = httpProvider;
_configFileProvider = configFileProvider;
2011-10-07 09:57:43 +03:00
}
public MonitoringProvider()
{
2011-10-07 09:36:04 +03:00
}
public void Start()
{
AppDomain.CurrentDomain.UnhandledException += ((s, e) => AppDomainException(e.ExceptionObject as Exception));
2011-10-07 09:36:04 +03:00
AppDomain.CurrentDomain.ProcessExit += ProgramExited;
AppDomain.CurrentDomain.DomainUnload += ProgramExited;
_processPriorityCheckTimer = new Timer(EnsurePriority);
_processPriorityCheckTimer.Change(TimeSpan.FromSeconds(15), TimeSpan.FromMinutes(30));
2011-10-07 09:36:04 +03:00
_pingTimer = new Timer(PingServer);
_pingTimer.Change(TimeSpan.FromSeconds(5), TimeSpan.FromMinutes(1));
2011-10-07 09:36:04 +03:00
}
public virtual void EnsurePriority(object sender)
2011-10-07 09:36:04 +03:00
{
try
2011-10-07 09:36:04 +03:00
{
var currentProcess = _processProvider.GetCurrentProcess();
if (currentProcess.Priority != ProcessPriorityClass.Normal)
{
_processProvider.SetPriority(currentProcess.Id, ProcessPriorityClass.Normal);
}
2011-10-07 09:36:04 +03:00
var iisProcess = _processProvider.GetProcessById(_iisProvider.IISProcessId);
if (iisProcess != null && iisProcess.Priority != ProcessPriorityClass.Normal &&
iisProcess.Priority != ProcessPriorityClass.AboveNormal)
{
_processProvider.SetPriority(iisProcess.Id, ProcessPriorityClass.Normal);
}
}
catch (Exception e)
2011-10-07 09:36:04 +03:00
{
logger.WarnException("Unable to verify priority", e);
2011-10-07 09:36:04 +03:00
}
}
public virtual void PingServer(object sender)
2011-10-07 09:36:04 +03:00
{
if (!_iisProvider.ServerStarted) return;
2011-10-07 09:36:04 +03:00
try
{
2012-05-11 03:04:44 +03:00
ICredentials 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);
2011-10-07 09:36:04 +03:00
if (!response.Contains("OK"))
{
throw new ServerException("Health services responded with an invalid response.");
}
2011-10-07 09:57:43 +03:00
2011-10-07 09:36:04 +03:00
if (_pingFailCounter > 0)
{
logger.Info("Application pool has been successfully recovered.");
2011-10-07 09:36:04 +03:00
}
2011-10-07 09:57:43 +03:00
2011-10-07 09:36:04 +03:00
_pingFailCounter = 0;
}
catch (Exception ex)
{
_pingFailCounter++;
2012-05-11 03:04:44 +03:00
logger.Error("Application pool is not responding. Count " + _pingFailCounter + ex.Message);
if (_pingFailCounter > 10)
2011-10-07 09:36:04 +03:00
{
_pingFailCounter = 0;
2012-05-11 03:04:44 +03:00
_iisProvider.RestartServer();
2011-10-07 09:36:04 +03:00
}
}
}
private void ProgramExited(object sender, EventArgs e)
{
_iisProvider.StopServer();
}
public static void AppDomainException(Exception excepion)
2011-10-07 09:36:04 +03:00
{
Console.WriteLine("EPIC FAIL: {0}", excepion);
2011-10-07 09:36:04 +03:00
logger.FatalException("EPIC FAIL: " + excepion.Message, excepion);
2011-10-07 09:36:04 +03:00
}
}
}