Fix multiple jobs running on the same connector
Fix state-updates
Add more Documentation to JobController
Only allow non-running jobs to be started
This commit is contained in:
Glax 2025-03-03 21:13:03 +01:00
parent 8a0829ef69
commit e2ff2c76ed
3 changed files with 47 additions and 7 deletions

View File

@ -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
/// </summary>
/// <param name="id">Job-ID</param>
/// <returns>Nothing</returns>
/// <response code="202">Job started</response>
/// <response code="404">Job with ID not found</response>
/// <response code="409">Job was already running</response>
/// <response code="500">Internal Error</response>
[HttpPost("{id}/Start")]
[ProducesResponseType(Status202Accepted)]
[ProducesResponseType(Status404NotFound)]
[ProducesResponseType(Status500InternalServerError)]
[ProducesResponseType<AcceptedResult>(Status202Accepted)]
[ProducesResponseType<NotFoundResult>(Status404NotFound)]
[ProducesResponseType<ConflictResult>(Status409Conflict)]
[ProducesResponseType<ObjectResult>(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
}
}
/// <summary>
/// NOT IMPLEMENTED. Stops the Job with the requested ID
/// </summary>
/// <param name="id">Job-ID</param>
/// <response code="202">Job started</response>
/// <response code="404">Job with ID not found</response>
/// <response code="409">Job was not running</response>
/// <response code="500">Internal Error</response>
/// <remarks>NOT IMPLEMENTED</remarks>
[ProducesResponseType<AcceptedResult>(Status202Accepted)]
[ProducesResponseType<NotFoundResult>(Status404NotFound)]
[ProducesResponseType<ConflictResult>(Status409Conflict)]
[ProducesResponseType<ObjectResult>(Status500InternalServerError)]
[HttpPost("{id}/Stop")]
public IActionResult StopJob(string id)
{

View File

@ -49,8 +49,10 @@ public abstract class Job
PgsqlContext context = scope.ServiceProvider.GetRequiredService<PgsqlContext>();
this.state = JobState.Running;
context.SaveChanges();
IEnumerable<Job> newJobs = RunInternal(context);
this.state = JobState.Completed;
context.SaveChanges();
return newJobs;
}

View File

@ -69,7 +69,7 @@ public static class Tranga
Log.Info(TRANGA);
while (true)
{
List<Job> completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
List<Job> 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<Job> newJobs = job.Run(serviceProvider);