From 6de6d060c4510abb8960b13d73ef49c2b83f3c0b Mon Sep 17 00:00:00 2001 From: glax Date: Fri, 19 May 2023 14:00:30 +0200 Subject: [PATCH] Rewrite Task-Structure for serialization. TrangaTask include information on what to execute where, do not execute tasks. TaskExecutor executes Tasks on information from TrangaTask --- Tranga/TaskExecutor.cs | 48 ++++++++++++++++++++++++++ Tranga/TaskManager.cs | 11 ++---- Tranga/TrangaTask.cs | 76 ++++++++++++++---------------------------- 3 files changed, 76 insertions(+), 59 deletions(-) create mode 100644 Tranga/TaskExecutor.cs diff --git a/Tranga/TaskExecutor.cs b/Tranga/TaskExecutor.cs new file mode 100644 index 0000000..83fb68d --- /dev/null +++ b/Tranga/TaskExecutor.cs @@ -0,0 +1,48 @@ +namespace Tranga; + +public static class TaskExecutor +{ + public static void Execute(TrangaTask trangaTask, Dictionary> chapterCollection) + { + switch (trangaTask.task) + { + case TrangaTask.Task.DownloadNewChapters: + DownloadNewChapters(trangaTask.connector, trangaTask.publication, trangaTask.language, chapterCollection); + break; + case TrangaTask.Task.UpdateChapters: + UpdateChapters(trangaTask.connector, trangaTask.publication, trangaTask.language, chapterCollection); + break; + case TrangaTask.Task.UpdatePublications: + UpdatePublications(trangaTask.connector, chapterCollection); + break; + } + } + + private static void UpdatePublications(Connector connector, Dictionary> chapterCollection) + { + Publication[] publications = connector.GetPublications(); + foreach (Publication publication in publications) + chapterCollection.TryAdd(publication, new List()); + } + + private static void DownloadNewChapters(Connector connector, Publication publication, string language, Dictionary> chapterCollection) + { + List newChapters = UpdateChapters(connector, publication, language, chapterCollection); + foreach(Chapter newChapter in newChapters) + connector.DownloadChapter(publication, newChapter); + } + + private static List UpdateChapters(Connector connector, Publication publication, string language, Dictionary> chapterCollection) + { + List newChaptersList = new(); + if (!chapterCollection.ContainsKey(publication)) + return newChaptersList; + + List currentChapters = chapterCollection[publication]; + Chapter[] newChapters = connector.GetChapters(publication, language); + + newChaptersList = newChapters.ToList() + .ExceptBy(currentChapters.Select(cChapter => cChapter.url), nChapter => nChapter.url).ToList(); + return newChaptersList; + } +} \ No newline at end of file diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index b028dd7..ce79fbb 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -2,7 +2,7 @@ public class TaskManager { - private readonly Dictionary _chapterCollection; + private readonly Dictionary> _chapterCollection; private readonly HashSet _allTasks; private bool _continueRunning = true; @@ -19,14 +19,9 @@ public class TaskManager { while (_continueRunning) { - foreach (TrangaTask task in _allTasks.Where(trangaTask => (DateTime.Now - trangaTask.lastExecuted) > trangaTask.reoccurrence)) + foreach (TrangaTask task in _allTasks.Where(trangaTask => trangaTask.ShouldExecute(true))) { - if (!task.lastExecutedSuccessfully) - { - task.Abort(); - //Add logging that task has failed - } - task.Execute(); + TaskExecutor.Execute(task, this._chapterCollection); } Thread.Sleep(1000); } diff --git a/Tranga/TrangaTask.cs b/Tranga/TrangaTask.cs index 4aac005..00c1ea9 100644 --- a/Tranga/TrangaTask.cs +++ b/Tranga/TrangaTask.cs @@ -1,64 +1,38 @@ -namespace Tranga; +using System.Text.Json.Serialization; -public class TrangaTask +namespace Tranga; + +public struct TrangaTask { - private readonly Action _taskAction; - private Task? _task; - public bool lastExecutedSuccessfully => _task is not null && _task.IsCompleted; - public TimeSpan reoccurrence { get; } - public DateTime lastExecuted { get; private set; } + [JsonInclude]public TimeSpan reoccurrence { get; } + [JsonInclude]public DateTime lastExecuted { get; set; } + [JsonInclude]public Connector connector { get; } + [JsonInclude]public Task task { get; } + [JsonInclude]public Publication publication { get; } + [JsonInclude]public string language { get; } - public TrangaTask(Action taskAction, TimeSpan reoccurrence) + public TrangaTask(Connector connector, Task task, Publication publication, TimeSpan reoccurrence, string language = "") { - this._taskAction = taskAction; this.reoccurrence = reoccurrence; + this.lastExecuted = DateTime.Now.Subtract(reoccurrence); + this.connector = connector; + this.task = task; + this.publication = publication; + this.language = language; } - public void Abort() + public bool ShouldExecute(bool willBeExecuted) { - if(_task is not null && !_task.IsCompleted) - _task.Dispose(); + bool ret = (DateTime.Now - lastExecuted) > reoccurrence; + if (ret && willBeExecuted) + lastExecuted = DateTime.Now; + return ret; } - public void Execute() + public enum Task { - lastExecuted = DateTime.Now; - _task = new (_taskAction); - _task.Start(); - } - - public static TrangaTask CreateDownloadChapterTask(Connector connector, Publication publication, Chapter chapter, TimeSpan reoccurrence) - { - void TaskAction() - { - connector.DownloadChapter(publication, chapter); - } - return new TrangaTask(TaskAction, reoccurrence); - } - - public static TrangaTask CreateUpdateChaptersTask(ref Dictionary chapterCollection, Connector connector, Publication publication, string language, TimeSpan reoccurrence) - { - Dictionary pChapterCollection = chapterCollection; - - void TaskAction() - { - Chapter[] chapters = connector.GetChapters(publication, language); - if(pChapterCollection.TryAdd(publication, chapters)) - pChapterCollection[publication] = chapters; - } - return new TrangaTask(TaskAction, reoccurrence); - } - - public static TrangaTask CreateUpdatePublicationsTask(ref Dictionary chapterCollection, Connector connector, TimeSpan reoccurrence) - { - Dictionary pChapterCollection = chapterCollection; - - void TaskAction() - { - Publication[] publications = connector.GetPublications(); - foreach (Publication publication in publications) - pChapterCollection.TryAdd(publication, Array.Empty()); - } - return new TrangaTask(TaskAction, reoccurrence); + UpdatePublications, + UpdateChapters, + DownloadNewChapters } } \ No newline at end of file