From 5465ac4e5cae256bc60a1d05982be6ae1c6ef591 Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 2 Sep 2023 14:13:15 +0200 Subject: [PATCH] Removed DELETE Jobs/DownloadChapter and Jobs/MonitorManga. Can both be reached with DELETE Jobs (jobId) Added POST Jobs/Cancel CancelJob and RemoveJob cancels/removes subJobs --- Tranga/Jobs/Job.cs | 8 ++++++-- Tranga/Jobs/JobBoss.cs | 2 ++ Tranga/Server.cs | 37 ++++++++++--------------------------- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/Tranga/Jobs/Job.cs b/Tranga/Jobs/Job.cs index d0520d6..2707fbd 100644 --- a/Tranga/Jobs/Job.cs +++ b/Tranga/Jobs/Job.cs @@ -11,6 +11,7 @@ public abstract class Job : GlobalBase public DateTime? lastExecution { get; private set; } public DateTime nextExecution => NextExecution(); public string id => GetId(); + internal IEnumerable? subJobs { get; private set; } public Job(GlobalBase clone, MangaConnector connector, bool recurring = false, TimeSpan? recurrenceTime = null) : base(clone) { @@ -65,14 +66,17 @@ public abstract class Job : GlobalBase Log($"Cancelling {this}"); this.progressToken.cancellationRequested = true; this.progressToken.Complete(); + if(subJobs is not null) + foreach(Job subJob in subJobs) + subJob.Cancel(); } public IEnumerable ExecuteReturnSubTasks() { progressToken.Start(); - IEnumerable ret = ExecuteReturnSubTasksInternal(); + subJobs = ExecuteReturnSubTasksInternal(); lastExecution = DateTime.Now; - return ret; + return subJobs; } protected abstract IEnumerable ExecuteReturnSubTasksInternal(); diff --git a/Tranga/Jobs/JobBoss.cs b/Tranga/Jobs/JobBoss.cs index ba8b3c7..12eea32 100644 --- a/Tranga/Jobs/JobBoss.cs +++ b/Tranga/Jobs/JobBoss.cs @@ -51,6 +51,8 @@ public class JobBoss : GlobalBase Log($"Removing {job}"); job.Cancel(); this.jobs.Remove(job); + if(job.subJobs is not null) + RemoveJobs(job.subJobs); } public void RemoveJobs(IEnumerable jobsToRemove) diff --git a/Tranga/Server.cs b/Tranga/Server.cs index 153f82c..f5ef7c7 100644 --- a/Tranga/Server.cs +++ b/Tranga/Server.cs @@ -258,6 +258,16 @@ public class Server : GlobalBase _parent.jobBoss.AddJobToQueue(job!); SendResponse(HttpStatusCode.Accepted, response); break; + case "Jobs/Cancel": + if (!requestVariables.TryGetValue("jobId", out jobId) || + !_parent.jobBoss.TryGetJobById(jobId, out job)) + { + SendResponse(HttpStatusCode.BadRequest, response); + break; + } + job!.Cancel(); + SendResponse(HttpStatusCode.Accepted, response); + break; case "Settings/UpdateDownloadLocation": if (!requestVariables.TryGetValue("downloadLocation", out string? downloadLocation) || !requestVariables.TryGetValue("moveFiles", out string? moveFilesStr) || @@ -372,33 +382,6 @@ public class Server : GlobalBase _parent.jobBoss.RemoveJob(job!); SendResponse(HttpStatusCode.Accepted, response); break; - case "Jobs/DownloadChapter": - if(!requestVariables.TryGetValue("connector", out connectorName) || - !requestVariables.TryGetValue("internalId", out internalId) || - !requestVariables.TryGetValue("chapterNumber", out string? chapterNumber) || - _parent.GetConnector(connectorName) is null || - _parent.GetPublicationById(internalId) is null) - { - SendResponse(HttpStatusCode.BadRequest, response); - break; - } - _parent.jobBoss.RemoveJobs(_parent.jobBoss.GetJobsLike(connectorName, internalId, chapterNumber)); - SendResponse(HttpStatusCode.Accepted, response); - break; - case "Jobs/MonitorManga": - if(!requestVariables.TryGetValue("connector", out connectorName) || - !requestVariables.TryGetValue("internalId", out internalId) || - _parent.GetConnector(connectorName) is null || - _parent.GetPublicationById(internalId) is null) - { - SendResponse(HttpStatusCode.BadRequest, response); - break; - } - connector = _parent.GetConnector(connectorName)!; - manga = (Manga)_parent.GetPublicationById(internalId)!; - _parent.jobBoss.RemoveJobs(_parent.jobBoss.GetJobsLike(connector, manga)); - SendResponse(HttpStatusCode.Accepted, response); - break; case "Jobs/DownloadNewChapters": if(!requestVariables.TryGetValue("connector", out connectorName) || !requestVariables.TryGetValue("internalId", out internalId) ||