Skip to content

Commit

Permalink
refactor: modernize scheduled tasks + more
Browse files Browse the repository at this point in the history
- Modernized the scheduled tasks by updating to more modern syntax.

- Added expert mode toggles to some scheduled tasks, since not all tasks are equal, and some require extra care when running and/or shouldn't be ran on a whim. 😔
  • Loading branch information
revam committed Sep 22, 2024
1 parent bbe1c6d commit dd56da0
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 225 deletions.
31 changes: 14 additions & 17 deletions Shokofin/Tasks/CleanupVirtualRootTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
namespace Shokofin.Tasks;

/// <summary>
/// Cleanup any old VFS roots leftover from an outdated install or failed removal of the roots.
/// Clean-up any old VFS roots leftover from an outdated install or failed removal of the roots.
/// </summary>
public class CleanupVirtualRootTask : IScheduledTask, IConfigurableScheduledTask
public class CleanupVirtualRootTask(ILogger<CleanupVirtualRootTask> logger, IFileSystem fileSystem, LibraryScanWatcher scanWatcher) : IScheduledTask, IConfigurableScheduledTask
{
/// <inheritdoc />
public string Name => "Cleanup Virtual File System Roots";
public string Name => "Clean-up Virtual File System Roots";

/// <inheritdoc />
public string Description => "Cleanup any old VFS roots leftover from an outdated install or failed removal of the roots.";
public string Description => "Clean-up any old VFS roots leftover from an outdated install or failed removal of the roots.";

/// <inheritdoc />
public string Category => "Shokofin";
Expand All @@ -29,29 +29,26 @@ public class CleanupVirtualRootTask : IScheduledTask, IConfigurableScheduledTask
public string Key => "ShokoCleanupVirtualRoot";

/// <inheritdoc />
public bool IsHidden => false;
public bool IsHidden => !Plugin.Instance.Configuration.ExpertMode;

/// <inheritdoc />
public bool IsEnabled => true;

/// <inheritdoc />
public bool IsLogged => true;
public bool IsLogged => Plugin.Instance.Configuration.ExpertMode;

private readonly ILogger<CleanupVirtualRootTask> Logger;
private readonly ILogger<CleanupVirtualRootTask> Logger = logger;

private readonly IFileSystem FileSystem;
private readonly IFileSystem FileSystem = fileSystem;

private readonly LibraryScanWatcher ScanWatcher;

public CleanupVirtualRootTask(ILogger<CleanupVirtualRootTask> logger, IFileSystem fileSystem, LibraryScanWatcher scanWatcher)
{
Logger = logger;
FileSystem = fileSystem;
ScanWatcher = scanWatcher;
}
private readonly LibraryScanWatcher ScanWatcher = scanWatcher;

public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
=> Array.Empty<TaskTriggerInfo>();
=> [
new() {
Type = TaskTriggerInfo.TriggerStartup,
},
];

public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
Expand Down
27 changes: 10 additions & 17 deletions Shokofin/Tasks/ClearPluginCacheTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Shokofin.Tasks;
/// <summary>
/// Forcefully clear the plugin cache. For debugging and troubleshooting. DO NOT RUN THIS TASK WHILE A LIBRARY SCAN IS RUNNING.
/// </summary>
public class ClearPluginCacheTask : IScheduledTask, IConfigurableScheduledTask
public class ClearPluginCacheTask(ShokoAPIManager apiManager, ShokoAPIClient apiClient, VirtualFileSystemService vfsService) : IScheduledTask, IConfigurableScheduledTask
{
/// <inheritdoc />
public string Name => "Clear Plugin Cache";
Expand All @@ -26,35 +26,28 @@ public class ClearPluginCacheTask : IScheduledTask, IConfigurableScheduledTask
public string Key => "ShokoClearPluginCache";

/// <inheritdoc />
public bool IsHidden => false;
public bool IsHidden => !Plugin.Instance.Configuration.ExpertMode;

/// <inheritdoc />
public bool IsEnabled => true;
public bool IsEnabled => Plugin.Instance.Configuration.ExpertMode;

/// <inheritdoc />
public bool IsLogged => true;

private readonly ShokoAPIManager ApiManager;
private readonly ShokoAPIManager _apiManager = apiManager;

private readonly ShokoAPIClient ApiClient;
private readonly ShokoAPIClient _apiClient = apiClient;

private readonly VirtualFileSystemService VfsService;

public ClearPluginCacheTask(ShokoAPIManager apiManager, ShokoAPIClient apiClient, VirtualFileSystemService vfsService)
{
ApiManager = apiManager;
ApiClient = apiClient;
VfsService = vfsService;
}
private readonly VirtualFileSystemService _vfsService = vfsService;

public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
=> Array.Empty<TaskTriggerInfo>();
=> [];

public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
ApiClient.Clear();
ApiManager.Clear();
VfsService.Clear();
_apiClient.Clear();
_apiManager.Clear();
_vfsService.Clear();
return Task.CompletedTask;
}
}
10 changes: 3 additions & 7 deletions Shokofin/Tasks/ExportUserDataTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Shokofin.Tasks;

