2011-08-05 07:49:18 +03:00
|
|
|
//https://github.com/kayone/NzbDrone/blob/master/NzbDrone.Core/Providers/Jobs/JobProvider.cs
|
|
|
|
using System;
|
2011-04-20 10:44:13 +03:00
|
|
|
using System.Collections.Generic;
|
2011-08-03 19:29:03 +03:00
|
|
|
using System.ComponentModel;
|
2011-04-20 10:44:13 +03:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
2011-06-14 04:23:04 +03:00
|
|
|
using Ninject;
|
2011-04-20 10:44:13 +03:00
|
|
|
using NLog;
|
2011-08-22 03:48:37 +03:00
|
|
|
using NzbDrone.Core.Model;
|
2011-04-20 10:44:13 +03:00
|
|
|
using NzbDrone.Core.Model.Notification;
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-17 22:50:49 +03:00
|
|
|
using PetaPoco;
|
2011-09-03 08:30:18 +03:00
|
|
|
using ThreadState = System.Threading.ThreadState;
|
2011-04-20 10:44:13 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers.Jobs
|
|
|
|
{
|
2011-08-03 19:29:03 +03: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 10:44:13 +03:00
|
|
|
public class JobProvider
|
|
|
|
{
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-06-17 22:50:49 +03:00
|
|
|
private readonly IDatabase _database;
|
2011-04-20 10:44:13 +03:00
|
|
|
private readonly NotificationProvider _notificationProvider;
|
2011-05-24 03:34:57 +03:00
|
|
|
private readonly IList<IJob> _jobs;
|
2011-04-20 10:44:13 +03:00
|
|
|
|
|
|
|
private Thread _jobThread;
|
2011-07-11 03:03:01 +03:00
|
|
|
|
2011-10-24 00:53:24 +03:00
|
|
|
private readonly object ExecutionLock = new object();
|
|
|
|
private bool _isRunning;
|
|
|
|
private readonly List<JobQueueItem> _queue = new List<JobQueueItem>();
|
2011-04-20 10:44:13 +03:00
|
|
|
|
|
|
|
private ProgressNotification _notification;
|
|
|
|
|
2011-06-14 04:23:04 +03:00
|
|
|
[Inject]
|
2011-06-17 22:50:49 +03:00
|
|
|
public JobProvider(IDatabase database, NotificationProvider notificationProvider, IList<IJob> jobs)
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-06-17 22:50:49 +03:00
|
|
|
_database = database;
|
2011-04-20 10:44:13 +03:00
|
|
|
_notificationProvider = notificationProvider;
|
|
|
|
_jobs = jobs;
|
|
|
|
}
|
|
|
|
|
2011-08-03 19:29:03 +03: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 10:44:13 +03:00
|
|
|
public JobProvider() { }
|
|
|
|
|
2011-08-03 19:29:03 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the active queue.
|
|
|
|
/// </summary>
|
2011-10-24 00:53:24 +03:00
|
|
|
public List<JobQueueItem> Queue
|
2011-08-03 19:29:03 +03:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _queue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 10:44:13 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Returns a list of all registered jobs
|
|
|
|
/// </summary>
|
2011-07-08 06:27:11 +03:00
|
|
|
public virtual List<JobDefinition> All()
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-07-08 06:27:11 +03:00
|
|
|
return _database.Fetch<JobDefinition>().ToList();
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2011-08-03 19:29:03 +03:00
|
|
|
/// Adds/Updates definitions for a job
|
2011-04-20 10:44:13 +03:00
|
|
|
/// </summary>
|
2011-08-03 19:29:03 +03:00
|
|
|
/// <param name="definitions">Settings to be added/updated</param>
|
|
|
|
public virtual void SaveDefinition(JobDefinition definitions)
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-07-08 06:27:11 +03:00
|
|
|
if (definitions.Id == 0)
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-07-08 06:27:11 +03:00
|
|
|
Logger.Trace("Adding job definitions for {0}", definitions.Name);
|
|
|
|
_database.Insert(definitions);
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-08 06:27:11 +03:00
|
|
|
Logger.Trace("Updating job definitions for {0}", definitions.Name);
|
|
|
|
_database.Update(definitions);
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2011-08-03 19:29:03 +03:00
|
|
|
/// Iterates through all registered jobs and queues any that are due for an execution.
|
2011-04-20 10:44:13 +03:00
|
|
|
/// </summary>
|
2011-08-03 19:29:03 +03:00
|
|
|
/// <remarks>Will ignore request if queue is already running.</remarks>
|
|
|
|
public virtual void QueueScheduled()
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
|
|
|
lock (ExecutionLock)
|
|
|
|
{
|
|
|
|
if (_isRunning)
|
|
|
|
{
|
2011-07-06 09:17:21 +03:00
|
|
|
Logger.Trace("Queue is already running. Ignoring scheduler's request.");
|
2011-07-11 07:53:34 +03:00
|
|
|
return;
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-11 07:53:34 +03:00
|
|
|
var counter = 0;
|
2011-04-20 10:44:13 +03:00
|
|
|
|
2011-07-11 07:53:34 +03:00
|
|
|
var pendingJobs = All().Where(
|
|
|
|
t => t.Enable &&
|
|
|
|
(DateTime.Now - t.LastExecution) > TimeSpan.FromMinutes(t.Interval)
|
|
|
|
).Select(c => _jobs.Where(t => t.GetType().ToString() == c.TypeName).Single());
|
|
|
|
|
|
|
|
foreach (var job in pendingJobs)
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-07-11 07:53:34 +03:00
|
|
|
QueueJob(job.GetType());
|
|
|
|
counter++;
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
|
|
|
|
2011-07-11 07:53:34 +03:00
|
|
|
Logger.Trace("{0} Scheduled tasks have been added to the queue", counter);
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2011-08-03 19:29:03 +03:00
|
|
|
/// Queues the execution of a job asynchronously
|
2011-04-20 10:44:13 +03:00
|
|
|
/// </summary>
|
2011-08-03 19:29:03 +03:00
|
|
|
/// <param name="jobType">Type of the job that should be queued.</param>
|
2011-04-20 10:44:13 +03:00
|
|
|
/// <param name="targetId">The targetId could be any Id parameter eg. SeriesId. it will be passed to the job implementation
|
|
|
|
/// to allow it to filter it's target of execution.</param>
|
2011-08-22 03:48:37 +03:00
|
|
|
/// /// <param name="secondaryTargetId">The secondaryTargetId could be any Id parameter eg. SeasonNumber. it will be passed to
|
|
|
|
/// the timer implementation to further allow it to filter it's target of execution</param>
|
2011-05-17 10:04:49 +03:00
|
|
|
/// <remarks>Job is only added to the queue if same job with the same targetId doesn't already exist in the queue.</remarks>
|
2011-08-22 03:48:37 +03:00
|
|
|
public virtual void QueueJob(Type jobType, int targetId = 0, int secondaryTargetId = 0)
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-07-06 09:17:21 +03:00
|
|
|
Logger.Debug("Adding [{0}:{1}] to the queue", jobType.Name, targetId);
|
2011-05-17 10:04:49 +03:00
|
|
|
|
2011-07-11 07:53:34 +03:00
|
|
|
lock (ExecutionLock)
|
|
|
|
{
|
|
|
|
lock (Queue)
|
2011-05-17 10:04:49 +03:00
|
|
|
{
|
2011-08-22 03:48:37 +03:00
|
|
|
var queueItem = new JobQueueItem
|
|
|
|
{
|
|
|
|
JobType = jobType,
|
|
|
|
TargetId = targetId,
|
|
|
|
SecondaryTargetId = secondaryTargetId
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!Queue.Contains(queueItem))
|
2011-07-11 07:53:34 +03:00
|
|
|
{
|
2011-08-22 03:48:37 +03:00
|
|
|
Queue.Add(queueItem);
|
2011-07-11 07:53:34 +03:00
|
|
|
Logger.Trace("Job [{0}:{1}] added to the queue", jobType.Name, targetId);
|
2011-05-17 10:04:49 +03:00
|
|
|
|
2011-07-11 07:53:34 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-03 19:29:03 +03:00
|
|
|
Logger.Info("[{0}:{1}] already exists in the queue. Skipping.", jobType.Name, targetId);
|
2011-07-11 07:53:34 +03:00
|
|
|
}
|
|
|
|
}
|
2011-05-17 10:04:49 +03:00
|
|
|
|
2011-04-20 10:44:13 +03:00
|
|
|
if (_isRunning)
|
|
|
|
{
|
2011-07-06 09:17:21 +03:00
|
|
|
Logger.Trace("Queue is already running. No need to start it up.");
|
2011-07-11 07:53:34 +03:00
|
|
|
return;
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
2011-05-20 09:25:39 +03:00
|
|
|
_isRunning = true;
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
2011-05-17 10:04:49 +03:00
|
|
|
|
2011-09-03 08:30:18 +03:00
|
|
|
if (_jobThread == null || _jobThread.ThreadState != ThreadState.Running)
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-05-17 10:04:49 +03:00
|
|
|
Logger.Trace("Initializing queue processor thread");
|
2011-04-20 10:44:13 +03:00
|
|
|
|
2011-04-21 04:26:13 +03:00
|
|
|
ThreadStart starter = () =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-05-17 10:04:49 +03:00
|
|
|
ProcessQueue();
|
2011-04-21 04:26:13 +03:00
|
|
|
}
|
2011-05-20 10:39:05 +03:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
2011-07-06 09:17:21 +03:00
|
|
|
Logger.ErrorException("Error has occurred in queue processor thread", e);
|
2011-05-20 10:39:05 +03:00
|
|
|
}
|
2011-04-21 04:26:13 +03:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
_isRunning = false;
|
2011-09-03 08:30:18 +03:00
|
|
|
_jobThread.Abort();
|
|
|
|
|
2011-04-21 04:26:13 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-11 03:03:01 +03:00
|
|
|
_jobThread = new Thread(starter) { Name = "JobQueueThread" };
|
2011-04-20 10:44:13 +03:00
|
|
|
_jobThread.Start();
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-25 02:54:48 +03:00
|
|
|
var messge = "Job Thread is null";
|
|
|
|
|
|
|
|
if (_jobThread != null)
|
|
|
|
{
|
|
|
|
messge = "Job Thread State: " + _jobThread.ThreadState;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Error("Execution lock has fucked up. {0}. Ignoring request.", messge);
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-05-17 10:04:49 +03:00
|
|
|
/// <summary>
|
2011-08-03 19:29:03 +03:00
|
|
|
/// Starts processing of queue synchronously.
|
2011-05-17 10:04:49 +03:00
|
|
|
/// </summary>
|
|
|
|
private void ProcessQueue()
|
|
|
|
{
|
2011-07-11 03:03:01 +03:00
|
|
|
do
|
2011-05-17 10:04:49 +03:00
|
|
|
{
|
2011-07-28 01:59:48 +03:00
|
|
|
using (NestedDiagnosticsContext.Push(Guid.NewGuid().ToString()))
|
2011-05-17 10:04:49 +03:00
|
|
|
{
|
2011-08-03 19:29:03 +03:00
|
|
|
try
|
2011-05-17 10:04:49 +03:00
|
|
|
{
|
2011-08-22 03:48:37 +03:00
|
|
|
JobQueueItem job = null;
|
2011-08-07 08:49:07 +03:00
|
|
|
|
2011-07-28 01:59:48 +03:00
|
|
|
lock (Queue)
|
2011-07-11 03:03:01 +03:00
|
|
|
{
|
2011-07-28 01:59:48 +03:00
|
|
|
if (Queue.Count != 0)
|
|
|
|
{
|
|
|
|
job = Queue.First();
|
2011-08-05 07:49:18 +03:00
|
|
|
Queue.Remove(job);
|
2011-07-28 01:59:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (job != null)
|
|
|
|
{
|
2011-08-22 03:48:37 +03:00
|
|
|
Execute(job.JobType, job.TargetId, job.SecondaryTargetId);
|
2011-07-11 03:03:01 +03:00
|
|
|
}
|
|
|
|
|
2011-07-28 01:59:48 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
2011-07-11 03:03:01 +03:00
|
|
|
{
|
2011-07-28 01:59:48 +03:00
|
|
|
Logger.FatalException("An error has occurred while processing queued job.", e);
|
2011-05-17 10:04:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-11 03:03:01 +03:00
|
|
|
} while (Queue.Count != 0);
|
|
|
|
|
|
|
|
Logger.Trace("Finished processing jobs in the queue.");
|
2011-05-17 10:04:49 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-20 10:44:13 +03:00
|
|
|
/// <summary>
|
2011-08-03 19:29:03 +03:00
|
|
|
/// Executes the job synchronously
|
2011-04-20 10:44:13 +03:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="jobType">Type of the job that should be executed</param>
|
|
|
|
/// <param name="targetId">The targetId could be any Id parameter eg. SeriesId. it will be passed to the timer implementation
|
|
|
|
/// to allow it to filter it's target of execution</param>
|
2011-08-22 03:48:37 +03:00
|
|
|
/// /// <param name="secondaryTargetId">The secondaryTargetId could be any Id parameter eg. SeasonNumber. it will be passed to
|
|
|
|
/// the timer implementation to further allow it to filter it's target of execution</param>
|
|
|
|
private void Execute(Type jobType, int targetId = 0, int secondaryTargetId = 0)
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-07-28 01:59:48 +03:00
|
|
|
var jobImplementation = _jobs.Where(t => t.GetType() == jobType).Single();
|
2011-05-17 10:24:29 +03:00
|
|
|
if (jobImplementation == null)
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-07-06 09:17:21 +03:00
|
|
|
Logger.Error("Unable to locate implementation for '{0}'. Make sure it is properly registered.", jobType);
|
2011-04-20 10:44:13 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-28 01:59:48 +03:00
|
|
|
var settings = All().Where(j => j.TypeName == jobType.ToString()).Single();
|
2011-04-22 08:46:47 +03:00
|
|
|
|
2011-05-17 10:24:29 +03:00
|
|
|
using (_notification = new ProgressNotification(jobImplementation.Name))
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-04-22 09:23:29 +03:00
|
|
|
try
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-07-06 09:17:21 +03:00
|
|
|
Logger.Debug("Starting '{0}' job. Last execution {1}", settings.Name, settings.LastExecution);
|
2011-05-18 08:29:23 +03:00
|
|
|
|
2011-04-22 09:23:29 +03:00
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
2011-04-20 10:44:13 +03:00
|
|
|
_notificationProvider.Register(_notification);
|
2011-08-22 03:48:37 +03:00
|
|
|
jobImplementation.Start(_notification, targetId, secondaryTargetId);
|
2011-04-20 10:44:13 +03:00
|
|
|
_notification.Status = ProgressNotificationStatus.Completed;
|
2011-05-17 18:33:32 +03:00
|
|
|
|
2011-05-18 08:29:23 +03:00
|
|
|
settings.LastExecution = DateTime.Now;
|
|
|
|
settings.Success = true;
|
2011-05-17 18:33:32 +03:00
|
|
|
|
2011-04-22 09:23:29 +03:00
|
|
|
sw.Stop();
|
2011-10-21 08:04:26 +03:00
|
|
|
Logger.Debug("Job '{0}' successfully completed in {1:0}.{2} seconds.", jobImplementation.Name, sw.Elapsed.TotalSeconds, sw.Elapsed.Milliseconds / 100,
|
2011-04-22 09:23:29 +03:00
|
|
|
sw.Elapsed.Seconds);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2011-07-06 09:17:21 +03:00
|
|
|
Logger.ErrorException("An error has occurred while executing job " + jobImplementation.Name, e);
|
2011-04-22 09:23:29 +03:00
|
|
|
_notification.Status = ProgressNotificationStatus.Failed;
|
2011-05-20 10:39:05 +03:00
|
|
|
_notification.CurrentMessage = jobImplementation.Name + " Failed.";
|
|
|
|
|
|
|
|
settings.LastExecution = DateTime.Now;
|
|
|
|
settings.Success = false;
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
|
|
|
}
|
2011-04-22 08:46:47 +03:00
|
|
|
|
2011-08-03 19:29:03 +03:00
|
|
|
//Only update last execution status if was triggered by the scheduler
|
2011-05-18 08:29:23 +03:00
|
|
|
if (targetId == 0)
|
|
|
|
{
|
2011-08-03 19:29:03 +03:00
|
|
|
SaveDefinition(settings);
|
2011-05-18 08:29:23 +03:00
|
|
|
}
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes jobs in the database using the IJob instances that are
|
2011-08-03 19:29:03 +03:00
|
|
|
/// registered using ninject
|
2011-04-20 10:44:13 +03:00
|
|
|
/// </summary>
|
|
|
|
public virtual void Initialize()
|
|
|
|
{
|
2011-04-25 06:51:18 +03:00
|
|
|
Logger.Debug("Initializing jobs. Count {0}", _jobs.Count());
|
2011-04-20 10:44:13 +03:00
|
|
|
var currentTimer = All();
|
|
|
|
|
|
|
|
foreach (var timer in _jobs)
|
|
|
|
{
|
|
|
|
var timerProviderLocal = timer;
|
|
|
|
if (!currentTimer.Exists(c => c.TypeName == timerProviderLocal.GetType().ToString()))
|
|
|
|
{
|
2011-07-08 06:27:11 +03:00
|
|
|
var settings = new JobDefinition
|
2011-04-20 10:44:13 +03:00
|
|
|
{
|
2011-05-11 21:25:32 +03:00
|
|
|
Enable = timerProviderLocal.DefaultInterval > 0,
|
2011-04-20 10:44:13 +03:00
|
|
|
TypeName = timer.GetType().ToString(),
|
|
|
|
Name = timerProviderLocal.Name,
|
|
|
|
Interval = timerProviderLocal.DefaultInterval,
|
2011-06-23 09:56:17 +03:00
|
|
|
LastExecution = new DateTime(2000, 1, 1)
|
2011-04-20 10:44:13 +03:00
|
|
|
};
|
|
|
|
|
2011-08-03 19:29:03 +03:00
|
|
|
SaveDefinition(settings);
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-17 07:01:01 +03:00
|
|
|
/// <summary>
|
2011-08-03 19:29:03 +03:00
|
|
|
/// Gets the next scheduled run time for a specific job
|
2011-05-17 07:01:01 +03:00
|
|
|
/// (Estimated due to schedule timer)
|
|
|
|
/// </summary>
|
2011-05-17 10:04:49 +03:00
|
|
|
/// <returns>DateTime of next scheduled job execution</returns>
|
2011-05-17 07:01:01 +03:00
|
|
|
public virtual DateTime NextScheduledRun(Type jobType)
|
|
|
|
{
|
2011-07-28 01:59:48 +03:00
|
|
|
var job = All().Where(t => t.TypeName == jobType.ToString()).Single();
|
2011-05-17 07:01:01 +03:00
|
|
|
return job.LastExecution.AddMinutes(job.Interval);
|
|
|
|
}
|
2011-04-20 10:44:13 +03:00
|
|
|
}
|
|
|
|
}
|