@ -1,5 +1,6 @@
using System ;
using System ;
using System.Collections.Generic ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Diagnostics ;
using System.Diagnostics ;
using System.Linq ;
using System.Linq ;
using System.Threading ;
using System.Threading ;
@ -11,6 +12,10 @@ using PetaPoco;
namespace NzbDrone.Core.Providers.Jobs
namespace NzbDrone.Core.Providers.Jobs
{
{
/// <summary>
/// Provides a background task runner, tasks could be queue either by the scheduler using QueueScheduled()
/// or by explicitly calling QueueJob(type,int)
/// </summary>
public class JobProvider
public class JobProvider
{
{
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
@ -21,9 +26,8 @@ namespace NzbDrone.Core.Providers.Jobs
private static readonly object ExecutionLock = new object ( ) ;
private static readonly object ExecutionLock = new object ( ) ;
private Thread _jobThread ;
private Thread _jobThread ;
private static bool _isRunning ;
private static bool _isRunning ;
public static readonly List < Tuple < Type , Int32 > > Queue = new List < Tuple < Type , int > > ( ) ;
private static readonly List < Tuple < Type , Int32 > > _queue = new List < Tuple < Type , int > > ( ) ;
private ProgressNotification _notification ;
private ProgressNotification _notification ;
@ -35,22 +39,38 @@ namespace NzbDrone.Core.Providers.Jobs
_jobs = jobs ;
_jobs = jobs ;
}
}
/// <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)]
public JobProvider ( ) { }
public JobProvider ( ) { }
/// <summary>
/// Gets the active queue.
/// </summary>
public static List < Tuple < Type , Int32 > > Queue
{
get
{
return _queue ;
}
}
/// <summary>
/// <summary>
/// Returns a list of all registered jobs
/// Returns a list of all registered jobs
/// </summary>
/// </summary>
/// <returns></returns>
public virtual List < JobDefinition > All ( )
public virtual List < JobDefinition > All ( )
{
{
return _database . Fetch < JobDefinition > ( ) . ToList ( ) ;
return _database . Fetch < JobDefinition > ( ) . ToList ( ) ;
}
}
/// <summary>
/// <summary>
/// Creates/Updates definitions for a job
/// Add s/Updates definitions for a job
/// </summary>
/// </summary>
/// <param name="definitions">Settings to be creat ed/updated</param>
/// <param name="definitions">Settings to be add ed/updated</param>
public virtual void Save Settings ( JobDefinition definitions )
public virtual void Save Definition ( JobDefinition definitions )
{
{
if ( definitions . Id = = 0 )
if ( definitions . Id = = 0 )
{
{
@ -65,9 +85,10 @@ namespace NzbDrone.Core.Providers.Jobs
}
}
/// <summary>
/// <summary>
/// Iterates through all registered jobs and executed any that are due for an execution.
/// Iterates through all registered jobs and queues any that are due for an execution.
/// </summary>
/// </summary>
public virtual void RunScheduled ( )
/// <remarks>Will ignore request if queue is already running.</remarks>
public virtual void QueueScheduled ( )
{
{
lock ( ExecutionLock )
lock ( ExecutionLock )
{
{
@ -95,9 +116,9 @@ namespace NzbDrone.Core.Providers.Jobs
}
}
/// <summary>
/// <summary>
/// Start s the execution of a job asynchronously
/// Queue s the execution of a job asynchronously
/// </summary>
/// </summary>
/// <param name="jobType">Type of the job that should be execut ed.</param>
/// <param name="jobType">Type of the job that should be queu ed.</param>
/// <param name="targetId">The targetId could be any Id parameter eg. SeriesId. it will be passed to the job implementation
/// <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>
/// to allow it to filter it's target of execution.</param>
/// <remarks>Job is only added to the queue if same job with the same targetId doesn't already exist in the queue.</remarks>
/// <remarks>Job is only added to the queue if same job with the same targetId doesn't already exist in the queue.</remarks>
@ -119,7 +140,7 @@ namespace NzbDrone.Core.Providers.Jobs
}
}
else
else
{
{
Logger . Info ( "[{0}:{1}] already exists in job queue. Skipping.", jobType . Name , targetId ) ;
Logger . Info ( "[{0}:{1}] already exists in the queue. Skipping.", jobType . Name , targetId ) ;
}
}
}
}
@ -163,7 +184,7 @@ namespace NzbDrone.Core.Providers.Jobs
}
}
/// <summary>
/// <summary>
/// Starts processing of queue .
/// Starts processing of queue synchronously .
/// </summary>
/// </summary>
private void ProcessQueue ( )
private void ProcessQueue ( )
{
{
@ -173,7 +194,7 @@ namespace NzbDrone.Core.Providers.Jobs
using ( NestedDiagnosticsContext . Push ( Guid . NewGuid ( ) . ToString ( ) ) )
using ( NestedDiagnosticsContext . Push ( Guid . NewGuid ( ) . ToString ( ) ) )
{
{
try
try
{
{
lock ( Queue )
lock ( Queue )
{
{
@ -210,7 +231,7 @@ namespace NzbDrone.Core.Providers.Jobs
}
}
/// <summary>
/// <summary>
/// Executes the job
/// Executes the job synchronously
/// </summary>
/// </summary>
/// <param name="jobType">Type of the job that should be executed</param>
/// <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
/// <param name="targetId">The targetId could be any Id parameter eg. SeriesId. it will be passed to the timer implementation
@ -256,15 +277,16 @@ namespace NzbDrone.Core.Providers.Jobs
}
}
}
}
//Only update last execution status if was triggered by the scheduler
if ( targetId = = 0 )
if ( targetId = = 0 )
{
{
Save Settings ( settings ) ;
Save Definition ( settings ) ;
}
}
}
}
/// <summary>
/// <summary>
/// Initializes jobs in the database using the IJob instances that are
/// Initializes jobs in the database using the IJob instances that are
/// registered in CentralDispatch
/// registered using ninject
/// </summary>
/// </summary>
public virtual void Initialize ( )
public virtual void Initialize ( )
{
{
@ -285,13 +307,13 @@ namespace NzbDrone.Core.Providers.Jobs
LastExecution = new DateTime ( 2000 , 1 , 1 )
LastExecution = new DateTime ( 2000 , 1 , 1 )
} ;
} ;
Save Settings ( settings ) ;
Save Definition ( settings ) ;
}
}
}
}
}
}
/// <summary>
/// <summary>
/// Gets the next scheduled run time for the job
/// Gets the next scheduled run time for a specific job
/// (Estimated due to schedule timer)
/// (Estimated due to schedule timer)
/// </summary>
/// </summary>
/// <returns>DateTime of next scheduled job execution</returns>
/// <returns>DateTime of next scheduled job execution</returns>