2011-04-22 02:23:31 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Web.Caching;
|
|
|
|
|
using NLog;
|
2011-12-02 01:33:17 +00:00
|
|
|
|
using NzbDrone.Core.Jobs;
|
2011-04-22 05:46:47 +00:00
|
|
|
|
using NzbDrone.Core.Providers.Jobs;
|
2011-04-22 02:23:31 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core
|
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
public class WebTimer
|
2011-04-22 02:23:31 +00:00
|
|
|
|
{
|
2011-04-22 05:46:47 +00:00
|
|
|
|
private readonly JobProvider _jobProvider;
|
2011-04-22 02:23:31 +00:00
|
|
|
|
|
|
|
|
|
private static CacheItemRemovedCallback _onCacheRemove;
|
2011-11-07 06:26:21 +00:00
|
|
|
|
private static bool _stop;
|
|
|
|
|
|
2011-04-22 02:23:31 +00:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
|
|
|
|
|
|
2011-04-22 05:46:47 +00:00
|
|
|
|
public WebTimer(JobProvider jobProvider)
|
|
|
|
|
{
|
|
|
|
|
_jobProvider = jobProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 23:46:29 +00:00
|
|
|
|
//TODO: Fix this so the timer doesn't keep running during unit tests.
|
2011-04-22 02:23:31 +00:00
|
|
|
|
public void StartTimer(int secondInterval)
|
|
|
|
|
{
|
2011-11-25 23:46:29 +00:00
|
|
|
|
_onCacheRemove = DoWork;
|
2011-04-22 02:23:31 +00: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 06:26:21 +00:00
|
|
|
|
if (!_stop)
|
|
|
|
|
{
|
|
|
|
|
_jobProvider.QueueScheduled();
|
|
|
|
|
StartTimer(Convert.ToInt32(v));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Stop()
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Stopping Web Timer");
|
|
|
|
|
_stop = true;
|
2011-04-22 02:23:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|