using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Tasks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Implementations.ScheduledTasks
{
///
/// Class ScheduledTaskWorker
///
public class ScheduledTaskWorker : IScheduledTaskWorker
{
///
/// Gets or sets the scheduled task.
///
/// The scheduled task.
public IScheduledTask ScheduledTask { get; private set; }
///
/// Gets or sets the json serializer.
///
/// The json serializer.
private IJsonSerializer JsonSerializer { get; set; }
///
/// Gets or sets the application paths.
///
/// The application paths.
private IApplicationPaths ApplicationPaths { get; set; }
///
/// Gets the logger.
///
/// The logger.
private ILogger Logger { get; set; }
///
/// Gets the task manager.
///
/// The task manager.
private ITaskManager TaskManager { get; set; }
///
/// Initializes a new instance of the class.
///
/// The scheduled task.
/// The application paths.
/// The task manager.
/// The json serializer.
/// The logger.
///
/// scheduledTask
/// or
/// applicationPaths
/// or
/// taskManager
/// or
/// jsonSerializer
/// or
/// logger
///
public ScheduledTaskWorker(IScheduledTask scheduledTask, IApplicationPaths applicationPaths, ITaskManager taskManager, IJsonSerializer jsonSerializer, ILogger logger)
{
if (scheduledTask == null)
{
throw new ArgumentNullException("scheduledTask");
}
if (applicationPaths == null)
{
throw new ArgumentNullException("applicationPaths");
}
if (taskManager == null)
{
throw new ArgumentNullException("taskManager");
}
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
if (logger == null)
{
throw new ArgumentNullException("logger");
}
ScheduledTask = scheduledTask;
ApplicationPaths = applicationPaths;
TaskManager = taskManager;
JsonSerializer = jsonSerializer;
Logger = logger;
ReloadTriggerEvents(true);
}
///
/// The _last execution result
///
private TaskResult _lastExecutionResult;
///
/// The _last execution resultinitialized
///
private bool _lastExecutionResultinitialized;
///
/// The _last execution result sync lock
///
private object _lastExecutionResultSyncLock = new object();
///
/// Gets the last execution result.
///
/// The last execution result.
public TaskResult LastExecutionResult
{
get
{
LazyInitializer.EnsureInitialized(ref _lastExecutionResult, ref _lastExecutionResultinitialized, ref _lastExecutionResultSyncLock, () =>
{
try
{
return JsonSerializer.DeserializeFromFile(GetHistoryFilePath(false));
}
catch (DirectoryNotFoundException)
{
// File doesn't exist. No biggie
return null;
}
catch (FileNotFoundException)
{
// File doesn't exist. No biggie
return null;
}
});
return _lastExecutionResult;
}
private set
{
_lastExecutionResult = value;
_lastExecutionResultinitialized = value != null;
}
}
///
/// Gets the name.
///
/// The name.
public string Name
{
get { return ScheduledTask.Name; }
}
///
/// Gets the description.
///
/// The description.
public string Description
{
get { return ScheduledTask.Description; }
}
///
/// Gets the category.
///
/// The category.
public string Category
{
get { return ScheduledTask.Category; }
}
///
/// Gets the current cancellation token
///
/// The current cancellation token source.
private CancellationTokenSource CurrentCancellationTokenSource { get; set; }
///
/// Gets or sets the current execution start time.
///
/// The current execution start time.
private DateTime CurrentExecutionStartTime { get; set; }
///
/// Gets the state.
///
/// The state.
public TaskState State
{
get
{
if (CurrentCancellationTokenSource != null)
{
return CurrentCancellationTokenSource.IsCancellationRequested
? TaskState.Cancelling
: TaskState.Running;
}
return TaskState.Idle;
}
}
///
/// Gets the current progress.
///
/// The current progress.
public double? CurrentProgress { get; private set; }
///
/// The _triggers
///
private IEnumerable _triggers;
///
/// The _triggers initialized
///
private bool _triggersInitialized;
///
/// The _triggers sync lock
///
private object _triggersSyncLock = new object();
///
/// Gets the triggers that define when the task will run
///
/// The triggers.
/// value
public IEnumerable Triggers
{
get
{
LazyInitializer.EnsureInitialized(ref _triggers, ref _triggersInitialized, ref _triggersSyncLock, LoadTriggers);
return _triggers;
}
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
// Cleanup current triggers
if (_triggers != null)
{
DisposeTriggers();
}
_triggers = value.ToList();
_triggersInitialized = true;
ReloadTriggerEvents(false);
SaveTriggers(_triggers);
}
}
///
/// The _id
///
private Guid? _id;
///
/// Gets the unique id.
///
/// The unique id.
public Guid Id
{
get
{
if (!_id.HasValue)
{
_id = ScheduledTask.GetType().FullName.GetMD5();
}
return _id.Value;
}
}
///
/// Reloads the trigger events.
///
/// if set to true [is application startup].
private void ReloadTriggerEvents(bool isApplicationStartup)
{
foreach (var trigger in Triggers)
{
trigger.Stop();
trigger.Triggered -= trigger_Triggered;
trigger.Triggered += trigger_Triggered;
trigger.Start(isApplicationStartup);
}
}
///
/// Handles the Triggered event of the trigger control.
///
/// The source of the event.
/// The instance containing the event data.
async void trigger_Triggered(object sender, EventArgs e)
{
var trigger = (ITaskTrigger)sender;
Logger.Info("{0} fired for task: {1}", trigger.GetType().Name, Name);
trigger.Stop();
TaskManager.QueueScheduledTask(ScheduledTask);
await Task.Delay(1000).ConfigureAwait(false);
trigger.Start(false);
}
///
/// Executes the task
///
/// Task.
/// Cannot execute a Task that is already running
public async Task Execute()
{
// Cancel the current execution, if any
if (CurrentCancellationTokenSource != null)
{
throw new InvalidOperationException("Cannot execute a Task that is already running");
}
CurrentCancellationTokenSource = new CancellationTokenSource();
Logger.Info("Executing {0}", Name);
((TaskManager)TaskManager).OnTaskExecuting(ScheduledTask);
var progress = new Progress();
progress.ProgressChanged += progress_ProgressChanged;
TaskCompletionStatus status;
CurrentExecutionStartTime = DateTime.UtcNow;
try
{
await ExecuteTask(CurrentCancellationTokenSource.Token, progress).ConfigureAwait(false);
status = TaskCompletionStatus.Completed;
}
catch (OperationCanceledException)
{
status = TaskCompletionStatus.Cancelled;
}
catch (Exception ex)
{
Logger.ErrorException("Error", ex);
status = TaskCompletionStatus.Failed;
}
var startTime = CurrentExecutionStartTime;
var endTime = DateTime.UtcNow;
progress.ProgressChanged -= progress_ProgressChanged;
CurrentCancellationTokenSource.Dispose();
CurrentCancellationTokenSource = null;
CurrentProgress = null;
OnTaskCompleted(startTime, endTime, status);
}
///
/// Executes the task.
///
/// The cancellation token.
/// The progress.
/// Task.
private Task ExecuteTask(CancellationToken cancellationToken, IProgress progress)
{
return Task.Run(async () => await ScheduledTask.Execute(cancellationToken, progress).ConfigureAwait(false));
}
///
/// Progress_s the progress changed.
///
/// The sender.
/// The e.
void progress_ProgressChanged(object sender, double e)
{
CurrentProgress = e;
}
///
/// Stops the task if it is currently executing
///
/// Cannot cancel a Task unless it is in the Running state.
public void Cancel()
{
if (State != TaskState.Running)
{
throw new InvalidOperationException("Cannot cancel a Task unless it is in the Running state.");
}
CancelIfRunning();
}
///
/// Cancels if running.
///
public void CancelIfRunning()
{
if (State == TaskState.Running)
{
Logger.Info("Attempting to cancel Scheduled Task {0}", Name);
CurrentCancellationTokenSource.Cancel();
}
}
///
/// Gets the scheduled tasks configuration directory.
///
/// if set to true [create].
/// System.String.
private string GetScheduledTasksConfigurationDirectory(bool create)
{
var path = Path.Combine(ApplicationPaths.ConfigurationDirectoryPath, "ScheduledTasks");
if (create && !Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
///
/// Gets the scheduled tasks data directory.
///
/// if set to true [create].
/// System.String.
private string GetScheduledTasksDataDirectory(bool create)
{
var path = Path.Combine(ApplicationPaths.DataPath, "ScheduledTasks");
if (create && !Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
///
/// Gets the history file path.
///
/// The history file path.
private string GetHistoryFilePath(bool createDirectory)
{
return Path.Combine(GetScheduledTasksDataDirectory(createDirectory), Id + ".js");
}
///
/// Gets the configuration file path.
///
/// System.String.
private string GetConfigurationFilePath()
{
return Path.Combine(GetScheduledTasksConfigurationDirectory(false), Id + ".js");
}
///
/// Loads the triggers.
///
/// IEnumerable{BaseTaskTrigger}.
private IEnumerable LoadTriggers()
{
try
{
return JsonSerializer.DeserializeFromFile>(GetConfigurationFilePath())
.Select(ScheduledTaskHelpers.GetTrigger)
.ToList();
}
catch (FileNotFoundException)
{
// File doesn't exist. No biggie. Return defaults.
return ScheduledTask.GetDefaultTriggers();
}
catch (DirectoryNotFoundException)
{
// File doesn't exist. No biggie. Return defaults.
return ScheduledTask.GetDefaultTriggers();
}
}
///
/// Saves the triggers.
///
/// The triggers.
private void SaveTriggers(IEnumerable triggers)
{
var path = GetConfigurationFilePath();
var parentPath = Path.GetDirectoryName(path);
if (!Directory.Exists(parentPath))
{
Directory.CreateDirectory(parentPath);
}
JsonSerializer.SerializeToFile(triggers.Select(ScheduledTaskHelpers.GetTriggerInfo), path);
}
///
/// Called when [task completed].
///
/// The start time.
/// The end time.
/// The status.
private void OnTaskCompleted(DateTime startTime, DateTime endTime, TaskCompletionStatus status)
{
var elapsedTime = endTime - startTime;
Logger.Info("{0} {1} after {2} minute(s) and {3} seconds", Name, status, Math.Truncate(elapsedTime.TotalMinutes), elapsedTime.Seconds);
var result = new TaskResult
{
StartTimeUtc = startTime,
EndTimeUtc = endTime,
Status = status,
Name = Name,
Id = Id
};
JsonSerializer.SerializeToFile(result, GetHistoryFilePath(true));
LastExecutionResult = result;
((TaskManager) TaskManager).OnTaskCompleted(ScheduledTask, result);
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases unmanaged and - optionally - managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected virtual void Dispose(bool dispose)
{
if (dispose)
{
DisposeTriggers();
if (State == TaskState.Running)
{
OnTaskCompleted(CurrentExecutionStartTime, DateTime.UtcNow, TaskCompletionStatus.Aborted);
}
if (CurrentCancellationTokenSource != null)
{
CurrentCancellationTokenSource.Dispose();
}
}
}
///
/// Disposes each trigger
///
private void DisposeTriggers()
{
foreach (var trigger in Triggers)
{
trigger.Triggered -= trigger_Triggered;
trigger.Stop();
}
}
}
}