From a1a5028858053ee201812a278c702c640c9f0236 Mon Sep 17 00:00:00 2001 From: Glax Date: Fri, 16 May 2025 19:18:07 +0200 Subject: [PATCH] Ordering of DownloadChapterJobs (start at first chapter and work up) --- API/Tranga.cs | 55 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/API/Tranga.cs b/API/Tranga.cs index 3cc65b8..48773d9 100644 --- a/API/Tranga.cs +++ b/API/Tranga.cs @@ -134,13 +134,35 @@ public static class Tranga } //Retrieve waiting and due Jobs - List waitingJobs = context.Jobs.Local.Where(j => - j.Enabled && (j.state == JobState.FirstExecution || j.state == JobState.CompletedWaiting)).ToList(); List runningJobs = context.Jobs.Local.Where(j => j.state == JobState.Running).ToList(); - List dueJobs = waitingJobs.Where(j => j.NextExecution < DateTime.UtcNow).ToList(); - + List busyConnectors = GetBusyConnectors(runningJobs); - List startJobs = FilterJobPreconditions(context, dueJobs, busyConnectors); + + List waitingJobs = GetWaitingJobs(context.Jobs.Local.ToList()); + List dueJobs = FilterDueJobs(waitingJobs); + List jobsWithoutBusyConnectors = FilterJobWithBusyConnectors(dueJobs, busyConnectors); + List jobsWithoutMissingDependencies = FilterJobDependencies(context, jobsWithoutBusyConnectors); + + List jobsWithoutDownloading = + jobsWithoutMissingDependencies + .Where(j => j.JobType != JobType.DownloadSingleChapterJob) + .ToList(); + List firstChapterPerConnector = + jobsWithoutMissingDependencies + .Where(j => j.JobType == JobType.DownloadSingleChapterJob) + .OrderBy(j => + { + DownloadSingleChapterJob dscj = (DownloadSingleChapterJob)j; + return dscj.Chapter; + }) + .DistinctBy(j => + { + DownloadSingleChapterJob dscj = (DownloadSingleChapterJob)j; + return dscj.Chapter.ParentManga.MangaConnector; + }) + .ToList(); + + List startJobs = jobsWithoutDownloading.Concat(firstChapterPerConnector).ToList(); //Start Jobs that are allowed to run (preconditions match) foreach (Job job in startJobs) @@ -155,7 +177,7 @@ public static class Tranga Log.Debug($"Jobs Completed: {completedJobs.Count} Failed: {failedJobs.Count} Running: {runningJobs.Count}\n" + $"Waiting: {waitingJobs.Count}\n" + $"\tof which Due: {dueJobs.Count}\n" + - $"\t\tof which Started: {startJobs.Count}"); + $"\t\tof which Started: {jobsWithoutMissingDependencies.Count}"); (Thread, Job)[] removeFromThreadsList = RunningJobs.Where(t => !t.Key.IsAlive) .Select(t => (t.Key, t.Value)).ToArray(); @@ -187,9 +209,21 @@ public static class Tranga } return busyConnectors.ToList(); } + + private static List GetWaitingJobs(List jobs) => + jobs + .Where(j => + j.Enabled && + (j.state == JobState.FirstExecution || j.state == JobState.CompletedWaiting)) + .ToList(); - private static List FilterJobPreconditions(PgsqlContext context, List dueJobs, List busyConnectors) => - dueJobs + private static List FilterDueJobs(List jobs) => + jobs + .Where(j => j.NextExecution < DateTime.UtcNow) + .ToList(); + + private static List FilterJobDependencies(PgsqlContext context, List jobs) => + jobs .Where(j => { Log.Debug($"Loading Job Preconditions {j}..."); @@ -197,6 +231,10 @@ public static class Tranga Log.Debug($"Loaded Job Preconditions {j}!"); return j.DependenciesFulfilled; }) + .ToList(); + + private static List FilterJobWithBusyConnectors(List jobs, List busyConnectors) => + jobs .Where(j => { //Filter jobs with busy connectors @@ -204,7 +242,6 @@ public static class Tranga return busyConnectors.Contains(mangaConnector) == false; return true; }) - .DistinctBy(j => j.JobType) .ToList(); private static MangaConnector? GetJobConnector(Job job)