@ -10,7 +10,6 @@ using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions ;
using MediaBrowser.Common.Progress ;
using MediaBrowser.Model.Events ;
using MediaBrowser.Model.IO ;
using MediaBrowser.Model.Serialization ;
using MediaBrowser.Model.Tasks ;
using Microsoft.Extensions.Logging ;
@ -22,37 +21,53 @@ namespace Emby.Server.Implementations.ScheduledTasks
/// </summary>
public class ScheduledTaskWorker : IScheduledTaskWorker
{
public event EventHandler < GenericEventArgs < double > > TaskProgress ;
/// <summary>
/// Gets the scheduled task.
/// </summary>
/// <value>The scheduled task.</value>
public IScheduledTask ScheduledTask { get ; private set ; }
/// <summary>
/// Gets or sets the json serializer.
/// </summary>
/// <value>The json serializer.</value>
private IJsonSerializer JsonSerializer { get ; set ; }
private readonly IJsonSerializer _jsonSerializer ;
/// <summary>
/// Gets or sets the application paths.
/// </summary>
/// <value>The application paths.</value>
private IApplicationPaths ApplicationPaths { get ; set ; }
private readonly IApplicationPaths _applicationPaths ;
/// <summary>
/// Gets the logger.
/// Gets or sets the logger.
/// </summary>
/// <value>The logger.</value>
private ILogger Logger { get ; set ; }
private readonly ILogger _logger ;
/// <summary>
/// Gets the task manager.
/// Gets or sets the task manager.
/// </summary>
/// <value>The task manager.</value>
private ITaskManager TaskManager { get ; set ; }
private readonly ITaskManager _taskManager ;
/// <summary>
/// The _last execution result sync lock.
/// </summary>
private readonly object _lastExecutionResultSyncLock = new object ( ) ;
private bool _readFromFile = false ;
/// <summary>
/// The _last execution result.
/// </summary>
private TaskResult _lastExecutionResult ;
private Task _currentTask ;
/// <summary>
/// The _triggers.
/// </summary>
private Tuple < TaskTriggerInfo , ITaskTrigger > [ ] _triggers ;
/// <summary>
/// The _id.
/// </summary>
private string _id ;
/// <summary>
/// Initializes a new instance of the <see cref="ScheduledTaskWorker" /> class.
@ -71,7 +86,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
/// or
/// jsonSerializer
/// or
/// logger
/// logger .
/// </exception>
public ScheduledTaskWorker ( IScheduledTask scheduledTask , IApplicationPaths applicationPaths , ITaskManager taskManager , IJsonSerializer jsonSerializer , ILogger logger )
{
@ -101,23 +116,22 @@ namespace Emby.Server.Implementations.ScheduledTasks
}
ScheduledTask = scheduledTask ;
A pplicationPaths = applicationPaths ;
T askManager = taskManager ;
J sonSerializer = jsonSerializer ;
L ogger = logger ;
_a pplicationPaths = applicationPaths ;
_t askManager = taskManager ;
_j sonSerializer = jsonSerializer ;
_l ogger = logger ;
InitTriggerEvents ( ) ;
}
private bool _readFromFile = false ;
/// <summary>
/// The _last execution result.
/// </summary>
private TaskResult _lastExecutionResult ;
public event EventHandler < GenericEventArgs < double > > TaskProgress ;
/// <summary>
/// The _last execution result sync loc k.
/// Gets the scheduled task.
/// </summary>
private readonly object _lastExecutionResultSyncLock = new object ( ) ;
/// <value>The scheduled task.</value>
public IScheduledTask ScheduledTask { get ; private set ; }
/// <summary>
/// Gets the last execution result.
/// </summary>
@ -136,11 +150,11 @@ namespace Emby.Server.Implementations.ScheduledTasks
{
try
{
_lastExecutionResult = J sonSerializer. DeserializeFromFile < TaskResult > ( path ) ;
_lastExecutionResult = _j sonSerializer. DeserializeFromFile < TaskResult > ( path ) ;
}
catch ( Exception ex )
{
L ogger. LogError ( ex , "Error deserializing {File}" , path ) ;
_l ogger. LogError ( ex , "Error deserializing {File}" , path ) ;
}
}
@ -160,7 +174,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
lock ( _lastExecutionResultSyncLock )
{
J sonSerializer. SerializeToFile ( value , path ) ;
_j sonSerializer. SerializeToFile ( value , path ) ;
}
}
}
@ -184,7 +198,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
public string Category = > ScheduledTask . Category ;
/// <summary>
/// Gets the current cancellation token.
/// Gets or sets the current cancellation token.
/// </summary>
/// <value>The current cancellation token source.</value>
private CancellationTokenSource CurrentCancellationTokenSource { get ; set ; }
@ -221,12 +235,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
public double? CurrentProgress { get ; private set ; }
/// <summary>
/// The _triggers.
/// </summary>
private Tuple < TaskTriggerInfo , ITaskTrigger > [ ] _triggers ;
/// <summary>
/// Gets the triggers that define when the task will run.
/// Gets or sets the triggers that define when the task will run.
/// </summary>
/// <value>The triggers.</value>
private Tuple < TaskTriggerInfo , ITaskTrigger > [ ] InternalTriggers
@ -255,7 +264,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
/// Gets the triggers that define when the task will run.
/// </summary>
/// <value>The triggers.</value>
/// <exception cref="ArgumentNullException"> value</exception>
/// <exception cref="ArgumentNullException"> <c> value</c> is <c>null</c>. </exception>
public TaskTriggerInfo [ ] Triggers
{
get
@ -280,11 +289,6 @@ namespace Emby.Server.Implementations.ScheduledTasks
}
}
/// <summary>
/// The _id.
/// </summary>
private string _id ;
/// <summary>
/// Gets the unique id.
/// </summary>
@ -325,9 +329,9 @@ namespace Emby.Server.Implementations.ScheduledTasks
trigger . Stop ( ) ;
trigger . Triggered - = trigger_ Triggered;
trigger . Triggered + = trigger_ Triggered;
trigger . Start ( LastExecutionResult , L ogger, Name , isApplicationStartup ) ;
trigger . Triggered - = OnTrigger Triggered;
trigger . Triggered + = OnTrigger Triggered;
trigger . Start ( LastExecutionResult , _l ogger, Name , isApplicationStartup ) ;
}
}
@ -336,7 +340,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
async void trigger_ Triggered( object sender , EventArgs e )
private async void OnTrigger Triggered( object sender , EventArgs e )
{
var trigger = ( ITaskTrigger ) sender ;
@ -347,19 +351,17 @@ namespace Emby.Server.Implementations.ScheduledTasks
return ;
}
L ogger. LogInformation ( "{0} fired for task: {1}" , trigger . GetType ( ) . Name , Name ) ;
_l ogger. LogInformation ( "{0} fired for task: {1}" , trigger . GetType ( ) . Name , Name ) ;
trigger . Stop ( ) ;
T askManager. QueueScheduledTask ( ScheduledTask , trigger . TaskOptions ) ;
_t askManager. QueueScheduledTask ( ScheduledTask , trigger . TaskOptions ) ;
await Task . Delay ( 1000 ) . ConfigureAwait ( false ) ;
trigger . Start ( LastExecutionResult , L ogger, Name , false ) ;
trigger . Start ( LastExecutionResult , _l ogger, Name , false ) ;
}
private Task _currentTask ;
/// <summary>
/// Executes the task.
/// </summary>
@ -395,9 +397,9 @@ namespace Emby.Server.Implementations.ScheduledTasks
CurrentCancellationTokenSource = new CancellationTokenSource ( ) ;
L ogger. LogInformation ( "Executing {0}" , Name ) ;
_l ogger. LogInformation ( "Executing {0}" , Name ) ;
( ( TaskManager ) T askManager) . OnTaskExecuting ( this ) ;
( ( TaskManager ) _t askManager) . OnTaskExecuting ( this ) ;
progress . ProgressChanged + = OnProgressChanged ;
@ -423,7 +425,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
}
catch ( Exception ex )
{
L ogger. LogError ( ex , "Error" ) ;
_l ogger. LogError ( ex , "Error" ) ;
failureException = ex ;
@ -476,7 +478,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
{
if ( State = = TaskState . Running )
{
L ogger. LogInformation ( "Attempting to cancel Scheduled Task {0}" , Name ) ;
_l ogger. LogInformation ( "Attempting to cancel Scheduled Task {0}" , Name ) ;
CurrentCancellationTokenSource . Cancel ( ) ;
}
}
@ -487,7 +489,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
/// <returns>System.String.</returns>
private string GetScheduledTasksConfigurationDirectory ( )
{
return Path . Combine ( A pplicationPaths. ConfigurationDirectoryPath , "ScheduledTasks" ) ;
return Path . Combine ( _a pplicationPaths. ConfigurationDirectoryPath , "ScheduledTasks" ) ;
}
/// <summary>
@ -496,7 +498,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
/// <returns>System.String.</returns>
private string GetScheduledTasksDataDirectory ( )
{
return Path . Combine ( A pplicationPaths. DataPath , "ScheduledTasks" ) ;
return Path . Combine ( _a pplicationPaths. DataPath , "ScheduledTasks" ) ;
}
/// <summary>
@ -535,7 +537,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
TaskTriggerInfo [ ] list = null ;
if ( File . Exists ( path ) )
{
list = J sonSerializer. DeserializeFromFile < TaskTriggerInfo [ ] > ( path ) ;
list = _j sonSerializer. DeserializeFromFile < TaskTriggerInfo [ ] > ( path ) ;
}
// Return defaults if file doesn't exist.
@ -571,7 +573,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
Directory . CreateDirectory ( Path . GetDirectoryName ( path ) ) ;
J sonSerializer. SerializeToFile ( triggers , path ) ;
_j sonSerializer. SerializeToFile ( triggers , path ) ;
}
/// <summary>
@ -585,7 +587,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
{
var elapsedTime = endTime - startTime ;
L ogger. LogInformation ( "{0} {1} after {2} minute(s) and {3} seconds" , Name , status , Math . Truncate ( elapsedTime . TotalMinutes ) , elapsedTime . Seconds ) ;
_l ogger. LogInformation ( "{0} {1} after {2} minute(s) and {3} seconds" , Name , status , Math . Truncate ( elapsedTime . TotalMinutes ) , elapsedTime . Seconds ) ;
var result = new TaskResult
{
@ -606,7 +608,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
LastExecutionResult = result ;
( ( TaskManager ) T askManager) . OnTaskCompleted ( this , result ) ;
( ( TaskManager ) _t askManager) . OnTaskCompleted ( this , result ) ;
}
/// <summary>
@ -615,6 +617,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
public void Dispose ( )
{
Dispose ( true ) ;
GC . SuppressFinalize ( this ) ;
}
/// <summary>
@ -635,12 +638,12 @@ namespace Emby.Server.Implementations.ScheduledTasks
{
try
{
L ogger. LogInformation ( Name + ": Cancelling" ) ;
_l ogger. LogInformation ( Name + ": Cancelling" ) ;
token . Cancel ( ) ;
}
catch ( Exception ex )
{
L ogger. LogError ( ex , "Error calling CancellationToken.Cancel();" ) ;
_l ogger. LogError ( ex , "Error calling CancellationToken.Cancel();" ) ;
}
}
@ -649,21 +652,21 @@ namespace Emby.Server.Implementations.ScheduledTasks
{
try
{
L ogger. LogInformation ( Name + ": Waiting on Task" ) ;
_l ogger. LogInformation ( Name + ": Waiting on Task" ) ;
var exited = Task . WaitAll ( new [ ] { task } , 2000 ) ;
if ( exited )
{
L ogger. LogInformation ( Name + ": Task exited" ) ;
_l ogger. LogInformation ( Name + ": Task exited" ) ;
}
else
{
L ogger. LogInformation ( Name + ": Timed out waiting for task to stop" ) ;
_l ogger. LogInformation ( Name + ": Timed out waiting for task to stop" ) ;
}
}
catch ( Exception ex )
{
L ogger. LogError ( ex , "Error calling Task.WaitAll();" ) ;
_l ogger. LogError ( ex , "Error calling Task.WaitAll();" ) ;
}
}
@ -671,12 +674,12 @@ namespace Emby.Server.Implementations.ScheduledTasks
{
try
{
L ogger. LogDebug ( Name + ": Disposing CancellationToken" ) ;
_l ogger. LogDebug ( Name + ": Disposing CancellationToken" ) ;
token . Dispose ( ) ;
}
catch ( Exception ex )
{
L ogger. LogError ( ex , "Error calling CancellationToken.Dispose();" ) ;
_l ogger. LogError ( ex , "Error calling CancellationToken.Dispose();" ) ;
}
}
@ -692,8 +695,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
/// </summary>
/// <param name="info">The info.</param>
/// <returns>BaseTaskTrigger.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException">Invalid trigger type: + info.Type</exception>
/// <exception cref="ArgumentException">Invalid trigger type: + info.Type.</exception>
private ITaskTrigger GetTrigger ( TaskTriggerInfo info )
{
var options = new TaskOptions
@ -765,7 +767,7 @@ namespace Emby.Server.Implementations.ScheduledTasks
foreach ( var triggerInfo in InternalTriggers )
{
var trigger = triggerInfo . Item2 ;
trigger . Triggered - = trigger_ Triggered;
trigger . Triggered - = OnTrigger Triggered;
trigger . Stop ( ) ;
}
}