48 lines
1.5 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)]
public string? ParentJobId { get; internal set; }
internal virtual Job ParentJob { get; }
[MaxLength(64)]
public string[]? DependsOnJobIds { get; init; }
public virtual Job[] DependsOnJobs { get; init; }
public JobType JobType { get; init; }
public ulong RecurrenceMs { get; set; }
public DateTime LastExecution { get; internal set; } = DateTime.UnixEpoch;
public DateTime NextExecution { get; internal set; }
public JobState state { get; internal set; } = JobState.Waiting;
public Job(string jobId, JobType jobType, ulong recurrenceMs, string? parentJobId = null,
string[]? dependsOnJobIds = null)
{
JobId = jobId;
ParentJobId = parentJobId;
DependsOnJobIds = dependsOnJobIds;
JobType = jobType;
RecurrenceMs = recurrenceMs;
NextExecution = LastExecution.AddMilliseconds(RecurrenceMs);
}
2024-12-16 21:24:00 +01:00
public IEnumerable<Job> Run()
{
this.state = JobState.Running;
IEnumerable<Job>? newJobs = RunInternal();
this.state = JobState.Completed;
return newJobs;
}
protected abstract IEnumerable<Job> RunInternal();
2024-12-14 21:53:29 +01:00
}