public class ExportUserDataTask : IScheduledTask, IConfigurableScheduledTask
public class ExportUserDataTask(UserDataSyncManager userSyncManager) : IScheduledTask, IConfigurableScheduledTask
{
/// <inheritdoc />
public string Name => "Export User Data";
Expand All @@ -30,14 +30,10 @@ public class ExportUserDataTask : IScheduledTask, IConfigurableScheduledTask
/// <inheritdoc />
public bool IsLogged => true;

private readonly UserDataSyncManager _userSyncManager;
private readonly UserDataSyncManager _userSyncManager = userSyncManager;

public ExportUserDataTask(UserDataSyncManager userSyncManager)
{
_userSyncManager = userSyncManager;
}
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
=> Array.Empty<TaskTriggerInfo>();
=> [];

public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
Expand Down
11 changes: 3 additions & 8 deletions Shokofin/Tasks/ImportUserDataTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Shokofin.Tasks;

public class ImportUserDataTask : IScheduledTask, IConfigurableScheduledTask
public class ImportUserDataTask(UserDataSyncManager userSyncManager) : IScheduledTask, IConfigurableScheduledTask
{
/// <inheritdoc />
public string Name => "Import User Data";
Expand All @@ -30,15 +30,10 @@ public class ImportUserDataTask : IScheduledTask, IConfigurableScheduledTask
/// <inheritdoc />
public bool IsLogged => true;

private readonly UserDataSyncManager _userSyncManager;

public ImportUserDataTask(UserDataSyncManager userSyncManager)
{
_userSyncManager = userSyncManager;
}
private readonly UserDataSyncManager _userSyncManager = userSyncManager;

public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
=> Array.Empty<TaskTriggerInfo>();
=> [];

public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
Expand Down
33 changes: 16 additions & 17 deletions Shokofin/Tasks/MergeEpisodesTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

namespace Shokofin.Tasks;

public class MergeEpisodesTask : IScheduledTask, IConfigurableScheduledTask
/// <summary>
/// Merge all episode entries with the same Shoko Episode ID set. For debugging and troubleshooting. DO NOT RUN THIS TASK WHILE A LIBRARY SCAN IS RUNNING.
/// </summary>
public class MergeEpisodesTask(MergeVersionsManager userSyncManager, LibraryScanWatcher libraryScanWatcher) : IScheduledTask, IConfigurableScheduledTask
{
private readonly MergeVersionsManager _mergeVersionManager = userSyncManager;

private readonly LibraryScanWatcher _libraryScanWatcher = libraryScanWatcher;

/// <inheritdoc />
public string Name => "Merge Episodes";

/// <inheritdoc />
public string Description => "Merge all episode entries with the same Shoko Episode ID set.";
public string Description => "Merge all episode entries with the same Shoko Episode ID set. For debugging and troubleshooting. DO NOT RUN THIS TASK WHILE A LIBRARY SCAN IS RUNNING.";

/// <inheritdoc />
public string Category => "Shokofin";
Expand All @@ -23,34 +30,26 @@ public class MergeEpisodesTask : IScheduledTask, IConfigurableScheduledTask
public string Key => "ShokoMergeEpisodes";

/// <inheritdoc />
public bool IsHidden => false;
public bool IsHidden => !Plugin.Instance.Configuration.ExpertMode;

/// <inheritdoc />
public bool IsEnabled => true;
public bool IsEnabled => Plugin.Instance.Configuration.ExpertMode;

/// <inheritdoc />
public bool IsLogged => true;

private readonly MergeVersionsManager VersionsManager;

private readonly LibraryScanWatcher LibraryScanWatcher;

public MergeEpisodesTask(MergeVersionsManager userSyncManager, LibraryScanWatcher libraryScanWatcher)
{
VersionsManager = userSyncManager;
LibraryScanWatcher = libraryScanWatcher;
}

/// <inheritdoc />
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
=> Array.Empty<TaskTriggerInfo>();
=> [];

/// <inheritdoc />
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
if (LibraryScanWatcher.IsScanRunning)
if (_libraryScanWatcher.IsScanRunning)
return;

using (Plugin.Instance.Tracker.Enter("Merge Episodes Task")) {
await VersionsManager.MergeAllEpisodes(progress, cancellationToken);
await _mergeVersionManager.MergeAllEpisodes(progress, cancellationToken);
}
}
}
29 changes: 14 additions & 15 deletions Shokofin/Tasks/MergeMoviesTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

namespace Shokofin.Tasks;

public class MergeMoviesTask : IScheduledTask, IConfigurableScheduledTask
/// <summary>
/// Merge all movie entries with the same Shoko Episode ID set. For debugging and troubleshooting. DO NOT RUN THIS TASK WHILE A LIBRARY SCAN IS RUNNING. <summary>
/// </summary>
public class MergeMoviesTask(MergeVersionsManager userSyncManager, LibraryScanWatcher libraryScanWatcher) : IScheduledTask, IConfigurableScheduledTask
{
private readonly MergeVersionsManager VersionsManager = userSyncManager;

private readonly LibraryScanWatcher LibraryScanWatcher = libraryScanWatcher;

/// <inheritdoc />
public string Name => "Merge Movies";

/// <inheritdoc />
public string Description => "Merge all movie entries with the same Shoko Episode ID set.";
public string Description => "Merge all movie entries with the same Shoko Episode ID set. For debugging and troubleshooting. DO NOT RUN THIS TASK WHILE A LIBRARY SCAN IS RUNNING.";

/// <inheritdoc />
public string Category => "Shokofin";
Expand All @@ -23,27 +30,19 @@ public class MergeMoviesTask : IScheduledTask, IConfigurableScheduledTask
public string Key => "ShokoMergeMovies";

/// <inheritdoc />
public bool IsHidden => false;
public bool IsHidden => !Plugin.Instance.Configuration.ExpertMode;

/// <inheritdoc />
public bool IsEnabled => true;
public bool IsEnabled => Plugin.Instance.Configuration.ExpertMode;

/// <inheritdoc />
public bool IsLogged => true;

private readonly MergeVersionsManager VersionsManager;

private readonly LibraryScanWatcher LibraryScanWatcher;

public MergeMoviesTask(MergeVersionsManager userSyncManager, LibraryScanWatcher libraryScanWatcher)
{
VersionsManager = userSyncManager;
LibraryScanWatcher = libraryScanWatcher;
}

/// <inheritdoc />
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
=> Array.Empty<TaskTriggerInfo>();
=> [];

/// <inheritdoc />
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
if (LibraryScanWatcher.IsScanRunning)
Expand Down
Loading

0 comments on commit dd56da0

Please sign in to comment.