1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-09 12:32:31 +01:00
Radarr/NzbDrone.Core/Jobs/Scheduler.cs

69 lines
1.9 KiB
C#
Raw Normal View History

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