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