From 4b0a1c0a9d5ba83c44d786e1384a97c713473396 Mon Sep 17 00:00:00 2001 From: glax Date: Wed, 17 May 2023 23:23:01 +0200 Subject: [PATCH] Added Structs Chapter and Publication Added TaskManager and TrangaTask TaskManager manages all TrangaTasks and starts Tasks when necessary. Execution is necessary when time elapsed between last execution and now is greater than TrangaTask.reoccurrence. --- Chapter.cs | 22 +++++++++++++++++++++ Class1.cs | 5 ----- Connector.cs | 9 +++++++++ Publication.cs | 20 +++++++++++++++++++ TaskManager.cs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ TrangaTask.cs | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 Chapter.cs delete mode 100644 Class1.cs create mode 100644 Connector.cs create mode 100644 Publication.cs create mode 100644 TaskManager.cs create mode 100644 TrangaTask.cs diff --git a/Chapter.cs b/Chapter.cs new file mode 100644 index 0000000..5bfcf34 --- /dev/null +++ b/Chapter.cs @@ -0,0 +1,22 @@ +namespace Tranga; + +public struct Chapter +{ + public Publication publication { get; } + public string name { get; } + public uint volumeNumber { get; } + public uint chapterNumber { get; } + public string summary { get; } + public string posterUrl { get; }//Better way? + + public Chapter(Publication publication, string name, uint volumeNumber, uint chapterNumber, string summary, + string posterUrl) + { + this.publication = publication; + this.name = name; + this.volumeNumber = volumeNumber; + this.chapterNumber = chapterNumber; + this.summary = summary; + this.posterUrl = posterUrl; + } +} \ No newline at end of file diff --git a/Class1.cs b/Class1.cs deleted file mode 100644 index c4c30b0..0000000 --- a/Class1.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace Tranga; - -public class Class1 -{ -} \ No newline at end of file diff --git a/Connector.cs b/Connector.cs new file mode 100644 index 0000000..a4655bf --- /dev/null +++ b/Connector.cs @@ -0,0 +1,9 @@ +namespace Tranga; + +public abstract class Connector +{ + public abstract string name { get; } + public abstract bool GetPublications(out Publication[] publications); + public abstract bool GetChapters(Publication publication, out Chapter[] chapters); + public abstract bool DownloadChapter(Chapter chapter); //where to? +} \ No newline at end of file diff --git a/Publication.cs b/Publication.cs new file mode 100644 index 0000000..6b99107 --- /dev/null +++ b/Publication.cs @@ -0,0 +1,20 @@ +namespace Tranga; + +public struct Publication +{ + public string sortName { get; } + public string[] titles { get; } + public string description { get; } + public string[] tags { get; } + public string posterUrl { get; } //maybe there is a better way? + + + public Publication(string sortName, string description, string[] titles, string[] tags, string posterUrl) + { + this.sortName = sortName; + this.description = description; + this.titles = titles; + this.tags = tags; + this.posterUrl = posterUrl; + } +} \ No newline at end of file diff --git a/TaskManager.cs b/TaskManager.cs new file mode 100644 index 0000000..5ef3569 --- /dev/null +++ b/TaskManager.cs @@ -0,0 +1,52 @@ +namespace Tranga; + +public class TaskManager +{ + private readonly Dictionary _allTasks; + private bool _continueRunning = true; + + public TaskManager() + { + _allTasks = new Dictionary(); + Thread taskChecker = new(TaskCheckerThread); + taskChecker.Start(); + } + + private void TaskCheckerThread() + { + while (_continueRunning) + { + foreach (TrangaTask task in _allTasks.Values.Where(tt => (DateTime.Now - tt.lastExecuted) > tt.reoccurrence)) + { + if (!task.lastExecutedSuccessfully) + { + task.Abort(); + //Add logging that task has failed + } + task.Execute(); + } + Thread.Sleep(1000); + } + } + + public bool PublicationAlreadyAdded(Publication publication) + { + return false; + //TODO fuzzy check publications + } + + public Publication[] GetAddedPublications() + { + return _allTasks.Keys.ToArray(); + } + + public TrangaTask[] GetTasks() + { + return _allTasks.Values.ToArray(); + } + + public void Shutdown() + { + _continueRunning = false; + } +} \ No newline at end of file diff --git a/TrangaTask.cs b/TrangaTask.cs new file mode 100644 index 0000000..96ce9fd --- /dev/null +++ b/TrangaTask.cs @@ -0,0 +1,49 @@ +namespace Tranga; + +public class 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; } + + public TrangaTask(Action taskAction, TimeSpan reoccurrence) + { + this._taskAction = taskAction; + this.reoccurrence = reoccurrence; + } + + 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, Chapter chapter, TimeSpan reoccurrence) + { + void TaskAction() + { + connector.DownloadChapter(chapter); + } + return new TrangaTask(TaskAction, reoccurrence); + } + + + public static TrangaTask CreateUpdateChaptersTask(Connector connector, Publication publication, TimeSpan reoccurrence) + { + throw new NotImplementedException(); + } + + public static TrangaTask CreateUpdatePublicationsTask(Connector connector, TimeSpan reoccurrence) + { + throw new NotImplementedException(); + } +} \ No newline at end of file