diff --git a/API/Schema/Jobs/Job.cs b/API/Schema/Jobs/Job.cs index 5e59ee0..89b1942 100644 --- a/API/Schema/Jobs/Job.cs +++ b/API/Schema/Jobs/Job.cs @@ -9,7 +9,7 @@ using Newtonsoft.Json; namespace API.Schema.Jobs; [PrimaryKey("JobId")] -public abstract class Job +public abstract class Job : IComparable { [StringLength(64)] [Required] @@ -119,16 +119,39 @@ public abstract class Job protected abstract IEnumerable RunInternal(PgsqlContext context); public List GetDependenciesAndSelf() + { + List ret = GetDependencies(); + ret.Add(this); + return ret; + } + + public List GetDependencies() { List ret = new (); foreach (Job job in DependsOnJobs) { ret.AddRange(job.GetDependenciesAndSelf()); } - ret.Add(this); return ret; } + public int CompareTo(Job? other) + { + if (other is null) + return -1; + // Sort by missing dependencies + if (this.GetDependencies().Count(job => !job.IsCompleted) < + other.GetDependencies().Count(job => !job.IsCompleted)) + return -1; + // Sort by NextExecution-time + if (this.NextExecution < other.NextExecution) + return -1; + // Sort by JobPriority + if (JobQueueSorter.GetPriority(this) > JobQueueSorter.GetPriority(other)) + return -1; + return 1; + } + public override string ToString() { return $"{JobId}";