2011-08-05 04:49:18 +00:00
|
|
|
|
//https://github.com/kayone/NzbDrone/blob/master/NzbDrone.Core/Providers/Jobs/JobProvider.cs
|
|
|
|
|
using System;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
using System.Collections.Generic;
|
2011-08-03 16:29:03 +00:00
|
|
|
|
using System.ComponentModel;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2011-06-14 01:23:04 +00:00
|
|
|
|
using Ninject;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
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;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-17 19:50:49 +00:00
|
|
|
|
using PetaPoco;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers.Jobs
|
|
|
|
|
{
|
2011-08-03 16:29:03 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides a background task runner, tasks could be queue either by the scheduler using QueueScheduled()
|
|
|
|
|
/// or by explicitly calling QueueJob(type,int)
|
|
|
|
|
/// </summary>
|
2011-04-20 07:44:13 +00:00
|
|
|
|
public class JobProvider
|
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2011-06-17 19:50:49 +00:00
|
|
|
|
private readonly IDatabase _database;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
private readonly NotificationProvider _notificationProvider;
|
2011-05-24 00:34:57 +00:00
|
|
|
|
private readonly IList<IJob> _jobs;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
|
|
|
|
private Thread _jobThread;
|
2011-11-07 06:26:21 +00:00
|
|
|
|
private Stopwatch _jobThreadStopWatch;
|
2011-07-11 00:03:01 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +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-06-14 01:23:04 +00:00
|
|
|
|
[Inject]
|
2011-06-17 19:50:49 +00:00
|
|
|
|
public JobProvider(IDatabase database, NotificationProvider notificationProvider, IList<IJob> jobs)
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
2011-06-17 19:50:49 +00:00
|
|
|
|
_database = database;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
_notificationProvider = notificationProvider;
|
|
|
|
|
_jobs = jobs;
|
2011-11-07 06:26:21 +00:00
|
|
|
|
ResetThread();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-08-03 16:29:03 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="JobProvider"/> class. by AutoMoq
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>Should only be used by AutoMoq</remarks>
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
2011-04-20 07:44:13 +00:00
|
|
|
|
public JobProvider() { }
|
|
|
|
|
|
2011-08-03 16:29:03 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the active queue.
|
|
|
|
|
/// </summary>
|
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
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a list of all registered jobs
|
|
|
|
|
/// </summary>
|
2011-07-08 03:27:11 +00:00
|
|
|
|
public virtual List<JobDefinition> All()
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
2011-07-08 03:27:11 +00:00
|
|
|
|
return _database.Fetch<JobDefinition>().ToList();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes jobs in the database using the IJob instances that are
|
|
|
|
|
/// registered using ninject
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual void Initialize()
|
|
|
|
|
{
|
|
|
|
|
logger.Debug("Initializing jobs. Count {0}", _jobs.Count());
|
|
|
|
|
var currentTimer = All();
|
|
|
|
|
|
|
|
|
|
foreach (var timer in _jobs)
|
|
|
|
|
{
|
|
|
|
|
var timerProviderLocal = timer;
|
|
|
|
|
if (!currentTimer.Exists(c => c.TypeName == timerProviderLocal.GetType().ToString()))
|
|
|
|
|
{
|
|
|
|
|
var settings = new JobDefinition
|
|
|
|
|
{
|
|
|
|
|
Enable = timerProviderLocal.DefaultInterval > 0,
|
|
|
|
|
TypeName = timer.GetType().ToString(),
|
|
|
|
|
Name = timerProviderLocal.Name,
|
|
|
|
|
Interval = timerProviderLocal.DefaultInterval,
|
|
|
|
|
LastExecution = new DateTime(2000, 1, 1)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SaveDefinition(settings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-20 07:44:13 +00:00
|
|
|
|
/// <summary>
|
2011-08-03 16:29:03 +00:00
|
|
|
|
/// Adds/Updates definitions for a job
|
2011-04-20 07:44:13 +00:00
|
|
|
|
/// </summary>
|
2011-08-03 16:29:03 +00:00
|
|
|
|
/// <param name="definitions">Settings to be added/updated</param>
|
|
|
|
|
public virtual void SaveDefinition(JobDefinition definitions)
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
2011-07-08 03:27:11 +00:00
|
|
|
|
if (definitions.Id == 0)
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
logger.Trace("Adding job definitions for {0}", definitions.Name);
|
2011-07-08 03:27:11 +00:00
|
|
|
|
_database.Insert(definitions);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
logger.Trace("Updating job definitions for {0}", definitions.Name);
|
2011-07-08 03:27:11 +00:00
|
|
|
|
_database.Update(definitions);
|
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
|
|
|
|
{
|
2011-11-07 06:26:21 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var pendingJobTypes = All().Where(
|
2011-07-11 04:53:34 +00:00
|
|
|
|
t => t.Enable &&
|
|
|
|
|
(DateTime.Now - t.LastExecution) > TimeSpan.FromMinutes(t.Interval)
|
2011-11-07 06:26:21 +00:00
|
|
|
|
).Select(c => _jobs.Where(t => t.GetType().ToString() == c.TypeName).Single().GetType()).ToList();
|
2011-07-11 04:53:34 +00:00
|
|
|
|
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
pendingJobTypes.ForEach(jobType => QueueJob(jobType));
|
|
|
|
|
logger.Trace("{0} Scheduled tasks have been added to the queue", pendingJobTypes.Count);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2011-11-07 06:26:21 +00:00
|
|
|
|
/// Gets the next scheduled run time for a specific job
|
|
|
|
|
/// (Estimated due to schedule timer)
|
2011-04-20 07:44:13 +00:00
|
|
|
|
/// </summary>
|
2011-11-07 06:26:21 +00:00
|
|
|
|
/// <returns>DateTime of next scheduled job execution</returns>
|
|
|
|
|
public virtual DateTime NextScheduledRun(Type jobType)
|
|
|
|
|
{
|
|
|
|
|
var job = All().Where(t => t.TypeName == jobType.ToString()).Single();
|
|
|
|
|
return job.LastExecution.AddMinutes(job.Interval);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-22 00:48:37 +00:00
|
|
|
|
public virtual void QueueJob(Type jobType, int targetId = 0, int secondaryTargetId = 0)
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var queueItem = new JobQueueItem
|
|
|
|
|
{
|
|
|
|
|
JobType = jobType,
|
|
|
|
|
TargetId = targetId,
|
|
|
|
|
SecondaryTargetId = secondaryTargetId
|
|
|
|
|
};
|
2011-05-17 07:04:49 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
logger.Debug("Attempting to queue {0}", queueItem);
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
_jobThreadStopWatch = Stopwatch.StartNew();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
_jobThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
job = Queue.First();
|
|
|
|
|
Queue.Remove(job);
|
|
|
|
|
logger.Debug("Popping {0} from the queue.", 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);
|
2011-07-11 00:03:01 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
logger.Trace("Finished processing jobs in the queue.");
|
2011-05-17 07:04:49 +00:00
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (ThreadAbortException e)
|
|
|
|
|
{
|
|
|
|
|
logger.Warn(e.Message);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.ErrorException("Error has occurred in queue processor thread", e);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
ResetThread();
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var jobImplementation = _jobs.Where(t => t.GetType() == queueItem.JobType).SingleOrDefault();
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-07 06:26:21 +00:00
|
|
|
|
var settings = All().Where(j => j.TypeName == queueItem.JobType.ToString()).Single();
|
2011-04-22 05:46:47 +00:00
|
|
|
|
|
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
|
|
|
|
{
|
2011-11-07 06:26:21 +00:00
|
|
|
|
logger.Debug("Starting {0}. Last execution {1}", queueItem, settings.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);
|
2011-11-07 06:26:21 +00:00
|
|
|
|
jobImplementation.Start(_notification, queueItem.TargetId, queueItem.SecondaryTargetId);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
_notification.Status = ProgressNotificationStatus.Completed;
|
2011-05-17 15:33:32 +00:00
|
|
|
|
|
2011-05-18 05:29:23 +00:00
|
|
|
|
settings.LastExecution = DateTime.Now;
|
|
|
|
|
settings.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.";
|
|
|
|
|
|
|
|
|
|
settings.LastExecution = DateTime.Now;
|
|
|
|
|
settings.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
|
2011-11-07 06:26:21 +00:00
|
|
|
|
if (queueItem.TargetId == 0)
|
2011-05-18 05:29:23 +00:00
|
|
|
|
{
|
2011-08-03 16:29:03 +00:00
|
|
|
|
SaveDefinition(settings);
|
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-07 06:26:21 +00:00
|
|
|
|
if (_jobThreadStopWatch.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" };
|
|
|
|
|
_jobThreadStopWatch = new Stopwatch();
|
2011-05-17 04:01:01 +00:00
|
|
|
|
}
|
2011-11-07 06:26:21 +00:00
|
|
|
|
|
|
|
|
|
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|