diff --git a/API/Controllers/JobController.cs b/API/Controllers/JobController.cs index 3559933..712e3c3 100644 --- a/API/Controllers/JobController.cs +++ b/API/Controllers/JobController.cs @@ -5,6 +5,7 @@ using API.Schema.Jobs; using Asp.Versioning; using log4net; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ModelBinding; using static Microsoft.AspNetCore.Http.StatusCodes; // ReSharper disable InconsistentNaming @@ -326,6 +327,7 @@ public class JobController(PgsqlContext context, ILog Log) : Controller /// Starts the Job with the requested ID /// /// Job-ID + /// Start Jobs necessary for execution /// Job started /// Job with ID not found /// Job was already running @@ -335,16 +337,22 @@ public class JobController(PgsqlContext context, ILog Log) : Controller [ProducesResponseType(Status404NotFound)] [ProducesResponseType(Status409Conflict)] [ProducesResponseType(Status500InternalServerError, "text/plain")] - public IActionResult StartJob(string JobId) + public IActionResult StartJob(string JobId, [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)]bool startDependencies = false) { Job? ret = context.Jobs.Find(JobId); if (ret is null) return NotFound(); + List dependencies = startDependencies ? ret.GetDependenciesAndSelf() : [ret]; + try { - if (ret.state >= JobState.Running && ret.state < JobState.Completed) + if(dependencies.Any(d => d.state >= JobState.Running && d.state < JobState.Completed)) return new ConflictResult(); - ret.LastExecution = DateTime.UnixEpoch; + dependencies.ForEach(d => + { + d.LastExecution = DateTime.UnixEpoch; + d.state = JobState.CompletedWaiting; + }); context.SaveChanges(); return Accepted(); } diff --git a/API/Schema/Jobs/Job.cs b/API/Schema/Jobs/Job.cs index 97ce835..5e59ee0 100644 --- a/API/Schema/Jobs/Job.cs +++ b/API/Schema/Jobs/Job.cs @@ -118,6 +118,17 @@ public abstract class Job protected abstract IEnumerable RunInternal(PgsqlContext context); + public List GetDependenciesAndSelf() + { + List ret = new (); + foreach (Job job in DependsOnJobs) + { + ret.AddRange(job.GetDependenciesAndSelf()); + } + ret.Add(this); + return ret; + } + public override string ToString() { return $"{JobId}";