sonarr-repo-only/NzbDrone.Core/Jobs/Scheduler.cs

51 lines
1.4 KiB
C#
Raw Normal View History

2013-05-07 00:39:33 +00:00
using System;
using System.Timers;
using NzbDrone.Common.Composition;
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Lifecycle;
namespace NzbDrone.Core.Jobs
{
[Singleton]
public class Scheduler :
2013-04-19 04:46:18 +00:00
IHandle<ApplicationStartedEvent>,
IHandle<ApplicationShutdownRequested>
{
private readonly ITaskManager _taskManager;
2013-05-07 00:39:33 +00:00
private readonly IMessageAggregator _messageAggregator;
private static readonly Timer Timer = new Timer();
public Scheduler(ITaskManager taskManager, IMessageAggregator messageAggregator)
{
_taskManager = taskManager;
2013-05-07 00:39:33 +00:00
_messageAggregator = messageAggregator;
}
public void Handle(ApplicationStartedEvent message)
{
Timer.Interval = 1000 * 30;
Timer.Elapsed += (o, args) => ExecuteCommands();
Timer.Start();
}
2013-04-19 04:46:18 +00:00
2013-05-07 00:39:33 +00:00
private void ExecuteCommands()
{
var tasks = _taskManager.GetPending();
2013-05-07 00:39:33 +00:00
foreach (var task in tasks)
2013-05-07 00:39:33 +00:00
{
var commandType = Type.GetType(task.Name);
2013-05-07 00:39:33 +00:00
var command = (ICommand)Activator.CreateInstance(commandType);
_messageAggregator.PublishCommand(command);
}
}
2013-04-19 04:46:18 +00:00
public void Handle(ApplicationShutdownRequested message)
{
Timer.Stop();
2013-04-19 04:46:18 +00:00
}
}
}