Sonarr/NzbDrone/Providers/MonitoringProvider.cs

61 lines
2.0 KiB
C#
Raw Normal View History

2011-10-07 06:36:04 +00:00
using System;
using System.Diagnostics;
using System.Threading;
2011-10-07 06:36:04 +00:00
using NLog;
using NzbDrone.Common;
2011-10-07 06:36:04 +00:00
namespace NzbDrone.Providers
{
public class MonitoringProvider
{
private static readonly Logger logger = LogManager.GetLogger("Host.MonitoringProvider");
2011-10-07 06:36:04 +00:00
private readonly ProcessProvider _processProvider;
private Timer _processPriorityCheckTimer;
2011-10-07 06:36:04 +00:00
2013-02-21 04:47:06 +00:00
public MonitoringProvider(ProcessProvider processProvider)
2011-10-07 06:36:04 +00:00
{
_processProvider = processProvider;
}
public void Start()
{
AppDomain.CurrentDomain.UnhandledException += ((s, e) => AppDomainException(e.ExceptionObject as Exception));
2011-10-07 06:36:04 +00:00
_processPriorityCheckTimer = new Timer(EnsurePriority);
_processPriorityCheckTimer.Change(TimeSpan.FromSeconds(15), TimeSpan.FromMinutes(30));
2011-10-07 06:36:04 +00:00
}
public virtual void EnsurePriority(object sender)
2011-10-07 06:36:04 +00:00
{
try
2011-10-07 06:36:04 +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);
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
{
logger.WarnException("Unable to verify priority", e);
2011-10-07 06:36:04 +00:00
}
}
public static void AppDomainException(Exception excepion)
2011-10-07 06:36:04 +00:00
{
Console.WriteLine("EPIC FAIL: {0}", excepion);
logger.FatalException("EPIC FAIL: " + excepion.Message, excepion);
2011-10-07 06:36:04 +00:00
}
}
}