Added parentJobId for deserialization

When creating Jobs with null as recurrence time, set it to zero
Job.NextExecution() removed the recurrence check
This commit is contained in:
glax 2023-09-02 16:11:56 +02:00
parent 79bbc92467
commit 51a1ae72ca
5 changed files with 34 additions and 34 deletions

View File

@ -7,7 +7,12 @@ public class DownloadChapter : Job
{ {
public Chapter chapter { get; init; } public Chapter chapter { get; init; }
public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter) : base(clone, connector) 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, string? parentJobId = null) : base(clone, connector, parentJobId: parentJobId)
{ {
this.chapter = chapter; this.chapter = chapter;
} }

View File

@ -8,13 +8,13 @@ public class DownloadNewChapters : Job
public Manga manga { get; init; } public Manga manga { get; init; }
public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Manga manga, DateTime lastExecution, public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Manga manga, DateTime lastExecution,
bool recurring = false, TimeSpan? recurrence = null) : base(clone, connector, lastExecution, recurring, bool recurring = false, TimeSpan? recurrence = null, string? parentJobId = null) : base(clone, connector, lastExecution, recurring,
recurrence) recurrence, parentJobId)
{ {
this.manga = manga; 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; this.manga = manga;
} }
@ -33,13 +33,13 @@ public class DownloadNewChapters : Job
{ {
Chapter[] chapters = mangaConnector.GetNewChapters(manga); Chapter[] chapters = mangaConnector.GetNewChapters(manga);
this.progressToken.increments = chapters.Length; this.progressToken.increments = chapters.Length;
List<Job> subJobs = new(); List<Job> jobs = new();
foreach (Chapter chapter in chapters) foreach (Chapter chapter in chapters)
{ {
DownloadChapter downloadChapterJob = new(this, this.mangaConnector, chapter); DownloadChapter downloadChapterJob = new(this, this.mangaConnector, chapter, parentJobId: this.id);
subJobs.Add(downloadChapterJob); jobs.Add(downloadChapterJob);
} }
progressToken.Complete(); progressToken.Complete();
return subJobs; return jobs;
} }
} }

View File

@ -12,8 +12,9 @@ public abstract class Job : GlobalBase
public DateTime nextExecution => NextExecution(); public DateTime nextExecution => NextExecution();
public string id => GetId(); public string id => GetId();
internal IEnumerable<Job>? subJobs { get; private set; } internal IEnumerable<Job>? 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.mangaConnector = connector;
this.progressToken = new ProgressToken(0); 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."); throw new ArgumentException("If recurrence is set to true, a recurrence time has to be provided.");
else if(recurring && recurrenceTime is not null) else if(recurring && recurrenceTime is not null)
this.lastExecution = DateTime.Now.Subtract((TimeSpan)recurrenceTime); 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, 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.mangaConnector = connector;
this.progressToken = new ProgressToken(0); this.progressToken = new ProgressToken(0);
@ -34,37 +36,23 @@ public abstract class Job : GlobalBase
if (recurring && recurrenceTime is null) if (recurring && recurrenceTime is null)
throw new ArgumentException("If recurrence is set to true, a recurrence time has to be provided."); throw new ArgumentException("If recurrence is set to true, a recurrence time has to be provided.");
this.lastExecution = lastExecution; this.lastExecution = lastExecution;
this.recurrenceTime = recurrenceTime; this.recurrenceTime = recurrenceTime ?? TimeSpan.Zero;
this.parentJobId = parentJobId;
} }
protected abstract string GetId(); 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; subJobs ??= new List<Job>();
this.progressToken = progressToken; subJobs = subJobs.Append(job);
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;
} }
private DateTime NextExecution() private DateTime NextExecution()
{ {
if(recurring && recurrenceTime.HasValue && lastExecution.HasValue) if(recurrenceTime.HasValue && lastExecution.HasValue)
return lastExecution.Value.Add(recurrenceTime.Value); return lastExecution.Value.Add(recurrenceTime.Value);
if(recurring && recurrenceTime.HasValue && !lastExecution.HasValue) if(recurrenceTime.HasValue && !lastExecution.HasValue)
return DateTime.Now; return DateTime.Now;
return DateTime.MaxValue; return DateTime.MaxValue;
} }

View File

@ -11,7 +11,11 @@ public class JobBoss : GlobalBase
public JobBoss(GlobalBase clone, HashSet<MangaConnector> connectors) : base(clone) public JobBoss(GlobalBase clone, HashSet<MangaConnector> connectors) : base(clone)
{ {
if (File.Exists(settings.jobsFilePath)) if (File.Exists(settings.jobsFilePath))
{
this.jobs = JsonConvert.DeserializeObject<HashSet<Job>>(File.ReadAllText(settings.jobsFilePath), new JobJsonConverter(this, new MangaConnectorJsonConverter(this, connectors)))!; this.jobs = JsonConvert.DeserializeObject<HashSet<Job>>(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 else
this.jobs = new(); this.jobs = new();
foreach (DownloadNewChapters ncJob in this.jobs.Where(job => job is DownloadNewChapters)) foreach (DownloadNewChapters ncJob in this.jobs.Where(job => job is DownloadNewChapters))

View File

@ -36,7 +36,8 @@ public class JobJsonConverter : JsonConverter
jo.GetValue("manga")!.ToObject<Manga>(), jo.GetValue("manga")!.ToObject<Manga>(),
jo.GetValue("lastExecution")!.ToObject<DateTime>(), jo.GetValue("lastExecution")!.ToObject<DateTime>(),
jo.GetValue("recurring")!.Value<bool>(), jo.GetValue("recurring")!.Value<bool>(),
jo.GetValue("recurrenceTime")!.ToObject<TimeSpan?>()); jo.GetValue("recurrenceTime")!.ToObject<TimeSpan?>(),
jo.GetValue("parentJobId")!.Value<string?>());
} }
if (jo.ContainsKey("chapter"))//DownloadChapter if (jo.ContainsKey("chapter"))//DownloadChapter
@ -49,7 +50,9 @@ public class JobJsonConverter : JsonConverter
this._mangaConnectorJsonConverter this._mangaConnectorJsonConverter
} }
}))!, }))!,
jo.GetValue("chapter")!.ToObject<Chapter>()); jo.GetValue("chapter")!.ToObject<Chapter>(),
DateTime.UnixEpoch,
jo.GetValue("parentJobId")!.Value<string?>());
} }
throw new Exception(); throw new Exception();