2011-04-22 04:23:31 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Web.Caching;
|
|
|
|
|
using NLog;
|
2011-04-22 07:46:47 +02:00
|
|
|
|
using NzbDrone.Core.Providers.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-04 00:23:54 +01:00
|
|
|
|
//TODO: Make timer doesn't keep running during unit tests.
|
2011-04-22 04:23:31 +02:00
|
|
|
|
public void StartTimer(int secondInterval)
|
|
|
|
|
{
|
|
|
|
|
_onCacheRemove = new CacheItemRemovedCallback(DoWork);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|