Job is IComparable<Job>

This commit is contained in:
2025-06-30 12:58:00 +02:00
parent e063cf1fd9
commit e5937d2654

View File

@ -9,7 +9,7 @@ using Newtonsoft.Json;
namespace API.Schema.Jobs;
[PrimaryKey("JobId")]
public abstract class Job
public abstract class Job : IComparable<Job>
{
[StringLength(64)]
[Required]
@ -119,16 +119,39 @@ public abstract class Job
protected abstract IEnumerable<Job> RunInternal(PgsqlContext context);
public List<Job> GetDependenciesAndSelf()
{
List<Job> ret = GetDependencies();
ret.Add(this);
return ret;
}
public List<Job> GetDependencies()
{
List<Job> 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}";