diff --git a/API/Controllers/JobController.cs b/API/Controllers/JobController.cs index 2ccb3a2..a469c12 100644 --- a/API/Controllers/JobController.cs +++ b/API/Controllers/JobController.cs @@ -152,7 +152,7 @@ public class JobController(PgsqlContext context) : Controller { context.Jobs.Add(job); context.SaveChanges(); - return Created(); + return new CreatedResult(job.JobId, job); } catch (Exception e) { @@ -193,11 +193,15 @@ public class JobController(PgsqlContext context) : Controller /// Starts the Job with the requested ID /// /// Job-ID - /// Nothing + /// Job started + /// Job with ID not found + /// Job was already running + /// Internal Error [HttpPost("{id}/Start")] - [ProducesResponseType(Status202Accepted)] - [ProducesResponseType(Status404NotFound)] - [ProducesResponseType(Status500InternalServerError)] + [ProducesResponseType(Status202Accepted)] + [ProducesResponseType(Status404NotFound)] + [ProducesResponseType(Status409Conflict)] + [ProducesResponseType(Status500InternalServerError)] public IActionResult StartJob(string id) { Job? ret = context.Jobs.Find(id); @@ -205,7 +209,9 @@ public class JobController(PgsqlContext context) : Controller return NotFound(); try { - context.Update(ret); + if (ret.state >= JobState.Running && ret.state < JobState.Completed) + return new ConflictResult(); + ret.LastExecution = DateTime.UnixEpoch; context.SaveChanges(); return Accepted(); } @@ -215,6 +221,19 @@ public class JobController(PgsqlContext context) : Controller } } + /// + /// NOT IMPLEMENTED. Stops the Job with the requested ID + /// + /// Job-ID + /// Job started + /// Job with ID not found + /// Job was not running + /// Internal Error + /// NOT IMPLEMENTED + [ProducesResponseType(Status202Accepted)] + [ProducesResponseType(Status404NotFound)] + [ProducesResponseType(Status409Conflict)] + [ProducesResponseType(Status500InternalServerError)] [HttpPost("{id}/Stop")] public IActionResult StopJob(string id) { diff --git a/API/Schema/Jobs/Job.cs b/API/Schema/Jobs/Job.cs index e5c078e..fc23512 100644 --- a/API/Schema/Jobs/Job.cs +++ b/API/Schema/Jobs/Job.cs @@ -49,8 +49,10 @@ public abstract class Job PgsqlContext context = scope.ServiceProvider.GetRequiredService(); this.state = JobState.Running; + context.SaveChanges(); IEnumerable newJobs = RunInternal(context); this.state = JobState.Completed; + context.SaveChanges(); return newJobs; } diff --git a/API/Tranga.cs b/API/Tranga.cs index e494fb3..9c15dc4 100644 --- a/API/Tranga.cs +++ b/API/Tranga.cs @@ -69,7 +69,7 @@ public static class Tranga Log.Info(TRANGA); while (true) { - List completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList(); + List completedJobs = context.Jobs.Where(j => j.state >= JobState.Completed && j.state < JobState.Failed).ToList(); foreach (Job job in completedJobs) if (job.RecurrenceMs <= 0) context.Jobs.Remove(job); @@ -87,6 +87,25 @@ public static class Tranga // If the job is already running, skip it if (RunningJobs.Values.Any(j => j.JobId == job.JobId)) continue; + if (job is DownloadNewChaptersJob dncj) + { + if (RunningJobs.Values.Any(j => + j is DownloadNewChaptersJob rdncj && + rdncj.Manga?.MangaConnector == dncj.Manga?.MangaConnector)) + { + continue; + } + } + else if (job is DownloadSingleChapterJob dscj) + { + if (RunningJobs.Values.Any(j => + j is DownloadSingleChapterJob rdscj && rdscj.Chapter?.ParentManga?.MangaConnector == + dscj.Chapter?.ParentManga?.MangaConnector)) + { + continue; + } + } + Thread t = new(() => { IEnumerable newJobs = job.Run(serviceProvider);