diff --git a/Tranga/Jobs/DownloadNewChapters.cs b/Tranga/Jobs/DownloadNewChapters.cs index bfe2ddc..e08dc5f 100644 --- a/Tranga/Jobs/DownloadNewChapters.cs +++ b/Tranga/Jobs/DownloadNewChapters.cs @@ -6,6 +6,13 @@ namespace Tranga.Jobs; 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) + { + this.manga = manga; + } public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Manga manga, bool recurring = false, TimeSpan? recurrence = null) : base (clone, connector, recurring, recurrence) { diff --git a/Tranga/Jobs/Job.cs b/Tranga/Jobs/Job.cs index 010efb6..612b818 100644 --- a/Tranga/Jobs/Job.cs +++ b/Tranga/Jobs/Job.cs @@ -13,7 +13,7 @@ public abstract class Job : GlobalBase public string id => GetId(); internal IEnumerable? subJobs { get; private set; } - public 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) : base(clone) { this.mangaConnector = connector; this.progressToken = new ProgressToken(0); @@ -25,6 +25,19 @@ public abstract class Job : GlobalBase this.recurrenceTime = recurrenceTime; } + internal Job(GlobalBase clone, MangaConnector connector, DateTime lastExecution, bool recurring = false, + TimeSpan? recurrenceTime = null) : base(clone) + { + this.mangaConnector = connector; + this.progressToken = new ProgressToken(0); + 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.lastExecution = lastExecution; + this.recurrenceTime = recurrenceTime; + + } + protected abstract string GetId(); public Job(GlobalBase clone, MangaConnector connector, ProgressToken progressToken, bool recurring = false, TimeSpan? recurrenceTime = null) : base(clone) diff --git a/Tranga/Jobs/JobBoss.cs b/Tranga/Jobs/JobBoss.cs index 4fa4cb5..a6693e9 100644 --- a/Tranga/Jobs/JobBoss.cs +++ b/Tranga/Jobs/JobBoss.cs @@ -29,9 +29,7 @@ public class JobBoss : GlobalBase { Log($"Added {job}"); this.jobs.Add(job); - while(IsFileInUse(settings.jobsFilePath)) - Thread.Sleep(10); - File.WriteAllText(settings.jobsFilePath, JsonConvert.SerializeObject(this.jobs)); + ExportJobsList(); } } @@ -55,9 +53,7 @@ public class JobBoss : GlobalBase this.jobs.Remove(job); if(job.subJobs is not null) RemoveJobs(job.subJobs); - while(IsFileInUse(settings.jobsFilePath)) - Thread.Sleep(10); - File.WriteAllText(settings.jobsFilePath, JsonConvert.SerializeObject(this.jobs)); + ExportJobsList(); } public void RemoveJobs(IEnumerable jobsToRemove) @@ -139,6 +135,14 @@ public class JobBoss : GlobalBase AddJobToQueue(job); } + public void ExportJobsList() + { + Log($"Exporting {settings.jobsFilePath}"); + while(IsFileInUse(settings.jobsFilePath)) + Thread.Sleep(10); + File.WriteAllText(settings.jobsFilePath, JsonConvert.SerializeObject(this.jobs)); + } + public void CheckJobs() { foreach (Job job in jobs.Where(job => job.nextExecution < DateTime.Now && !QueueContainsJob(job)).OrderBy(job => job.nextExecution)) @@ -153,8 +157,10 @@ public class JobBoss : GlobalBase if(queueHead.recurring) queueHead.ResetProgress(); jobQueue.Dequeue(); - }else if(queueHead.progressToken.state is ProgressToken.State.Standby) + }else if (queueHead.progressToken.state is ProgressToken.State.Standby) + { AddJobsToQueue(jobQueue.Peek().ExecuteReturnSubTasks()); + } else if (queueHead.progressToken.state is ProgressToken.State.Cancelled) { switch (queueHead) diff --git a/Tranga/Jobs/JobJsonConverter.cs b/Tranga/Jobs/JobJsonConverter.cs index e49cfdd..ea8a03a 100644 --- a/Tranga/Jobs/JobJsonConverter.cs +++ b/Tranga/Jobs/JobJsonConverter.cs @@ -34,6 +34,7 @@ public class JobJsonConverter : JsonConverter } }))!, jo.GetValue("manga")!.ToObject(), + jo.GetValue("lastExecution")!.ToObject(), jo.GetValue("recurring")!.Value(), jo.GetValue("recurrenceTime")!.ToObject()); }