2013-05-07 00:39:33 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Timers;
|
2013-04-24 01:56:00 +00:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-03-05 05:51:07 +00:00
|
|
|
|
using NzbDrone.Core.Lifecycle;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Jobs
|
|
|
|
|
{
|
2013-04-19 04:46:18 +00:00
|
|
|
|
public class JobTimer :
|
|
|
|
|
IHandle<ApplicationStartedEvent>,
|
|
|
|
|
IHandle<ApplicationShutdownRequested>
|
2013-03-05 05:51:07 +00:00
|
|
|
|
{
|
2013-05-07 00:39:33 +00:00
|
|
|
|
private readonly IJobRepository _jobRepository;
|
|
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
2013-03-05 05:51:07 +00:00
|
|
|
|
private readonly Timer _timer;
|
|
|
|
|
|
2013-05-07 00:39:33 +00:00
|
|
|
|
public JobTimer(IJobRepository jobRepository, IMessageAggregator messageAggregator)
|
2013-03-05 05:51:07 +00:00
|
|
|
|
{
|
2013-05-07 00:39:33 +00:00
|
|
|
|
_jobRepository = jobRepository;
|
|
|
|
|
_messageAggregator = messageAggregator;
|
2013-03-05 05:51:07 +00:00
|
|
|
|
_timer = new Timer();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-10 00:47:04 +00:00
|
|
|
|
public void Handle(ApplicationStartedEvent message)
|
2013-03-05 05:51:07 +00:00
|
|
|
|
{
|
|
|
|
|
_timer.Interval = 1000 * 30;
|
2013-05-07 00:39:33 +00:00
|
|
|
|
_timer.Elapsed += (o, args) => ExecuteCommands();
|
2013-05-07 04:31:55 +00:00
|
|
|
|
//_timer.Start();
|
2013-03-05 05:51:07 +00:00
|
|
|
|
}
|
2013-04-19 04:46:18 +00:00
|
|
|
|
|
2013-05-07 00:39:33 +00:00
|
|
|
|
private void ExecuteCommands()
|
|
|
|
|
{
|
|
|
|
|
var jobs = _jobRepository.GetPendingJobs();
|
|
|
|
|
|
|
|
|
|
foreach (var jobDefinition in jobs)
|
|
|
|
|
{
|
|
|
|
|
var commandType = Type.GetType(jobDefinition.Name);
|
|
|
|
|
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-03-05 05:51:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|