55 lines
1.8 KiB
C#
Raw Normal View History

2024-12-14 21:53:29 +01:00
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace API.Schema.Jobs;
[PrimaryKey("JobId")]
public abstract class Job
2024-12-14 21:53:29 +01:00
{
[MaxLength(64)]
public string JobId { get; init; }
[MaxLength(64)]
2025-01-09 01:34:03 +01:00
public string? ParentJobId { get; init; }
public Job? ParentJob { get; init; }
2024-12-14 21:53:29 +01:00
[MaxLength(64)]
2025-01-09 01:34:03 +01:00
public ICollection<string>? DependsOnJobsIds { get; init; }
public ICollection<Job>? DependsOnJobs { get; init; }
2024-12-14 21:53:29 +01:00
public JobType JobType { get; init; }
public ulong RecurrenceMs { get; set; }
public DateTime LastExecution { get; internal set; } = DateTime.UnixEpoch;
2025-01-09 01:34:03 +01:00
[NotMapped]
public DateTime NextExecution => LastExecution.AddMilliseconds(RecurrenceMs);
2024-12-14 21:53:29 +01:00
public JobState state { get; internal set; } = JobState.Waiting;
2025-01-09 01:34:03 +01:00
public Job(string jobId, JobType jobType, ulong recurrenceMs, Job? parentJob = null, ICollection<Job>? dependsOnJobs = null)
: this(jobId, jobType, recurrenceMs, parentJob?.JobId, dependsOnJobs?.Select(j => j.JobId).ToList())
{
this.ParentJob = parentJob;
this.DependsOnJobs = dependsOnJobs;
}
public Job(string jobId, JobType jobType, ulong recurrenceMs, string? parentJobId = null, ICollection<string>? dependsOnJobsIds = null)
2024-12-14 21:53:29 +01:00
{
JobId = jobId;
ParentJobId = parentJobId;
2025-01-09 01:34:03 +01:00
DependsOnJobsIds = dependsOnJobsIds;
2024-12-14 21:53:29 +01:00
JobType = jobType;
RecurrenceMs = recurrenceMs;
}
2024-12-16 23:29:57 +01:00
public IEnumerable<Job> Run(PgsqlContext context)
2024-12-16 21:24:00 +01:00
{
this.state = JobState.Running;
2024-12-16 23:29:57 +01:00
IEnumerable<Job> newJobs = RunInternal(context);
2024-12-16 21:24:00 +01:00
this.state = JobState.Completed;
return newJobs;
}
2024-12-16 23:29:57 +01:00
protected abstract IEnumerable<Job> RunInternal(PgsqlContext context);
2024-12-14 21:53:29 +01:00
}