From 1b9ebd096bc48275c03ae1e461f05802d841bd98 Mon Sep 17 00:00:00 2001 From: glax Date: Wed, 31 May 2023 21:15:32 +0200 Subject: [PATCH] Removed TaskExecutor TrangaTask is now abstract TrangaTask implements Execute Method, that is now called instead of TaskExecutor Created inheriting classes of TrangaTask: UpdateKomgaLibraryTask, DownloadNewChaptersTask --- Tranga-CLI/Tranga_Cli.cs | 2 +- Tranga/TaskExecutor.cs | 127 ------------------ Tranga/TaskManager.cs | 40 +++--- Tranga/TrangaTask.cs | 16 +-- Tranga/TrangaTasks/DownloadNewChaptersTask.cs | 46 +++++++ Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs | 19 +++ 6 files changed, 91 insertions(+), 159 deletions(-) delete mode 100644 Tranga/TaskExecutor.cs create mode 100644 Tranga/TrangaTasks/DownloadNewChaptersTask.cs create mode 100644 Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga-CLI/Tranga_Cli.cs index 94c3c92..7a0133d 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga-CLI/Tranga_Cli.cs @@ -344,7 +344,7 @@ public static class Tranga_Cli } Publication? publication = null; - if (task != TrangaTask.Task.UpdatePublications && task != TrangaTask.Task.UpdateKomgaLibrary) + if (task != TrangaTask.Task.UpdateKomgaLibrary) { publication = SelectPublication(taskManager, connector!, logger); if (publication is null) diff --git a/Tranga/TaskExecutor.cs b/Tranga/TaskExecutor.cs deleted file mode 100644 index 7a09372..0000000 --- a/Tranga/TaskExecutor.cs +++ /dev/null @@ -1,127 +0,0 @@ -using Logging; - -namespace Tranga; - -/// -/// Executes TrangaTasks -/// Based on the TrangaTask.Task a method is called. -/// The chapterCollection is updated with new Publications/Chapters. -/// -public static class TaskExecutor -{ - /// - /// Executes TrangaTask. - /// - /// Parent - /// Task to execute - /// Current chapterCollection to update - /// - /// Is thrown when there is no Connector available with the name of the TrangaTask.connectorName - public static void Execute(TaskManager taskManager, TrangaTask trangaTask, Logger? logger) - { - //Only execute task if it is not already being executed. - if (trangaTask.state == TrangaTask.ExecutionState.Running) - { - logger?.WriteLine("TaskExecutor", $"Task already running {trangaTask}"); - return; - } - trangaTask.state = TrangaTask.ExecutionState.Running; - logger?.WriteLine("TaskExecutor", $"Starting Task {trangaTask}"); - - //Connector is not needed for all tasks - Connector? connector = null; - if (trangaTask.task != TrangaTask.Task.UpdateKomgaLibrary) - connector = taskManager.GetConnector(trangaTask.connectorName!); - - //Call appropriate Method based on TrangaTask.Task - switch (trangaTask.task) - { - case TrangaTask.Task.DownloadNewChapters: - DownloadNewChapters(connector!, (Publication)trangaTask.publication!, trangaTask.language, ref taskManager._chapterCollection, taskManager.settings); - break; - case TrangaTask.Task.UpdateChapters: - UpdateChapters(connector!, (Publication)trangaTask.publication!, trangaTask.language, ref taskManager._chapterCollection); - break; - case TrangaTask.Task.UpdatePublications: - UpdatePublications(connector!, ref taskManager._chapterCollection); - break; - case TrangaTask.Task.UpdateKomgaLibrary: - UpdateKomgaLibrary(taskManager); - break; - } - - logger?.WriteLine("TaskExecutor", $"Task finished! {trangaTask}"); - trangaTask.lastExecuted = DateTime.Now; - trangaTask.state = TrangaTask.ExecutionState.Waiting; - } - - /// - /// Updates all Komga-Libraries - /// - /// Parent - private static void UpdateKomgaLibrary(TaskManager taskManager) - { - if (taskManager.komga is null) - return; - Komga komga = taskManager.komga; - - Komga.KomgaLibrary[] allLibraries = komga.GetLibraries(); - foreach (Komga.KomgaLibrary lib in allLibraries) - komga.UpdateLibrary(lib.id); - } - - /// - /// Updates the available Publications from a Connector (all of them) - /// - /// Connector to receive Publications from - /// - private static void UpdatePublications(Connector connector, ref Dictionary> chapterCollection) - { - Publication[] publications = connector.GetPublications(); - foreach (Publication publication in publications) - chapterCollection.TryAdd(publication, new List()); - } - - /// - /// Checks for new Chapters and Downloads new ones. - /// If no Chapters had been downloaded previously, download also cover and create series.json - /// - /// Connector to use - /// Publication to check - /// Language to receive chapters for - /// - private static void DownloadNewChapters(Connector connector, Publication publication, string language, ref Dictionary> chapterCollection, TrangaSettings settings) - { - //Check if Publication already has a Folder - string publicationFolder = Path.Join(connector.downloadLocation, publication.folderName); - if(!Directory.Exists(publicationFolder)) - Directory.CreateDirectory(publicationFolder); - List newChapters = UpdateChapters(connector, publication, language, ref chapterCollection); - - connector.CopyCoverFromCacheToDownloadLocation(publication, settings); - - publication.SaveSeriesInfoJson(connector.downloadLocation); - - foreach(Chapter newChapter in newChapters) - connector.DownloadChapter(publication, newChapter); - } - - /// - /// Updates the available Chapters of a Publication - /// - /// Connector to use - /// Publication to check - /// Language to receive chapters for - /// - /// List of Chapters that were previously not in collection - private static List UpdateChapters(Connector connector, Publication publication, string language, ref Dictionary> chapterCollection) - { - List newChaptersList = new(); - chapterCollection.TryAdd(publication, newChaptersList); //To ensure publication is actually in collection - - Chapter[] newChapters = connector.GetChapters(publication, language); - newChaptersList = newChapters.Where(nChapter => !connector.CheckChapterIsDownloaded(publication, nChapter)).ToList(); - - return newChaptersList; - } -} \ No newline at end of file diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index c44451c..d2269e3 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -1,6 +1,7 @@ using Logging; using Newtonsoft.Json; using Tranga.Connectors; +using Tranga.TrangaTasks; namespace Tranga; @@ -116,9 +117,9 @@ public class TaskManager return; logger?.WriteLine(this.GetType().ToString(), $"Forcing Execution: {task}"); - Task t = new Task(() => + Task t = new(() => { - TaskExecutor.Execute(this, task, logger); + task.Execute(this); }); t.Start(); } @@ -137,37 +138,36 @@ public class TaskManager { logger?.WriteLine(this.GetType().ToString(), $"Adding new Task {task} {connectorName} {publication?.sortName}"); - TrangaTask newTask; + TrangaTask newTask = null; if (task == TrangaTask.Task.UpdateKomgaLibrary) { - newTask = new TrangaTask(task, null, null, reoccurrence); + newTask = new UpdateKomgaLibraryTask(task, reoccurrence); logger?.WriteLine(this.GetType().ToString(), $"Removing old {task}-Task."); //Only one UpdateKomgaLibrary Task _allTasks.RemoveWhere(trangaTask => trangaTask.task is TrangaTask.Task.UpdateKomgaLibrary); _allTasks.Add(newTask); - } - else + logger?.WriteLine(this.GetType().ToString(), $"Added new Task {newTask}"); + }else if (task == TrangaTask.Task.DownloadNewChapters) { - if(connectorName is null) - throw new ArgumentException($"connectorName can not be null for task {task}"); - //Get appropriate Connector from available Connectors for TrangaTask Connector? connector = _connectors.FirstOrDefault(c => c.name == connectorName); - if (connector is null) - throw new ArgumentException($"Connector {connectorName} is not a known connector."); - - newTask = new TrangaTask(task, connector.name, publication, reoccurrence, language); - - //Check if same task already exists - if (!_allTasks.Any(trangaTask => trangaTask.task == task && trangaTask.connectorName == connector.name && - trangaTask.publication?.internalId == publication?.internalId)) + if (connectorName is null) + throw new ArgumentException($"connectorName can not be null for task {task}"); + + if (publication is null) + throw new ArgumentException($"publication can not be null for task {task}"); + Publication pub = (Publication)publication; + newTask = new DownloadNewChaptersTask(task, connector!.name, pub, reoccurrence, language); + + if (!_allTasks.Any(trangaTask => + trangaTask.task == task && trangaTask.publication?.internalId == pub.internalId && + trangaTask.connectorName == connector.name)) { - if(task != TrangaTask.Task.UpdatePublications) - _chapterCollection.TryAdd((Publication)publication!, new List()); _allTasks.Add(newTask); + logger?.WriteLine(this.GetType().ToString(), $"Added new Task {newTask.ToString()}"); } else - logger?.WriteLine(this.GetType().ToString(), $"Publication already exists {publication?.internalId}"); + logger?.WriteLine(this.GetType().ToString(), $"Task already exists {newTask.ToString()}"); } ExportDataAndSettings(); diff --git a/Tranga/TrangaTask.cs b/Tranga/TrangaTask.cs index 7aec2d8..25515c1 100644 --- a/Tranga/TrangaTask.cs +++ b/Tranga/TrangaTask.cs @@ -5,7 +5,7 @@ namespace Tranga; /// /// Stores information on Task /// -public class TrangaTask +public abstract class TrangaTask { // ReSharper disable once CommentTypo ...Tell me why! // ReSharper disable once MemberCanBePrivate.Global I want it thaaat way @@ -14,7 +14,7 @@ public class TrangaTask public string? connectorName { get; } public Task task { get; } public Publication? publication { get; } - public string language { get; } + public string? language { get; } [JsonIgnore]public ExecutionState state { get; set; } public enum ExecutionState @@ -24,14 +24,8 @@ public class TrangaTask Running }; - public TrangaTask(Task task, string? connectorName, Publication? publication, TimeSpan reoccurrence, string language = "") + protected TrangaTask(Task task, string? connectorName, Publication? publication, TimeSpan reoccurrence, string? language = null) { - if(task != Task.UpdateKomgaLibrary && connectorName is null) - throw new ArgumentException($"connectorName can not be null for task {task}"); - - if (publication is null && task != Task.UpdatePublications && task != Task.UpdateKomgaLibrary) - throw new ArgumentException($"Publication can not be null for task {task}"); - this.publication = publication; this.reoccurrence = reoccurrence; this.lastExecuted = DateTime.Now.Subtract(reoccurrence); @@ -40,6 +34,8 @@ public class TrangaTask this.language = language; } + public abstract void Execute(TaskManager taskManager); + /// True if elapsed time since last execution is greater than set interval public bool ShouldExecute() { @@ -48,8 +44,6 @@ public class TrangaTask public enum Task { - UpdatePublications, - UpdateChapters, DownloadNewChapters, UpdateKomgaLibrary } diff --git a/Tranga/TrangaTasks/DownloadNewChaptersTask.cs b/Tranga/TrangaTasks/DownloadNewChaptersTask.cs new file mode 100644 index 0000000..1aabace --- /dev/null +++ b/Tranga/TrangaTasks/DownloadNewChaptersTask.cs @@ -0,0 +1,46 @@ +namespace Tranga.TrangaTasks; + +public class DownloadNewChaptersTask : TrangaTask +{ + public DownloadNewChaptersTask(Task task, string connectorName, Publication publication, TimeSpan reoccurrence, string language = "en") : base(task, connectorName, publication, reoccurrence, language) + { + } + + public override void Execute(TaskManager taskManager) + { + Publication pub = (Publication)this.publication!; + Connector connector = taskManager.GetConnector(this.connectorName); + + //Check if Publication already has a Folder + string publicationFolder = Path.Join(connector.downloadLocation, pub.folderName); + if(!Directory.Exists(publicationFolder)) + Directory.CreateDirectory(publicationFolder); + List newChapters = UpdateChapters(connector, pub, language!, ref taskManager._chapterCollection); + + connector.CopyCoverFromCacheToDownloadLocation(pub, taskManager.settings); + + pub.SaveSeriesInfoJson(connector.downloadLocation); + + foreach(Chapter newChapter in newChapters) + connector.DownloadChapter(pub, newChapter); + } + + /// + /// Updates the available Chapters of a Publication + /// + /// Connector to use + /// Publication to check + /// Language to receive chapters for + /// + /// List of Chapters that were previously not in collection + private static List UpdateChapters(Connector connector, Publication publication, string language, ref Dictionary> chapterCollection) + { + List newChaptersList = new(); + chapterCollection.TryAdd(publication, newChaptersList); //To ensure publication is actually in collection + + Chapter[] newChapters = connector.GetChapters(publication, language); + newChaptersList = newChapters.Where(nChapter => !connector.CheckChapterIsDownloaded(publication, nChapter)).ToList(); + + return newChaptersList; + } +} \ No newline at end of file diff --git a/Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs b/Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs new file mode 100644 index 0000000..0a605f4 --- /dev/null +++ b/Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs @@ -0,0 +1,19 @@ +namespace Tranga.TrangaTasks; + +public class UpdateKomgaLibraryTask : TrangaTask +{ + public UpdateKomgaLibraryTask(Task task, TimeSpan reoccurrence) : base(task, null, null, reoccurrence) + { + } + + public override void Execute(TaskManager taskManager) + { + if (taskManager.komga is null) + return; + Komga komga = taskManager.komga; + + Komga.KomgaLibrary[] allLibraries = komga.GetLibraries(); + foreach (Komga.KomgaLibrary lib in allLibraries) + komga.UpdateLibrary(lib.id); + } +} \ No newline at end of file