From 08ed0a9a5d2a241abe4304e03fab4ae74291e3c9 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Wed, 4 Sep 2024 17:38:10 +0200 Subject: [PATCH] Cleanup tasks --- .../ScheduledTasks/ScheduledTaskWorker.cs | 105 ++++-------------- .../ScheduledTasks/TaskManager.cs | 49 +++----- .../Tasks/AudioNormalizationTask.cs | 2 +- .../ScheduledTasks/Tasks/ChapterImagesTask.cs | 28 ++--- .../Tasks/CleanActivityLogTask.cs | 3 +- .../CleanupCollectionAndPlaylistPathsTask.cs | 8 +- .../Tasks/DeleteCacheFileTask.cs | 11 +- .../ScheduledTasks/Tasks/DeleteLogFileTask.cs | 17 ++- .../Tasks/DeleteTranscodeFileTask.cs | 10 +- .../Tasks/OptimizeDatabaseTask.cs | 17 ++- .../Tasks/PeopleValidationTask.cs | 18 +-- .../ScheduledTasks/Tasks/PluginUpdateTask.cs | 16 ++- .../Tasks/RefreshMediaLibraryTask.cs | 7 +- .../ScheduledTasks/Triggers/DailyTrigger.cs | 20 +--- .../ScheduledTasks/Triggers/StartupTrigger.cs | 24 +--- .../ScheduledTasks/Triggers/WeeklyTrigger.cs | 22 +--- .../Tasks/IConfigurableScheduledTask.cs | 9 +- .../Tasks/IScheduledTaskWorker.cs | 3 +- MediaBrowser.Model/Tasks/ITaskManager.cs | 31 +++++- .../Tasks/TaskCompletionEventArgs.cs | 18 ++- MediaBrowser.Model/Tasks/TaskInfo.cs | 5 +- MediaBrowser.Model/Tasks/TaskOptions.cs | 9 +- MediaBrowser.Model/Tasks/TaskTriggerInfo.cs | 17 ++- 23 files changed, 191 insertions(+), 258 deletions(-) diff --git a/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs b/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs index 40e1bbf15..9b342cfbe 100644 --- a/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs +++ b/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs @@ -1,8 +1,7 @@ #nullable disable -#pragma warning disable CS1591 - using System; +using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; @@ -24,51 +23,15 @@ namespace Emby.Server.Implementations.ScheduledTasks /// public class ScheduledTaskWorker : IScheduledTaskWorker { - /// - /// The options for the json Serializer. - /// private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; - - /// - /// Gets or sets the application paths. - /// - /// The application paths. private readonly IApplicationPaths _applicationPaths; - - /// - /// Gets or sets the logger. - /// - /// The logger. private readonly ILogger _logger; - - /// - /// Gets or sets the task manager. - /// - /// The task manager. private readonly ITaskManager _taskManager; - - /// - /// The _last execution result sync lock. - /// - private readonly object _lastExecutionResultSyncLock = new object(); - - private bool _readFromFile = false; - - /// - /// The _last execution result. - /// + private readonly object _lastExecutionResultSyncLock = new(); + private bool _readFromFile; private TaskResult _lastExecutionResult; - private Task _currentTask; - - /// - /// The _triggers. - /// private Tuple[] _triggers; - - /// - /// The _id. - /// private string _id; /// @@ -104,18 +67,13 @@ namespace Emby.Server.Implementations.ScheduledTasks InitTriggerEvents(); } + /// public event EventHandler> TaskProgress; - /// - /// Gets the scheduled task. - /// - /// The scheduled task. + /// public IScheduledTask ScheduledTask { get; private set; } - /// - /// Gets the last execution result. - /// - /// The last execution result. + /// public TaskResult LastExecutionResult { get @@ -169,22 +127,13 @@ namespace Emby.Server.Implementations.ScheduledTasks } } - /// - /// Gets the name. - /// - /// The name. + /// public string Name => ScheduledTask.Name; - /// - /// Gets the description. - /// - /// The description. + /// public string Description => ScheduledTask.Description; - /// - /// Gets the category. - /// - /// The category. + /// public string Category => ScheduledTask.Category; /// @@ -199,10 +148,7 @@ namespace Emby.Server.Implementations.ScheduledTasks /// The current execution start time. private DateTime CurrentExecutionStartTime { get; set; } - /// - /// Gets the state. - /// - /// The state. + /// public TaskState State { get @@ -218,10 +164,7 @@ namespace Emby.Server.Implementations.ScheduledTasks } } - /// - /// Gets the current progress. - /// - /// The current progress. + /// public double? CurrentProgress { get; private set; } /// @@ -247,12 +190,8 @@ namespace Emby.Server.Implementations.ScheduledTasks } } - /// - /// Gets or sets the triggers that define when the task will run. - /// - /// The triggers. - /// value is null. - public TaskTriggerInfo[] Triggers + /// + public IReadOnlyList Triggers { get { @@ -272,10 +211,7 @@ namespace Emby.Server.Implementations.ScheduledTasks } } - /// - /// Gets the unique id. - /// - /// The unique id. + /// public string Id { get @@ -290,6 +226,7 @@ namespace Emby.Server.Implementations.ScheduledTasks ReloadTriggerEvents(true); } + /// public void ReloadTriggerEvents() { ReloadTriggerEvents(false); @@ -529,14 +466,14 @@ namespace Emby.Server.Implementations.ScheduledTasks } catch { - return new TaskTriggerInfo[] - { - new TaskTriggerInfo + return + [ + new() { IntervalTicks = TimeSpan.FromDays(1).Ticks, Type = TaskTriggerInfo.TriggerInterval } - }; + ]; } } @@ -589,9 +526,7 @@ namespace Emby.Server.Implementations.ScheduledTasks ((TaskManager)_taskManager).OnTaskCompleted(this, result); } - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// + /// public void Dispose() { Dispose(true); diff --git a/Emby.Server.Implementations/ScheduledTasks/TaskManager.cs b/Emby.Server.Implementations/ScheduledTasks/TaskManager.cs index 42c30c959..a5e4104ff 100644 --- a/Emby.Server.Implementations/ScheduledTasks/TaskManager.cs +++ b/Emby.Server.Implementations/ScheduledTasks/TaskManager.cs @@ -1,5 +1,3 @@ -#pragma warning disable CS1591 - using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -41,21 +39,16 @@ namespace Emby.Server.Implementations.ScheduledTasks ScheduledTasks = Array.Empty(); } + /// public event EventHandler>? TaskExecuting; + /// public event EventHandler? TaskCompleted; - /// - /// Gets the list of Scheduled Tasks. - /// - /// The scheduled tasks. - public IScheduledTaskWorker[] ScheduledTasks { get; private set; } + /// + public IReadOnlyList ScheduledTasks { get; private set; } - /// - /// Cancels if running and queue. - /// - /// The task type. - /// Task options. + /// public void CancelIfRunningAndQueue(TaskOptions options) where T : IScheduledTask { @@ -65,16 +58,14 @@ namespace Emby.Server.Implementations.ScheduledTasks QueueScheduledTask(options); } + /// public void CancelIfRunningAndQueue() where T : IScheduledTask { CancelIfRunningAndQueue(new TaskOptions()); } - /// - /// Cancels if running. - /// - /// The task type. + /// public void CancelIfRunning() where T : IScheduledTask { @@ -82,11 +73,7 @@ namespace Emby.Server.Implementations.ScheduledTasks ((ScheduledTaskWorker)task).CancelIfRunning(); } - /// - /// Queues the scheduled task. - /// - /// The task type. - /// Task options. + /// public void QueueScheduledTask(TaskOptions options) where T : IScheduledTask { @@ -102,12 +89,14 @@ namespace Emby.Server.Implementations.ScheduledTasks } } + /// public void QueueScheduledTask() where T : IScheduledTask { QueueScheduledTask(new TaskOptions()); } + /// public void QueueIfNotRunning() where T : IScheduledTask { @@ -119,6 +108,7 @@ namespace Emby.Server.Implementations.ScheduledTasks } } + /// public void Execute() where T : IScheduledTask { @@ -144,11 +134,7 @@ namespace Emby.Server.Implementations.ScheduledTasks } } - /// - /// Queues the scheduled task. - /// - /// The task. - /// The task options. + /// public void QueueScheduledTask(IScheduledTask task, TaskOptions options) { var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == task.GetType()); @@ -186,10 +172,7 @@ namespace Emby.Server.Implementations.ScheduledTasks } } - /// - /// Adds the tasks. - /// - /// The tasks. + /// public void AddTasks(IEnumerable tasks) { var list = tasks.Select(t => new ScheduledTaskWorker(t, _applicationPaths, this, _logger)); @@ -197,9 +180,7 @@ namespace Emby.Server.Implementations.ScheduledTasks ScheduledTasks = ScheduledTasks.Concat(list).ToArray(); } - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// + /// public void Dispose() { Dispose(true); @@ -218,11 +199,13 @@ namespace Emby.Server.Implementations.ScheduledTasks } } + /// public void Cancel(IScheduledTaskWorker task) { ((ScheduledTaskWorker)task).Cancel(); } + /// public Task Execute(IScheduledTaskWorker task, TaskOptions options) { return ((ScheduledTaskWorker)task).Execute(options); diff --git a/Emby.Server.Implementations/ScheduledTasks/Tasks/AudioNormalizationTask.cs b/Emby.Server.Implementations/ScheduledTasks/Tasks/AudioNormalizationTask.cs index 301c04915..eb6afe05d 100644 --- a/Emby.Server.Implementations/ScheduledTasks/Tasks/AudioNormalizationTask.cs +++ b/Emby.Server.Implementations/ScheduledTasks/Tasks/AudioNormalizationTask.cs @@ -142,7 +142,7 @@ public partial class AudioNormalizationTask : IScheduledTask continue; } - t.LUFS = await CalculateLUFSAsync(string.Format(CultureInfo.InvariantCulture, "-i \"{0}\"", t.Path.Replace("\"", "\\\"", StringComparison.Ordinal)), cancellationToken); + t.LUFS = await CalculateLUFSAsync(string.Format(CultureInfo.InvariantCulture, "-i \"{0}\"", t.Path.Replace("\"", "\\\"", StringComparison.Ordinal)), cancellationToken).ConfigureAwait(false); } _itemRepository.SaveItems(tracks, cancellationToken); diff --git a/Emby.Server.Implementations/ScheduledTasks/Tasks/ChapterImagesTask.cs b/Emby.Server.Implementations/ScheduledTasks/Tasks/ChapterImagesTask.cs index 36456504b..cb3f5b836 100644 --- a/Emby.Server.Implementations/ScheduledTasks/Tasks/ChapterImagesTask.cs +++ b/Emby.Server.Implementations/ScheduledTasks/Tasks/ChapterImagesTask.cs @@ -36,13 +36,13 @@ namespace Emby.Server.Implementations.ScheduledTasks.Tasks /// /// Initializes a new instance of the class. /// - /// The logger.. - /// The library manager.. - /// The item repository. - /// The application paths. - /// The encoding manager. - /// The filesystem. - /// The localization manager. + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. public ChapterImagesTask( ILogger logger, ILibraryManager libraryManager, @@ -76,15 +76,15 @@ namespace Emby.Server.Implementations.ScheduledTasks.Tasks /// public IEnumerable GetDefaultTriggers() { - return new[] - { + return + [ new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerDaily, TimeOfDayTicks = TimeSpan.FromHours(2).Ticks, MaxRuntimeTicks = TimeSpan.FromHours(4).Ticks } - }; + ]; } /// @@ -92,18 +92,18 @@ namespace Emby.Server.Implementations.ScheduledTasks.Tasks { var videos = _libraryManager.GetItemList(new InternalItemsQuery { - MediaTypes = new[] { MediaType.Video }, + MediaTypes = [MediaType.Video], IsFolder = false, Recursive = true, DtoOptions = new DtoOptions(false) { EnableImages = false }, - SourceTypes = new SourceType[] { SourceType.Library }, + SourceTypes = [SourceType.Library], IsVirtualItem = false }) - .OfType public sealed class StartupTrigger : ITaskTrigger { - public const int DelayMs = 3000; + private const int DelayMs = 3000; /// /// Initializes a new instance of the class. @@ -23,23 +21,13 @@ namespace Emby.Server.Implementations.ScheduledTasks.Triggers TaskOptions = taskOptions; } - /// - /// Occurs when [triggered]. - /// + /// public event EventHandler? Triggered; - /// - /// Gets the options of this task. - /// + /// public TaskOptions TaskOptions { get; } - /// - /// Stars waiting for the trigger action. - /// - /// The last result. - /// The logger. - /// The name of the task. - /// if set to true [is application startup]. + /// public async void Start(TaskResult? lastResult, ILogger logger, string taskName, bool isApplicationStartup) { if (isApplicationStartup) @@ -50,9 +38,7 @@ namespace Emby.Server.Implementations.ScheduledTasks.Triggers } } - /// - /// Stops waiting for the trigger action. - /// + /// public void Stop() { } diff --git a/Emby.Server.Implementations/ScheduledTasks/Triggers/WeeklyTrigger.cs b/Emby.Server.Implementations/ScheduledTasks/Triggers/WeeklyTrigger.cs index fab49f2fb..ad94fdda5 100644 --- a/Emby.Server.Implementations/ScheduledTasks/Triggers/WeeklyTrigger.cs +++ b/Emby.Server.Implementations/ScheduledTasks/Triggers/WeeklyTrigger.cs @@ -13,7 +13,7 @@ namespace Emby.Server.Implementations.ScheduledTasks.Triggers private readonly TimeSpan _timeOfDay; private readonly DayOfWeek _dayOfWeek; private Timer? _timer; - private bool _disposed = false; + private bool _disposed; /// /// Initializes a new instance of the class. @@ -28,23 +28,13 @@ namespace Emby.Server.Implementations.ScheduledTasks.Triggers TaskOptions = taskOptions; } - /// - /// Occurs when [triggered]. - /// + /// public event EventHandler? Triggered; - /// - /// Gets the options of this task. - /// + /// public TaskOptions TaskOptions { get; } - /// - /// Stars waiting for the trigger action. - /// - /// The last result. - /// The logger. - /// The name of the task. - /// if set to true [is application startup]. + /// public void Start(TaskResult? lastResult, ILogger logger, string taskName, bool isApplicationStartup) { DisposeTimer(); @@ -81,9 +71,7 @@ namespace Emby.Server.Implementations.ScheduledTasks.Triggers return triggerDate.Add(_timeOfDay); } - /// - /// Stops waiting for the trigger action. - /// + /// public void Stop() { DisposeTimer(); diff --git a/MediaBrowser.Model/Tasks/IConfigurableScheduledTask.cs b/MediaBrowser.Model/Tasks/IConfigurableScheduledTask.cs index 6212d76f7..b4f99208d 100644 --- a/MediaBrowser.Model/Tasks/IConfigurableScheduledTask.cs +++ b/MediaBrowser.Model/Tasks/IConfigurableScheduledTask.cs @@ -1,7 +1,8 @@ -#pragma warning disable CS1591 - namespace MediaBrowser.Model.Tasks { + /// + /// Interface for configurable scheduled tasks. + /// public interface IConfigurableScheduledTask { /// @@ -16,6 +17,10 @@ namespace MediaBrowser.Model.Tasks /// true if this instance is enabled; otherwise, false. bool IsEnabled { get; } + /// + /// Gets a value indicating whether this instance is logged. + /// + /// true if this instance is logged; otherwise, false. bool IsLogged { get; } } } diff --git a/MediaBrowser.Model/Tasks/IScheduledTaskWorker.cs b/MediaBrowser.Model/Tasks/IScheduledTaskWorker.cs index ca769e26b..dcf849fcd 100644 --- a/MediaBrowser.Model/Tasks/IScheduledTaskWorker.cs +++ b/MediaBrowser.Model/Tasks/IScheduledTaskWorker.cs @@ -1,5 +1,6 @@ #nullable disable using System; +using System.Collections.Generic; using Jellyfin.Data.Events; namespace MediaBrowser.Model.Tasks @@ -60,7 +61,7 @@ namespace MediaBrowser.Model.Tasks /// Gets or sets the triggers that define when the task will run. /// /// The triggers. - TaskTriggerInfo[] Triggers { get; set; } + IReadOnlyList Triggers { get; set; } /// /// Gets the unique id. diff --git a/MediaBrowser.Model/Tasks/ITaskManager.cs b/MediaBrowser.Model/Tasks/ITaskManager.cs index 5b55667e8..6066bbde4 100644 --- a/MediaBrowser.Model/Tasks/ITaskManager.cs +++ b/MediaBrowser.Model/Tasks/ITaskManager.cs @@ -1,5 +1,3 @@ -#pragma warning disable CS1591 - using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -7,17 +5,26 @@ using Jellyfin.Data.Events; namespace MediaBrowser.Model.Tasks { + /// + /// Interface for the TaskManager class. + /// public interface ITaskManager : IDisposable { + /// + /// Event handler for task execution. + /// event EventHandler>? TaskExecuting; + /// + /// Event handler for task completion. + /// event EventHandler? TaskCompleted; /// /// Gets the list of Scheduled Tasks. /// /// The scheduled tasks. - IScheduledTaskWorker[] ScheduledTasks { get; } + IReadOnlyList ScheduledTasks { get; } /// /// Cancels if running and queue. @@ -56,6 +63,10 @@ namespace MediaBrowser.Model.Tasks void QueueScheduledTask() where T : IScheduledTask; + /// + /// Queues the scheduled task if it is not already running. + /// + /// An implementation of . void QueueIfNotRunning() where T : IScheduledTask; @@ -72,10 +83,24 @@ namespace MediaBrowser.Model.Tasks /// The tasks. void AddTasks(IEnumerable tasks); + /// + /// Adds the tasks. + /// + /// The tasks. void Cancel(IScheduledTaskWorker task); + /// + /// Executes the tasks. + /// + /// The tasks. + /// The options. + /// The executed tasks. Task Execute(IScheduledTaskWorker task, TaskOptions options); + /// + /// Executes the tasks. + /// + /// An implementation of . void Execute() where T : IScheduledTask; } diff --git a/MediaBrowser.Model/Tasks/TaskCompletionEventArgs.cs b/MediaBrowser.Model/Tasks/TaskCompletionEventArgs.cs index 48950667e..ab2c0ada5 100644 --- a/MediaBrowser.Model/Tasks/TaskCompletionEventArgs.cs +++ b/MediaBrowser.Model/Tasks/TaskCompletionEventArgs.cs @@ -1,19 +1,33 @@ -#pragma warning disable CS1591 - using System; namespace MediaBrowser.Model.Tasks { + /// + /// Class containing event arguments for task completion. + /// public class TaskCompletionEventArgs : EventArgs { + /// + /// Initializes a new instance of the class. + /// + /// Instance of the interface. + /// The task result. public TaskCompletionEventArgs(IScheduledTaskWorker task, TaskResult result) { Task = task; Result = result; } + /// + /// Gets the task. + /// + /// The task. public IScheduledTaskWorker Task { get; } + /// + /// Gets the result. + /// + /// The result. public TaskResult Result { get; } } } diff --git a/MediaBrowser.Model/Tasks/TaskInfo.cs b/MediaBrowser.Model/Tasks/TaskInfo.cs index 16de0b121..8c8ddc597 100644 --- a/MediaBrowser.Model/Tasks/TaskInfo.cs +++ b/MediaBrowser.Model/Tasks/TaskInfo.cs @@ -1,5 +1,6 @@ #nullable disable using System; +using System.Collections.Generic; namespace MediaBrowser.Model.Tasks { @@ -13,7 +14,7 @@ namespace MediaBrowser.Model.Tasks /// public TaskInfo() { - Triggers = Array.Empty(); + Triggers = []; } /// @@ -50,7 +51,7 @@ namespace MediaBrowser.Model.Tasks /// Gets or sets the triggers. /// /// The triggers. - public TaskTriggerInfo[] Triggers { get; set; } + public IReadOnlyList Triggers { get; set; } /// /// Gets or sets the description. diff --git a/MediaBrowser.Model/Tasks/TaskOptions.cs b/MediaBrowser.Model/Tasks/TaskOptions.cs index 3a221b878..799c0554b 100644 --- a/MediaBrowser.Model/Tasks/TaskOptions.cs +++ b/MediaBrowser.Model/Tasks/TaskOptions.cs @@ -1,9 +1,14 @@ -#pragma warning disable CS1591 - namespace MediaBrowser.Model.Tasks { + /// + /// Class containing options for tasks. + /// public class TaskOptions { + /// + /// Gets or sets the maximum runtime in ticks. + /// + /// The ticks. public long? MaxRuntimeTicks { get; set; } } } diff --git a/MediaBrowser.Model/Tasks/TaskTriggerInfo.cs b/MediaBrowser.Model/Tasks/TaskTriggerInfo.cs index 1d8767dc1..63709557d 100644 --- a/MediaBrowser.Model/Tasks/TaskTriggerInfo.cs +++ b/MediaBrowser.Model/Tasks/TaskTriggerInfo.cs @@ -1,6 +1,4 @@ #nullable disable -#pragma warning disable CS1591 - using System; namespace MediaBrowser.Model.Tasks @@ -10,9 +8,24 @@ namespace MediaBrowser.Model.Tasks /// public class TaskTriggerInfo { + /// + /// The daily trigger. + /// public const string TriggerDaily = "DailyTrigger"; + + /// + /// The weekly trigger. + /// public const string TriggerWeekly = "WeeklyTrigger"; + + /// + /// The interval trigger. + /// public const string TriggerInterval = "IntervalTrigger"; + + /// + /// The startup trigger. + /// public const string TriggerStartup = "StartupTrigger"; ///