From 86bb49508a17878e9f293396a26ad2011c27c428 Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 18 May 2023 21:08:09 +0200 Subject: [PATCH] Revert serialization attempt --- Tranga/Publication.cs | 10 ++-- Tranga/TaskManager.cs | 16 +++--- Tranga/TrangaTask.cs | 112 +++++++++++++++++------------------------- 3 files changed, 59 insertions(+), 79 deletions(-) diff --git a/Tranga/Publication.cs b/Tranga/Publication.cs index 74580d5..1b16650 100644 --- a/Tranga/Publication.cs +++ b/Tranga/Publication.cs @@ -42,17 +42,17 @@ public struct Publication internal struct SeriesInfo { - [JsonInclude]public Metadata metadata { get; } + [JsonRequired]public Metadata metadata { get; } public SeriesInfo(Metadata metadata) => this.metadata = metadata; } internal struct Metadata { - [JsonInclude]public string name { get; } - [JsonInclude]public string year { get; } - [JsonInclude]public string status { get; } + [JsonRequired]public string name { get; } + [JsonRequired]public string year { get; } + [JsonRequired]public string status { get; } // ReSharper disable twice InconsistentNaming - [JsonInclude]public string description_text { get; } + [JsonRequired]public string description_text { get; } public Metadata(string name, string year, string status, string description_text) { diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index eae4af6..90a9b9e 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -2,7 +2,7 @@ public class TaskManager { - private Dictionary _chapterCollection; + private readonly Dictionary _chapterCollection; private readonly HashSet _allTasks; private bool _continueRunning = true; @@ -14,18 +14,18 @@ public class TaskManager taskChecker.Start(); } - public void AddTask(Connector connector, TrangaTask.AvailableTasks task, TimeSpan reoccurrence, Publication? publication = null, string language = "en") - { - this._allTasks.Add(new TrangaTask(connector, task, reoccurrence, publication, language)); - } - private void TaskCheckerThread() { while (_continueRunning) { foreach (TrangaTask task in _allTasks.Where(trangaTask => (DateTime.Now - trangaTask.lastExecuted) > trangaTask.reoccurrence)) { - task.Execute(ref _chapterCollection); + if (!task.lastExecutedSuccessfully) + { + task.Abort(); + //Add logging that task has failed + } + task.Execute(); } Thread.Sleep(1000); } @@ -39,7 +39,7 @@ public class TaskManager public Publication[] GetAddedPublications() { - return this._chapterCollection.Keys.ToArray(); + throw new NotImplementedException(); } public TrangaTask[] GetTasks() diff --git a/Tranga/TrangaTask.cs b/Tranga/TrangaTask.cs index e5b82e0..4aac005 100644 --- a/Tranga/TrangaTask.cs +++ b/Tranga/TrangaTask.cs @@ -1,84 +1,64 @@ -using System.Text.Json.Serialization; - -namespace Tranga; +namespace Tranga; public class TrangaTask { - [JsonInclude]public TimeSpan reoccurrence { get; } - [JsonInclude]public DateTime lastExecuted { get; private set; } - [JsonIgnore] private Connector connector { get; } - [JsonInclude] public string connectorName; - [JsonInclude]public AvailableTasks task { get; } - public enum AvailableTasks - { - DownloadNewChapters, - UpdateChapters, - UpdatePublications - }; - [JsonIgnore]public Publication? publication { get; } - [JsonInclude]public string? publicationIdentifier; - [JsonInclude]public string language { get; } + 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; } - - public TrangaTask(Connector connector, AvailableTasks task, TimeSpan reoccurrence, Publication? publication = null, string language = "en") + public TrangaTask(Action taskAction, TimeSpan reoccurrence) { - this.connector = connector; - this.connectorName = connector.name; - this.task = task; - this.lastExecuted = DateTime.Now.Subtract(reoccurrence); + this._taskAction = taskAction; this.reoccurrence = reoccurrence; - this.publication = publication; - this.publicationIdentifier = publication?.downloadUrl; - this.language = language; - if (publication is null && task is AvailableTasks.UpdateChapters or AvailableTasks.DownloadNewChapters) + } + + public void Abort() + { + if(_task is not null && !_task.IsCompleted) + _task.Dispose(); + } + + public void Execute() + { + lastExecuted = DateTime.Now; + _task = new (_taskAction); + _task.Start(); + } + + public static TrangaTask CreateDownloadChapterTask(Connector connector, Publication publication, Chapter chapter, TimeSpan reoccurrence) + { + void TaskAction() { - if (publication is null) - throw new ArgumentException( - "If task is updateChapters or downloadNewChapters, Argument publication can not be null!"); + connector.DownloadChapter(publication, chapter); } + return new TrangaTask(TaskAction, reoccurrence); } - - public void Execute(ref Dictionary chapterCollection) + + public static TrangaTask CreateUpdateChaptersTask(ref Dictionary chapterCollection, Connector connector, Publication publication, string language, TimeSpan reoccurrence) { - switch (this.task) + Dictionary pChapterCollection = chapterCollection; + + void TaskAction() { - case AvailableTasks.UpdateChapters: - UpdateChapters(ref chapterCollection); - break; - case AvailableTasks.UpdatePublications: - UpdatePublications(ref chapterCollection); - break; - case AvailableTasks.DownloadNewChapters: - DownloadNewChapters(UpdateChapters(ref chapterCollection)); - break; + Chapter[] chapters = connector.GetChapters(publication, language); + if(pChapterCollection.TryAdd(publication, chapters)) + pChapterCollection[publication] = chapters; } - - this.lastExecuted = DateTime.Now; + return new TrangaTask(TaskAction, reoccurrence); } - private Chapter[] UpdateChapters(ref Dictionary chapterCollection) + public static TrangaTask CreateUpdatePublicationsTask(ref Dictionary chapterCollection, Connector connector, TimeSpan reoccurrence) { - Publication pPublication = (Publication)this.publication!; - Chapter[] presentChapters = chapterCollection[pPublication]; - Chapter[] allChapters = connector.GetChapters(pPublication); - chapterCollection[pPublication] = allChapters; + Dictionary pChapterCollection = chapterCollection; - Dictionary pChapter = presentChapters.ToDictionary(chapter => chapter.url, chapter => chapter); - Dictionary aChapter = allChapters.ToDictionary(chapter => chapter.url, chapter => chapter); - return aChapter.Except(pChapter).ToDictionary(pair => pair.Key, pair => pair.Value).Values.ToArray(); - } - - private void UpdatePublications(ref Dictionary chapterCollection) - { - Publication[] allPublications = connector.GetPublications(); - foreach(Publication publication in allPublications) - chapterCollection.TryAdd(publication, Array.Empty()); - } - - private void DownloadNewChapters(Chapter[] newChapters) - { - Publication pPublication = (Publication)this.publication!; - foreach(Chapter chapter in newChapters) - connector.DownloadChapter(pPublication, chapter); + void TaskAction() + { + Publication[] publications = connector.GetPublications(); + foreach (Publication publication in publications) + pChapterCollection.TryAdd(publication, Array.Empty()); + } + return new TrangaTask(TaskAction, reoccurrence); } } \ No newline at end of file