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