2011-04-20 07:44:13 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
public class JobProvider
|
|
|
|
|
{
|
|
|
|
|
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 static readonly object ExecutionLock = new object();
|
|
|
|
|
private Thread _jobThread;
|
|
|
|
|
private static bool _isRunning;
|
2011-05-17 07:04:49 +00:00
|
|
|
|
private static readonly List<Tuple<Type, Int32>> Queue = new List<Tuple<Type, int>>();
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JobProvider() { }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a list of all registered jobs
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public virtual List<JobSetting> All()
|
|
|
|
|
{
|
2011-06-17 19:50:49 +00:00
|
|
|
|
return _database.Fetch<JobSetting>().ToList();
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates/Updates settings for a job
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="settings">Settings to be created/updated</param>
|
|
|
|
|
public virtual void SaveSettings(JobSetting settings)
|
|
|
|
|
{
|
|
|
|
|
if (settings.Id == 0)
|
|
|
|
|
{
|
2011-06-22 06:34:33 +00:00
|
|
|
|
Logger.Trace("Adding job settings for {0}", settings.Name);
|
2011-06-17 19:50:49 +00:00
|
|
|
|
_database.Insert(settings);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-06-22 06:34:33 +00:00
|
|
|
|
Logger.Trace("Updating job settings for {0}", settings.Name);
|
2011-06-17 19:50:49 +00:00
|
|
|
|
_database.Update(settings);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Iterates through all registered jobs and executed any that are due for an execution.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if ran, false if skipped</returns>
|
|
|
|
|
public virtual bool RunScheduled()
|
|
|
|
|
{
|
|
|
|
|
lock (ExecutionLock)
|
|
|
|
|
{
|
|
|
|
|
if (_isRunning)
|
|
|
|
|
{
|
2011-07-06 06:17:21 +00:00
|
|
|
|
Logger.Trace("Queue is already running. Ignoring scheduler's request.");
|
2011-04-20 07:44:13 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_isRunning = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var pendingJobs = All().Where(
|
|
|
|
|
t => t.Enable &&
|
|
|
|
|
(DateTime.Now - t.LastExecution) > TimeSpan.FromMinutes(t.Interval)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach (var pendingTimer in pendingJobs)
|
|
|
|
|
{
|
2011-05-17 07:24:29 +00:00
|
|
|
|
var timer = pendingTimer;
|
|
|
|
|
var timerClass = _jobs.Where(t => t.GetType().ToString() == timer.TypeName).FirstOrDefault();
|
2011-05-17 07:04:49 +00:00
|
|
|
|
Execute(timerClass.GetType());
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_isRunning = false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-22 06:34:33 +00:00
|
|
|
|
Logger.Trace("Finished executing scheduled tasks.");
|
2011-04-20 07:44:13 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the execution of a job asynchronously
|
|
|
|
|
/// </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 job implementation
|
|
|
|
|
/// to allow it to filter it's target of execution.</param>
|
2011-05-20 06:25:39 +00:00
|
|
|
|
/// <returns>True if queued, false if duplicate and was skipped</returns>
|
2011-05-17 07:04:49 +00:00
|
|
|
|
/// <remarks>Job is only added to the queue if same job with the same targetId doesn't already exist in the queue.</remarks>
|
|
|
|
|
public virtual bool QueueJob(Type jobType, int targetId = 0)
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
2011-07-06 06:17:21 +00:00
|
|
|
|
Logger.Debug("Adding [{0}:{1}] to the queue", jobType.Name, targetId);
|
2011-05-17 07:04:49 +00:00
|
|
|
|
lock (Queue)
|
|
|
|
|
{
|
|
|
|
|
var queueTuple = new Tuple<Type, int>(jobType, targetId);
|
|
|
|
|
|
|
|
|
|
if (Queue.Contains(queueTuple))
|
|
|
|
|
{
|
2011-07-06 06:17:21 +00:00
|
|
|
|
Logger.Info("[{0}:{1}] already exists in job queue. Skipping.", jobType.Name, targetId);
|
2011-05-17 07:04:49 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Queue.Add(queueTuple);
|
2011-07-06 06:17:21 +00:00
|
|
|
|
Logger.Trace("Job [{0}:{1}] added to the queue", jobType.Name, targetId);
|
2011-05-17 07:04:49 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-20 07:44:13 +00:00
|
|
|
|
lock (ExecutionLock)
|
|
|
|
|
{
|
|
|
|
|
if (_isRunning)
|
|
|
|
|
{
|
2011-07-06 06:17:21 +00:00
|
|
|
|
Logger.Trace("Queue is already running. No need to start it up.");
|
2011-05-17 07:04:49 +00:00
|
|
|
|
return true;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
2011-05-20 06:25:39 +00:00
|
|
|
|
_isRunning = true;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
2011-05-17 07:04:49 +00:00
|
|
|
|
|
2011-04-20 07:44:13 +00:00
|
|
|
|
if (_jobThread == null || !_jobThread.IsAlive)
|
|
|
|
|
{
|
2011-05-17 07:04:49 +00:00
|
|
|
|
Logger.Trace("Initializing queue processor thread");
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-04-21 01:26:13 +00:00
|
|
|
|
ThreadStart starter = () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2011-05-17 07:04:49 +00:00
|
|
|
|
ProcessQueue();
|
2011-04-21 01:26:13 +00:00
|
|
|
|
}
|
2011-05-20 07:39:05 +00:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2011-07-06 06:17:21 +00:00
|
|
|
|
Logger.ErrorException("Error has occurred in queue processor thread", e);
|
2011-05-20 07:39:05 +00:00
|
|
|
|
}
|
2011-04-21 01:26:13 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_isRunning = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2011-07-06 06:17:21 +00:00
|
|
|
|
_jobThread = new Thread(starter) { Name = "JobQueueThread" };
|
2011-04-20 07:44:13 +00:00
|
|
|
|
_jobThread.Start();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-07-06 06:17:21 +00:00
|
|
|
|
Logger.Error("Execution lock has fucked up. Thread still active. Ignoring request.");
|
2011-05-20 06:25:39 +00:00
|
|
|
|
return true;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-17 07:04:49 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts processing of queue.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void ProcessQueue()
|
|
|
|
|
{
|
|
|
|
|
Tuple<Type, int> job = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
lock (Queue)
|
|
|
|
|
{
|
|
|
|
|
if (Queue.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
job = Queue[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (job != null)
|
|
|
|
|
{
|
|
|
|
|
Execute(job.Item1, job.Item2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2011-05-19 03:59:12 +00:00
|
|
|
|
Logger.FatalException("An error has occurred while processing queued job.", e);
|
2011-05-17 07:04:49 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (job != null)
|
|
|
|
|
{
|
|
|
|
|
Queue.Remove(job);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Try to find next job is last run found a job.
|
|
|
|
|
if (job != null)
|
|
|
|
|
{
|
|
|
|
|
ProcessQueue();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-06-22 06:34:33 +00:00
|
|
|
|
Logger.Trace("Finished processing jobs in the queue.");
|
2011-05-17 07:04:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-20 07:44:13 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes the job
|
|
|
|
|
/// </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>
|
|
|
|
|
private void Execute(Type jobType, int targetId = 0)
|
|
|
|
|
{
|
2011-05-17 07:24:29 +00:00
|
|
|
|
var jobImplementation = _jobs.Where(t => t.GetType() == jobType).FirstOrDefault();
|
|
|
|
|
if (jobImplementation == null)
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
2011-07-06 06:17:21 +00:00
|
|
|
|
Logger.Error("Unable to locate implementation for '{0}'. Make sure it is properly registered.", jobType);
|
2011-04-20 07:44:13 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-22 05:46:47 +00:00
|
|
|
|
var settings = All().Where(j => j.TypeName == jobType.ToString()).FirstOrDefault();
|
|
|
|
|
|
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-07-06 06:17:21 +00:00
|
|
|
|
Logger.Debug("Starting '{0}' job. Last execution {1}", settings.Name, 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-05-17 07:24:29 +00:00
|
|
|
|
jobImplementation.Start(_notification, targetId);
|
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-07-06 06:17:21 +00:00
|
|
|
|
Logger.Debug("Job '{0}' successfully completed in {1}.{2} seconds.", jobImplementation.Name, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds / 100,
|
2011-04-22 06:23:29 +00:00
|
|
|
|
sw.Elapsed.Seconds);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2011-07-06 06:17: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-05-18 05:29:23 +00:00
|
|
|
|
if (targetId == 0)
|
|
|
|
|
{
|
|
|
|
|
SaveSettings(settings);
|
|
|
|
|
}
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes jobs in the database using the IJob instances that are
|
|
|
|
|
/// registered in CentralDispatch
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual void Initialize()
|
|
|
|
|
{
|
2011-04-25 03:51:18 +00:00
|
|
|
|
Logger.Debug("Initializing jobs. Count {0}", _jobs.Count());
|
2011-04-20 07:44:13 +00:00
|
|
|
|
var currentTimer = All();
|
|
|
|
|
|
|
|
|
|
foreach (var timer in _jobs)
|
|
|
|
|
{
|
|
|
|
|
var timerProviderLocal = timer;
|
|
|
|
|
if (!currentTimer.Exists(c => c.TypeName == timerProviderLocal.GetType().ToString()))
|
|
|
|
|
{
|
2011-04-21 01:26:13 +00:00
|
|
|
|
var settings = new JobSetting
|
2011-04-20 07:44:13 +00:00
|
|
|
|
{
|
2011-05-11 18:25:32 +00:00
|
|
|
|
Enable = timerProviderLocal.DefaultInterval > 0,
|
2011-04-20 07:44:13 +00:00
|
|
|
|
TypeName = timer.GetType().ToString(),
|
|
|
|
|
Name = timerProviderLocal.Name,
|
|
|
|
|
Interval = timerProviderLocal.DefaultInterval,
|
2011-06-23 06:56:17 +00:00
|
|
|
|
LastExecution = new DateTime(2000, 1, 1)
|
2011-04-20 07:44:13 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SaveSettings(settings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-17 04:01:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the next scheduled run time for the job
|
|
|
|
|
/// (Estimated due to schedule timer)
|
|
|
|
|
/// </summary>
|
2011-05-17 07:04:49 +00:00
|
|
|
|
/// <returns>DateTime of next scheduled job execution</returns>
|
2011-05-17 04:01:01 +00:00
|
|
|
|
public virtual DateTime NextScheduledRun(Type jobType)
|
|
|
|
|
{
|
|
|
|
|
var job = All().Where(t => t.TypeName == jobType.ToString()).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (job == null)
|
|
|
|
|
return DateTime.Now;
|
2011-04-20 07:44:13 +00:00
|
|
|
|
|
2011-05-17 04:01:01 +00:00
|
|
|
|
return job.LastExecution.AddMinutes(job.Interval);
|
|
|
|
|
}
|
2011-04-20 07:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|