2013-05-07 03:39:33 +03:00
|
|
|
using System;
|
2013-05-22 08:39:46 +03:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2013-05-11 02:53:50 +03:00
|
|
|
using NLog;
|
2013-05-09 09:38:20 +03:00
|
|
|
using NzbDrone.Common.Composition;
|
2013-04-24 04:56:00 +03:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-03-05 08:51:07 +03:00
|
|
|
using NzbDrone.Core.Lifecycle;
|
2013-05-22 08:39:46 +03:00
|
|
|
using Timer = System.Timers.Timer;
|
2013-03-05 08:51:07 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Jobs
|
|
|
|
{
|
2013-05-09 09:38:20 +03:00
|
|
|
[Singleton]
|
|
|
|
public class Scheduler :
|
2013-04-19 07:46:18 +03:00
|
|
|
IHandle<ApplicationStartedEvent>,
|
|
|
|
IHandle<ApplicationShutdownRequested>
|
2013-03-05 08:51:07 +03:00
|
|
|
{
|
2013-05-09 09:38:20 +03:00
|
|
|
private readonly ITaskManager _taskManager;
|
2013-05-07 03:39:33 +03:00
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
2013-05-11 02:53:50 +03:00
|
|
|
private readonly Logger _logger;
|
2013-05-09 09:38:20 +03:00
|
|
|
private static readonly Timer Timer = new Timer();
|
2013-05-22 08:39:46 +03:00
|
|
|
private static CancellationTokenSource _cancellationTokenSource;
|
2013-03-05 08:51:07 +03:00
|
|
|
|
2013-05-11 02:53:50 +03:00
|
|
|
public Scheduler(ITaskManager taskManager, IMessageAggregator messageAggregator, Logger logger)
|
2013-03-05 08:51:07 +03:00
|
|
|
{
|
2013-05-09 09:38:20 +03:00
|
|
|
_taskManager = taskManager;
|
2013-05-07 03:39:33 +03:00
|
|
|
_messageAggregator = messageAggregator;
|
2013-05-11 02:53:50 +03:00
|
|
|
_logger = logger;
|
2013-03-05 08:51:07 +03:00
|
|
|
}
|
|
|
|
|
2013-04-10 03:47:04 +03:00
|
|
|
public void Handle(ApplicationStartedEvent message)
|
2013-03-05 08:51:07 +03:00
|
|
|
{
|
2013-05-22 08:39:46 +03:00
|
|
|
_cancellationTokenSource = new CancellationTokenSource();
|
2013-05-09 09:38:20 +03:00
|
|
|
Timer.Interval = 1000 * 30;
|
2013-05-22 08:39:46 +03:00
|
|
|
Timer.Elapsed += (o, args) => Task.Factory.StartNew(ExecuteCommands, _cancellationTokenSource.Token);
|
2013-05-09 09:38:20 +03:00
|
|
|
Timer.Start();
|
2013-03-05 08:51:07 +03:00
|
|
|
}
|
2013-04-19 07:46:18 +03:00
|
|
|
|
2013-05-07 03:39:33 +03:00
|
|
|
private void ExecuteCommands()
|
|
|
|
{
|
2013-05-12 02:38:41 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Timer.Enabled = false;
|
2013-05-07 03:39:33 +03:00
|
|
|
|
2013-05-12 02:38:41 +03:00
|
|
|
var tasks = _taskManager.GetPending();
|
2013-05-11 08:59:42 +03:00
|
|
|
|
2013-05-12 02:38:41 +03:00
|
|
|
_logger.Trace("Pending Tasks: {0}", tasks.Count);
|
2013-05-11 02:53:50 +03:00
|
|
|
|
2013-05-12 02:38:41 +03:00
|
|
|
foreach (var task in tasks)
|
2013-05-11 02:53:50 +03:00
|
|
|
{
|
2013-05-22 08:39:46 +03:00
|
|
|
_cancellationTokenSource.Token.ThrowIfCancellationRequested();
|
|
|
|
|
2013-05-12 02:38:41 +03:00
|
|
|
try
|
|
|
|
{
|
2013-05-20 03:30:02 +03:00
|
|
|
_messageAggregator.PublishCommand(task.TypeName);
|
2013-05-12 02:38:41 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("Error occurred while execution task " + task.TypeName, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2013-05-22 08:39:46 +03:00
|
|
|
if (!_cancellationTokenSource.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
Timer.Enabled = true;
|
|
|
|
}
|
2013-05-07 03:39:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 07:46:18 +03:00
|
|
|
public void Handle(ApplicationShutdownRequested message)
|
|
|
|
{
|
2013-05-22 08:39:46 +03:00
|
|
|
_cancellationTokenSource.Cancel(true);
|
2013-05-09 09:38:20 +03:00
|
|
|
Timer.Stop();
|
2013-04-19 07:46:18 +03:00
|
|
|
}
|
2013-03-05 08:51:07 +03:00
|
|
|
}
|
|
|
|
}
|