2011-04-22 04:23:31 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Web.Caching;
|
|
|
|
|
using NLog;
|
2011-12-02 02:33:17 +01:00
|
|
|
|
using NzbDrone.Core.Jobs;
|
2011-04-22 04:23:31 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core
|
|
|
|
|
{
|
2011-11-07 07:26:21 +01:00
|
|
|
|
public class WebTimer
|
2011-04-22 04:23:31 +02:00
|
|
|
|
{
|
2011-04-22 07:46:47 +02:00
|
|
|
|
private readonly JobProvider _jobProvider;
|
2011-04-22 04:23:31 +02:00
|
|
|
|
|
|
|
|
|
private static CacheItemRemovedCallback _onCacheRemove;
|
2011-11-07 07:26:21 +01:00
|
|
|
|
private static bool _stop;
|
|
|
|
|
|
2011-04-22 04:23:31 +02:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-11-07 07:26:21 +01:00
|
|
|
|
|
|
|
|
|
|
2011-04-22 07:46:47 +02:00
|
|
|
|
public WebTimer(JobProvider jobProvider)
|
|
|
|
|
{
|
|
|
|
|
_jobProvider = jobProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-26 00:46:29 +01:00
|
|
|
|
//TODO: Fix this so the timer doesn't keep running during unit tests.
|
2011-04-22 04:23:31 +02:00
|
|
|
|
public void StartTimer(int secondInterval)
|
|
|
|
|
{
|
2011-11-26 00:46:29 +01:00
|
|
|
|
_onCacheRemove = DoWork;
|
2011-04-22 04:23:31 +02:00
|
|
|
|
|
|
|
|
|
HttpRuntime.Cache.Insert(GetType().ToString(), secondInterval, null,
|
|
|
|
|
DateTime.Now.AddSeconds(secondInterval), Cache.NoSlidingExpiration,
|
|
|
|
|
CacheItemPriority.NotRemovable, _onCacheRemove);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void DoWork(string k, object v, CacheItemRemovedReason r)
|
|
|
|
|
{
|
2011-11-07 07:26:21 +01:00
|
|
|
|
if (!_stop)
|
|
|
|
|
{
|
|
|
|
|
_jobProvider.QueueScheduled();
|
|
|
|
|
StartTimer(Convert.ToInt32(v));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Stop()
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Stopping Web Timer");
|
|
|
|
|
_stop = true;
|
2011-04-22 04:23:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|