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