From 51a1ae72ca1e44982bfea178c880cef23c20415b Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 2 Sep 2023 16:11:56 +0200 Subject: [PATCH] Added parentJobId for deserialization When creating Jobs with null as recurrence time, set it to zero Job.NextExecution() removed the recurrence check --- Tranga/Jobs/DownloadChapter.cs | 7 +++++- Tranga/Jobs/DownloadNewChapters.cs | 14 ++++++------ Tranga/Jobs/Job.cs | 36 ++++++++++-------------------- Tranga/Jobs/JobBoss.cs | 4 ++++ Tranga/Jobs/JobJsonConverter.cs | 7 ++++-- 5 files changed, 34 insertions(+), 34 deletions(-) diff --git a/Tranga/Jobs/DownloadChapter.cs b/Tranga/Jobs/DownloadChapter.cs index 9cfb23f..357e7ba 100644 --- a/Tranga/Jobs/DownloadChapter.cs +++ b/Tranga/Jobs/DownloadChapter.cs @@ -6,8 +6,13 @@ namespace Tranga.Jobs; public class DownloadChapter : Job { public Chapter chapter { get; init; } + + public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter, DateTime lastExecution, string? parentJobId = null) : base(clone, connector, lastExecution, parentJobId: parentJobId) + { + this.chapter = chapter; + } - public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter) : base(clone, connector) + public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter, string? parentJobId = null) : base(clone, connector, parentJobId: parentJobId) { this.chapter = chapter; } diff --git a/Tranga/Jobs/DownloadNewChapters.cs b/Tranga/Jobs/DownloadNewChapters.cs index e08dc5f..b41746d 100644 --- a/Tranga/Jobs/DownloadNewChapters.cs +++ b/Tranga/Jobs/DownloadNewChapters.cs @@ -8,13 +8,13 @@ public class DownloadNewChapters : Job public Manga manga { get; init; } public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Manga manga, DateTime lastExecution, - bool recurring = false, TimeSpan? recurrence = null) : base(clone, connector, lastExecution, recurring, - recurrence) + bool recurring = false, TimeSpan? recurrence = null, string? parentJobId = null) : base(clone, connector, lastExecution, recurring, + recurrence, parentJobId) { this.manga = manga; } - public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Manga manga, bool recurring = false, TimeSpan? recurrence = null) : base (clone, connector, recurring, recurrence) + public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Manga manga, bool recurring = false, TimeSpan? recurrence = null, string? parentJobId = null) : base (clone, connector, recurring, recurrence, parentJobId) { this.manga = manga; } @@ -33,13 +33,13 @@ public class DownloadNewChapters : Job { Chapter[] chapters = mangaConnector.GetNewChapters(manga); this.progressToken.increments = chapters.Length; - List subJobs = new(); + List jobs = new(); foreach (Chapter chapter in chapters) { - DownloadChapter downloadChapterJob = new(this, this.mangaConnector, chapter); - subJobs.Add(downloadChapterJob); + DownloadChapter downloadChapterJob = new(this, this.mangaConnector, chapter, parentJobId: this.id); + jobs.Add(downloadChapterJob); } progressToken.Complete(); - return subJobs; + return jobs; } } \ No newline at end of file diff --git a/Tranga/Jobs/Job.cs b/Tranga/Jobs/Job.cs index 612b818..deb598e 100644 --- a/Tranga/Jobs/Job.cs +++ b/Tranga/Jobs/Job.cs @@ -12,8 +12,9 @@ public abstract class Job : GlobalBase public DateTime nextExecution => NextExecution(); public string id => GetId(); internal IEnumerable? subJobs { get; private set; } + public string? parentJobId { get; init; } - internal Job(GlobalBase clone, MangaConnector connector, bool recurring = false, TimeSpan? recurrenceTime = null) : base(clone) + internal Job(GlobalBase clone, MangaConnector connector, bool recurring = false, TimeSpan? recurrenceTime = null, string? parentJobId = null) : base(clone) { this.mangaConnector = connector; this.progressToken = new ProgressToken(0); @@ -22,11 +23,12 @@ public abstract class Job : GlobalBase throw new ArgumentException("If recurrence is set to true, a recurrence time has to be provided."); else if(recurring && recurrenceTime is not null) this.lastExecution = DateTime.Now.Subtract((TimeSpan)recurrenceTime); - this.recurrenceTime = recurrenceTime; + this.recurrenceTime = recurrenceTime ?? TimeSpan.Zero; + this.parentJobId = parentJobId; } internal Job(GlobalBase clone, MangaConnector connector, DateTime lastExecution, bool recurring = false, - TimeSpan? recurrenceTime = null) : base(clone) + TimeSpan? recurrenceTime = null, string? parentJobId = null) : base(clone) { this.mangaConnector = connector; this.progressToken = new ProgressToken(0); @@ -34,37 +36,23 @@ public abstract class Job : GlobalBase if (recurring && recurrenceTime is null) throw new ArgumentException("If recurrence is set to true, a recurrence time has to be provided."); this.lastExecution = lastExecution; - this.recurrenceTime = recurrenceTime; - + this.recurrenceTime = recurrenceTime ?? TimeSpan.Zero; + this.parentJobId = parentJobId; } protected abstract string GetId(); - public Job(GlobalBase clone, MangaConnector connector, ProgressToken progressToken, bool recurring = false, TimeSpan? recurrenceTime = null) : base(clone) + public void AddSubJob(Job job) { - this.mangaConnector = connector; - this.progressToken = progressToken; - this.recurring = recurring; - if (recurring && recurrenceTime is null) - throw new ArgumentException("If recurrence is set to true, a recurrence time has to be provided."); - this.recurrenceTime = recurrenceTime; - } - - public Job(GlobalBase clone, MangaConnector connector, int taskIncrements, bool recurring = false, TimeSpan? recurrenceTime = null) : base(clone) - { - this.mangaConnector = connector; - this.progressToken = new ProgressToken(taskIncrements); - this.recurring = recurring; - if (recurring && recurrenceTime is null) - throw new ArgumentException("If recurrence is set to true, a recurrence time has to be provided."); - this.recurrenceTime = recurrenceTime; + subJobs ??= new List(); + subJobs = subJobs.Append(job); } private DateTime NextExecution() { - if(recurring && recurrenceTime.HasValue && lastExecution.HasValue) + if(recurrenceTime.HasValue && lastExecution.HasValue) return lastExecution.Value.Add(recurrenceTime.Value); - if(recurring && recurrenceTime.HasValue && !lastExecution.HasValue) + if(recurrenceTime.HasValue && !lastExecution.HasValue) return DateTime.Now; return DateTime.MaxValue; } diff --git a/Tranga/Jobs/JobBoss.cs b/Tranga/Jobs/JobBoss.cs index a6693e9..d81d2df 100644 --- a/Tranga/Jobs/JobBoss.cs +++ b/Tranga/Jobs/JobBoss.cs @@ -11,7 +11,11 @@ public class JobBoss : GlobalBase public JobBoss(GlobalBase clone, HashSet connectors) : base(clone) { if (File.Exists(settings.jobsFilePath)) + { this.jobs = JsonConvert.DeserializeObject>(File.ReadAllText(settings.jobsFilePath), new JobJsonConverter(this, new MangaConnectorJsonConverter(this, connectors)))!; + foreach (Job job in this.jobs) + this.jobs.FirstOrDefault(jjob => jjob.id == job.parentJobId)?.AddSubJob(job); + } else this.jobs = new(); foreach (DownloadNewChapters ncJob in this.jobs.Where(job => job is DownloadNewChapters)) diff --git a/Tranga/Jobs/JobJsonConverter.cs b/Tranga/Jobs/JobJsonConverter.cs index ea8a03a..794075e 100644 --- a/Tranga/Jobs/JobJsonConverter.cs +++ b/Tranga/Jobs/JobJsonConverter.cs @@ -36,7 +36,8 @@ public class JobJsonConverter : JsonConverter jo.GetValue("manga")!.ToObject(), jo.GetValue("lastExecution")!.ToObject(), jo.GetValue("recurring")!.Value(), - jo.GetValue("recurrenceTime")!.ToObject()); + jo.GetValue("recurrenceTime")!.ToObject(), + jo.GetValue("parentJobId")!.Value()); } if (jo.ContainsKey("chapter"))//DownloadChapter @@ -49,7 +50,9 @@ public class JobJsonConverter : JsonConverter this._mangaConnectorJsonConverter } }))!, - jo.GetValue("chapter")!.ToObject()); + jo.GetValue("chapter")!.ToObject(), + DateTime.UnixEpoch, + jo.GetValue("parentJobId")!.Value()); } throw new Exception();