|
|
@ -4,6 +4,7 @@ using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading;
|
|
|
|
using NLog;
|
|
|
|
using NLog;
|
|
|
|
|
|
|
|
using NzbDrone.Core.Lifecycle;
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
|
|
|
using NzbDrone.Core.Providers;
|
|
|
|
using NzbDrone.Core.Providers;
|
|
|
@ -19,12 +20,13 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
bool QueueJob(string jobTypeString);
|
|
|
|
bool QueueJob(string jobTypeString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class JobController : IJobController
|
|
|
|
public class JobController : IJobController, IInitializable
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly NotificationProvider _notificationProvider;
|
|
|
|
private readonly NotificationProvider _notificationProvider;
|
|
|
|
private readonly IEnumerable<IJob> _jobs;
|
|
|
|
private readonly IEnumerable<IJob> _jobs;
|
|
|
|
private readonly IJobRepository _jobRepository;
|
|
|
|
private readonly IJobRepository _jobRepository;
|
|
|
|
private readonly Logger logger;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
private Timer _timer;
|
|
|
|
|
|
|
|
|
|
|
|
private Thread _jobThread;
|
|
|
|
private Thread _jobThread;
|
|
|
|
public Stopwatch StopWatch { get; private set; }
|
|
|
|
public Stopwatch StopWatch { get; private set; }
|
|
|
@ -41,10 +43,16 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
_notificationProvider = notificationProvider;
|
|
|
|
_notificationProvider = notificationProvider;
|
|
|
|
_jobs = jobs;
|
|
|
|
_jobs = jobs;
|
|
|
|
_jobRepository = jobRepository;
|
|
|
|
_jobRepository = jobRepository;
|
|
|
|
this.logger = logger;
|
|
|
|
_logger = logger;
|
|
|
|
ResetThread();
|
|
|
|
ResetThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_timer = new Timer(c => QueueScheduled());
|
|
|
|
|
|
|
|
_timer.Change(0, 60 * 1000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<JobQueueItem> Queue
|
|
|
|
public List<JobQueueItem> Queue
|
|
|
|
{
|
|
|
|
{
|
|
|
|
get
|
|
|
|
get
|
|
|
@ -62,7 +70,7 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
|
|
|
|
|
|
|
|
if (_jobThread.IsAlive)
|
|
|
|
if (_jobThread.IsAlive)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logger.Trace("Queue is already running. Ignoring scheduler's request.");
|
|
|
|
_logger.Trace("Queue is already running. Ignoring scheduler's request.");
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -73,7 +81,7 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pendingJobs.ForEach(jobType => QueueJob(jobType, source: JobQueueItem.JobSourceType.Scheduler));
|
|
|
|
pendingJobs.ForEach(jobType => QueueJob(jobType, source: JobQueueItem.JobSourceType.Scheduler));
|
|
|
|
logger.Trace("{0} Scheduled tasks have been added to the queue", pendingJobs.Count);
|
|
|
|
_logger.Trace("{0} Scheduled tasks have been added to the queue", pendingJobs.Count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void QueueJob(Type jobType, dynamic options = null, JobQueueItem.JobSourceType source = JobQueueItem.JobSourceType.User)
|
|
|
|
public virtual void QueueJob(Type jobType, dynamic options = null, JobQueueItem.JobSourceType source = JobQueueItem.JobSourceType.User)
|
|
|
@ -85,7 +93,7 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
Source = source
|
|
|
|
Source = source
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
logger.Debug("Attempting to queue {0}", queueItem);
|
|
|
|
_logger.Debug("Attempting to queue {0}", queueItem);
|
|
|
|
|
|
|
|
|
|
|
|
lock (_executionLock)
|
|
|
|
lock (_executionLock)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -96,17 +104,17 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
if (!Queue.Contains(queueItem))
|
|
|
|
if (!Queue.Contains(queueItem))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Queue.Add(queueItem);
|
|
|
|
Queue.Add(queueItem);
|
|
|
|
logger.Trace("Job {0} added to the queue. current items in queue: {1}", queueItem, Queue.Count);
|
|
|
|
_logger.Trace("Job {0} added to the queue. current items in queue: {1}", queueItem, Queue.Count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logger.Info("{0} already exists in the queue. Skipping. current items in queue: {1}", queueItem, Queue.Count);
|
|
|
|
_logger.Info("{0} already exists in the queue. Skipping. current items in queue: {1}", queueItem, Queue.Count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (_jobThread.IsAlive)
|
|
|
|
if (_jobThread.IsAlive)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logger.Trace("Queue is already running. No need to start it up.");
|
|
|
|
_logger.Trace("Queue is already running. No need to start it up.");
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -145,7 +153,7 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
if (Queue.Count != 0)
|
|
|
|
if (Queue.Count != 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
job = Queue.OrderBy(c => c.Source).First();
|
|
|
|
job = Queue.OrderBy(c => c.Source).First();
|
|
|
|
logger.Trace("Popping {0} from the queue.", job);
|
|
|
|
_logger.Trace("Popping {0} from the queue.", job);
|
|
|
|
Queue.Remove(job);
|
|
|
|
Queue.Remove(job);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -161,7 +169,7 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logger.FatalException("An error has occurred while executing job.", e);
|
|
|
|
_logger.FatalException("An error has occurred while executing job.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -169,16 +177,16 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ThreadAbortException e)
|
|
|
|
catch (ThreadAbortException e)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logger.Warn(e.Message);
|
|
|
|
_logger.Warn(e.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logger.ErrorException("Error has occurred in queue processor thread", e);
|
|
|
|
_logger.ErrorException("Error has occurred in queue processor thread", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
{
|
|
|
|
StopWatch.Stop();
|
|
|
|
StopWatch.Stop();
|
|
|
|
logger.Trace("Finished processing jobs in the queue.");
|
|
|
|
_logger.Trace("Finished processing jobs in the queue.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -187,7 +195,7 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
var jobImplementation = _jobs.SingleOrDefault(t => t.GetType() == queueItem.JobType);
|
|
|
|
var jobImplementation = _jobs.SingleOrDefault(t => t.GetType() == queueItem.JobType);
|
|
|
|
if (jobImplementation == null)
|
|
|
|
if (jobImplementation == null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logger.Error("Unable to locate implementation for '{0}'. Make sure it is properly registered.", queueItem.JobType);
|
|
|
|
_logger.Error("Unable to locate implementation for '{0}'. Make sure it is properly registered.", queueItem.JobType);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -196,7 +204,7 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logger.Debug("Starting {0}. Last execution {1}", queueItem, jobDefinition.LastExecution);
|
|
|
|
_logger.Debug("Starting {0}. Last execution {1}", queueItem, jobDefinition.LastExecution);
|
|
|
|
|
|
|
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
|
|
|
|
|
|
@ -208,7 +216,7 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
jobDefinition.Success = true;
|
|
|
|
jobDefinition.Success = true;
|
|
|
|
|
|
|
|
|
|
|
|
sw.Stop();
|
|
|
|
sw.Stop();
|
|
|
|
logger.Debug("Job {0} successfully completed in {1:0}.{2} seconds.", queueItem, sw.Elapsed.TotalSeconds, sw.Elapsed.Milliseconds / 100,
|
|
|
|
_logger.Debug("Job {0} successfully completed in {1:0}.{2} seconds.", queueItem, sw.Elapsed.TotalSeconds, sw.Elapsed.Milliseconds / 100,
|
|
|
|
sw.Elapsed.Seconds);
|
|
|
|
sw.Elapsed.Seconds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ThreadAbortException)
|
|
|
|
catch (ThreadAbortException)
|
|
|
@ -217,7 +225,7 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logger.ErrorException("An error has occurred while executing job [" + jobImplementation.Name + "].", e);
|
|
|
|
_logger.ErrorException("An error has occurred while executing job [" + jobImplementation.Name + "].", e);
|
|
|
|
_notification.Status = ProgressNotificationStatus.Failed;
|
|
|
|
_notification.Status = ProgressNotificationStatus.Failed;
|
|
|
|
_notification.CurrentMessage = jobImplementation.Name + " Failed.";
|
|
|
|
_notification.CurrentMessage = jobImplementation.Name + " Failed.";
|
|
|
|
|
|
|
|
|
|
|
@ -237,7 +245,7 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (StopWatch.Elapsed.TotalHours > 1)
|
|
|
|
if (StopWatch.Elapsed.TotalHours > 1)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logger.Warn("Thread job has been running for more than an hour. fuck it!");
|
|
|
|
_logger.Warn("Thread job has been running for more than an hour. fuck it!");
|
|
|
|
ResetThread();
|
|
|
|
ResetThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -249,8 +257,10 @@ namespace NzbDrone.Core.Jobs
|
|
|
|
_jobThread.Abort();
|
|
|
|
_jobThread.Abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.Trace("resetting queue processor thread");
|
|
|
|
_logger.Trace("resetting queue processor thread");
|
|
|
|
_jobThread = new Thread(ProcessQueue) { Name = "JobQueueThread" };
|
|
|
|
_jobThread = new Thread(ProcessQueue) { Name = "JobQueueThread" };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|