2011-08-05 06:49:18 +02:00
|
|
|
using System;
|
2013-03-05 02:48:20 +01:00
|
|
|
using System.Collections.Concurrent;
|
2011-04-20 09:44:13 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
2013-03-05 03:34:38 +01:00
|
|
|
using System.Threading.Tasks;
|
2011-04-20 09:44:13 +02:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2011-12-02 02:33:17 +01:00
|
|
|
using NzbDrone.Core.Providers;
|
2011-04-20 09:44:13 +02:00
|
|
|
|
2013-03-05 06:37:33 +01:00
|
|
|
namespace NzbDrone.Core.Jobs
|
2011-04-20 09:44:13 +02:00
|
|
|
{
|
2013-02-18 08:59:43 +01:00
|
|
|
public interface IJobController
|
|
|
|
{
|
2013-03-05 02:48:20 +01:00
|
|
|
bool IsProcessing { get; }
|
|
|
|
IEnumerable<JobQueueItem> Queue { get; }
|
2013-03-05 03:34:38 +01:00
|
|
|
void EnqueueScheduled();
|
|
|
|
void Enqueue(Type jobType, dynamic options = null, JobQueueItem.JobSourceType source = JobQueueItem.JobSourceType.User);
|
|
|
|
bool Enqueue(string jobTypeString);
|
2013-02-18 08:59:43 +01:00
|
|
|
}
|
|
|
|
|
2013-03-05 02:48:20 +01:00
|
|
|
public class JobController : IJobController
|
2011-04-20 09:44:13 +02:00
|
|
|
{
|
|
|
|
private readonly NotificationProvider _notificationProvider;
|
2013-01-03 02:09:13 +01:00
|
|
|
private readonly IEnumerable<IJob> _jobs;
|
2013-02-18 08:59:43 +01:00
|
|
|
private readonly IJobRepository _jobRepository;
|
2013-02-19 02:13:42 +01:00
|
|
|
private readonly Logger _logger;
|
2011-04-20 09:44:13 +02:00
|
|
|
|
2013-03-05 02:48:20 +01:00
|
|
|
private readonly BlockingCollection<JobQueueItem> _queue = new BlockingCollection<JobQueueItem>();
|
2011-04-20 09:44:13 +02:00
|
|
|
|
|
|
|
private ProgressNotification _notification;
|
2013-03-05 03:34:38 +01:00
|
|
|
private readonly CancellationTokenSource _cancellationTokenSource;
|
2011-04-20 09:44:13 +02:00
|
|
|
|
2013-02-18 08:59:43 +01:00
|
|
|
public JobController(NotificationProvider notificationProvider, IEnumerable<IJob> jobs, IJobRepository jobRepository, Logger logger)
|
2011-04-20 09:44:13 +02:00
|
|
|
{
|
|
|
|
_notificationProvider = notificationProvider;
|
|
|
|
_jobs = jobs;
|
2013-02-18 08:59:43 +01:00
|
|
|
_jobRepository = jobRepository;
|
2013-02-19 02:13:42 +01:00
|
|
|
_logger = logger;
|
2013-03-05 03:34:38 +01:00
|
|
|
_cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
Task.Factory.StartNew(ProcessQueue, _cancellationTokenSource.Token);
|
2011-04-20 09:44:13 +02:00
|
|
|
}
|
|
|
|
|
2013-03-05 02:48:20 +01:00
|
|
|
public bool IsProcessing { get; private set; }
|
|
|
|
|
|
|
|
public IEnumerable<JobQueueItem> Queue
|
2011-08-03 18:29:03 +02:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _queue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 03:34:38 +01:00
|
|
|
public void EnqueueScheduled()
|
2011-04-20 09:44:13 +02:00
|
|
|
{
|
2013-03-05 03:34:38 +01:00
|
|
|
if (IsProcessing)
|
2011-04-20 09:44:13 +02:00
|
|
|
{
|
2013-03-05 03:34:38 +01:00
|
|
|
_logger.Trace("Queue is already running. Ignoring scheduler's request.");
|
|
|
|
return;
|
2011-04-20 09:44:13 +02:00
|
|
|
}
|
|
|
|
|
2013-02-18 08:59:43 +01:00
|
|
|
var pendingJobs = _jobRepository.GetPendingJobs()
|
2013-03-25 05:36:24 +01:00
|
|
|
.Select(c => _jobs.Single(t => t.GetType().ToString() == c.Type)
|
2013-02-18 08:59:43 +01:00
|
|
|
.GetType()).ToList();
|
2011-07-11 06:53:34 +02:00
|
|
|
|
2011-04-20 09:44:13 +02:00
|
|
|
|
2013-03-05 03:34:38 +01:00
|
|
|
pendingJobs.ForEach(jobType => Enqueue(jobType, source: JobQueueItem.JobSourceType.Scheduler));
|
2013-02-19 02:13:42 +01:00
|
|
|
_logger.Trace("{0} Scheduled tasks have been added to the queue", pendingJobs.Count);
|
2011-04-20 09:44:13 +02:00
|
|
|
}
|
|
|
|
|
2013-03-05 03:34:38 +01:00
|
|
|
public void Enqueue(Type jobType, dynamic options = null, JobQueueItem.JobSourceType source = JobQueueItem.JobSourceType.User)
|
2011-04-20 09:44:13 +02:00
|
|
|
{
|
2013-03-05 02:48:20 +01:00
|
|
|
IsProcessing = true;
|
|
|
|
|
2011-11-07 07:26:21 +01:00
|
|
|
var queueItem = new JobQueueItem
|
|
|
|
{
|
|
|
|
JobType = jobType,
|
2012-09-10 21:04:17 +02:00
|
|
|
Options = options,
|
2012-02-26 23:35:45 +01:00
|
|
|
Source = source
|
2011-11-07 07:26:21 +01:00
|
|
|
};
|
2011-05-17 09:04:49 +02:00
|
|
|
|
2013-02-19 02:13:42 +01:00
|
|
|
_logger.Debug("Attempting to queue {0}", queueItem);
|
2011-11-07 07:26:21 +01:00
|
|
|
|
2013-03-05 03:34:38 +01:00
|
|
|
lock (_queue)
|
2011-07-11 06:53:34 +02:00
|
|
|
{
|
2013-03-05 03:34:38 +01:00
|
|
|
if (!_queue.Contains(queueItem))
|
2011-05-17 09:04:49 +02:00
|
|
|
{
|
2013-03-05 03:34:38 +01:00
|
|
|
_queue.Add(queueItem);
|
|
|
|
_logger.Trace("Job {0} added to the queue. current items in queue: {1}", queueItem, _queue.Count);
|
2011-07-11 06:53:34 +02:00
|
|
|
}
|
2013-03-05 03:34:38 +01:00
|
|
|
else
|
2011-04-20 09:44:13 +02:00
|
|
|
{
|
2013-03-05 03:34:38 +01:00
|
|
|
_logger.Info("{0} already exists in the queue. Skipping. current items in queue: {1}", queueItem, _queue.Count);
|
2011-04-20 09:44:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-05 03:34:38 +01:00
|
|
|
public bool Enqueue(string jobTypeString)
|
2011-12-15 09:25:16 +01:00
|
|
|
{
|
|
|
|
var type = Type.GetType(jobTypeString);
|
|
|
|
|
|
|
|
if (type == null)
|
|
|
|
return false;
|
|
|
|
|
2013-03-05 03:34:38 +01:00
|
|
|
Enqueue(type);
|
2011-12-15 09:25:16 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-17 09:04:49 +02:00
|
|
|
private void ProcessQueue()
|
|
|
|
{
|
2013-03-05 03:34:38 +01:00
|
|
|
while (true)
|
2011-05-17 09:04:49 +02:00
|
|
|
{
|
2013-03-05 03:34:38 +01:00
|
|
|
try
|
2011-05-17 09:04:49 +02:00
|
|
|
{
|
2013-03-05 02:48:20 +01:00
|
|
|
IsProcessing = false;
|
|
|
|
var item = _queue.Take();
|
2013-03-05 03:34:38 +01:00
|
|
|
Execute(item);
|
|
|
|
}
|
|
|
|
catch (ThreadAbortException e)
|
|
|
|
{
|
|
|
|
_logger.Warn(e.Message);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("Error has occurred in queue processor thread", e);
|
2013-03-05 02:48:20 +01:00
|
|
|
}
|
2011-11-07 07:26:21 +01:00
|
|
|
}
|
2011-05-17 09:04:49 +02:00
|
|
|
}
|
|
|
|
|
2011-11-07 07:26:21 +01:00
|
|
|
private void Execute(JobQueueItem queueItem)
|
2011-04-20 09:44:13 +02:00
|
|
|
{
|
2013-03-05 03:34:38 +01:00
|
|
|
IsProcessing = true;
|
|
|
|
|
2013-01-14 17:36:16 +01:00
|
|
|
var jobImplementation = _jobs.SingleOrDefault(t => t.GetType() == queueItem.JobType);
|
2011-05-17 09:24:29 +02:00
|
|
|
if (jobImplementation == null)
|
2011-04-20 09:44:13 +02:00
|
|
|
{
|
2013-02-19 02:13:42 +01:00
|
|
|
_logger.Error("Unable to locate implementation for '{0}'. Make sure it is properly registered.", queueItem.JobType);
|
2011-04-20 09:44:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-18 08:59:43 +01:00
|
|
|
var jobDefinition = _jobRepository.GetDefinition(queueItem.JobType);
|
2011-05-17 09:24:29 +02:00
|
|
|
using (_notification = new ProgressNotification(jobImplementation.Name))
|
2011-04-20 09:44:13 +02:00
|
|
|
{
|
2011-04-22 08:23:29 +02:00
|
|
|
try
|
2011-04-20 09:44:13 +02:00
|
|
|
{
|
2013-02-19 02:13:42 +01:00
|
|
|
_logger.Debug("Starting {0}. Last execution {1}", queueItem, jobDefinition.LastExecution);
|
2011-05-18 07:29:23 +02:00
|
|
|
|
2011-04-22 08:23:29 +02:00
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
2011-04-20 09:44:13 +02:00
|
|
|
_notificationProvider.Register(_notification);
|
2012-09-10 21:04:17 +02:00
|
|
|
jobImplementation.Start(_notification, queueItem.Options);
|
2011-04-20 09:44:13 +02:00
|
|
|
_notification.Status = ProgressNotificationStatus.Completed;
|
2011-05-17 17:33:32 +02:00
|
|
|
|
2013-02-18 08:59:43 +01:00
|
|
|
jobDefinition.LastExecution = DateTime.Now;
|
|
|
|
jobDefinition.Success = true;
|
2011-05-17 17:33:32 +02:00
|
|
|
|
2011-04-22 08:23:29 +02:00
|
|
|
sw.Stop();
|
2013-02-19 02:13:42 +01:00
|
|
|
_logger.Debug("Job {0} successfully completed in {1:0}.{2} seconds.", queueItem, sw.Elapsed.TotalSeconds, sw.Elapsed.Milliseconds / 100,
|
2011-11-07 07:26:21 +01:00
|
|
|
sw.Elapsed.Seconds);
|
|
|
|
}
|
2011-04-22 08:23:29 +02:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
2013-02-19 02:13:42 +01:00
|
|
|
_logger.ErrorException("An error has occurred while executing job [" + jobImplementation.Name + "].", e);
|
2011-04-22 08:23:29 +02:00
|
|
|
_notification.Status = ProgressNotificationStatus.Failed;
|
2011-05-20 09:39:05 +02:00
|
|
|
_notification.CurrentMessage = jobImplementation.Name + " Failed.";
|
|
|
|
|
2013-02-18 08:59:43 +01:00
|
|
|
jobDefinition.LastExecution = DateTime.Now;
|
|
|
|
jobDefinition.Success = false;
|
2011-04-20 09:44:13 +02:00
|
|
|
}
|
|
|
|
}
|
2011-04-22 07:46:47 +02:00
|
|
|
|
2011-08-03 18:29:03 +02:00
|
|
|
//Only update last execution status if was triggered by the scheduler
|
2012-09-10 21:04:17 +02:00
|
|
|
if (queueItem.Options == null)
|
2011-05-18 07:29:23 +02:00
|
|
|
{
|
2013-02-18 08:59:43 +01:00
|
|
|
_jobRepository.Update(jobDefinition);
|
2011-05-18 07:29:23 +02:00
|
|
|
}
|
2011-04-20 09:44:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|