feat: owner review 2 phase 2: scheduled jobs and log settings (#10)
CI / Build and test (push) Successful in 7m0s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 13m28s
CI / PostgreSQL 16 integration (push) Successful in 5m52s
CI / PostgreSQL 17 integration (push) Successful in 4m50s
CI / Docker image (push) Successful in 1m4s
Release / Server image (push) Successful in 1m35s
CI / Build and test (push) Successful in 7m0s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 13m28s
CI / PostgreSQL 16 integration (push) Successful in 5m52s
CI / PostgreSQL 17 integration (push) Successful in 4m50s
CI / Docker image (push) Successful in 1m4s
Release / Server image (push) Successful in 1m35s
- Jobs > Scheduled on the desktop and the admin panel. Every timer that queued work is now a visible system schedule: archive scan, upload clean-up, index backup, job-history and audit clean-ups, desktop housekeeping. New actions: verify, thumbnails, proxies, analysis, maintenance, backup, clean-ups, reports. - The scheduler: migration 0007, in-house recurrence, compare-and-set claim, catch-up, scan at start. - Log settings: levels, retention, size parts, total limit, daily or hourly rotation, audit retention, diagnostics share; the server writes Debug/Trace to its files again; logging never fails a job. Upgrade: CHRONOS_LOG_LEVEL set in an existing container locks both levels; migration 0007 is one-way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #10.
This commit is contained in:
1 parent
1f089888e8
commit
af9eeaab9b
349 files changed
+28341
-1304
No files matched your search
@@ -15,7 +15,11 @@ public sealed class JobRepository
|
||||
{
|
||||
private const string Columns =
|
||||
"id, kind, target_ids, target_label, parameters_json, state, priority, progress_done, progress_total, items_per_second, " +
|
||||
"eta_seconds, checkpoint, log_tail, created_by, node_id, attempts, error, created, started, finished";
|
||||
"eta_seconds, checkpoint, log_tail, created_by, node_id, attempts, error, created, started, finished, schedule_id";
|
||||
|
||||
/// <summary>The states of a job that has not ended: queued, running, paused.</summary>
|
||||
private static readonly string PendingStates =
|
||||
string.Join(", ", new[] { JobState.Queued, JobState.Running, JobState.Paused }.Select(s => ((int)s).ToString(CultureInfo.InvariantCulture)));
|
||||
|
||||
private readonly DbAccess _db;
|
||||
|
||||
@@ -23,11 +27,11 @@ public sealed class JobRepository
|
||||
|
||||
public async Task EnqueueAsync(Job job, CancellationToken cancellationToken = default) =>
|
||||
await _db.ExecuteAsync(
|
||||
$"INSERT INTO jobs ({Columns}) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)",
|
||||
$"INSERT INTO jobs ({Columns}) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)",
|
||||
[
|
||||
job.Id, (int)job.Kind, string.Join(',', job.TargetIds), job.TargetLabel, DbParam.Json(job.ParametersJson), (int)job.State,
|
||||
(int)job.Priority, job.ProgressDone, job.ProgressTotal, job.ItemsPerSecond, job.Eta?.TotalSeconds, job.Checkpoint, job.LogTail,
|
||||
job.CreatedBy, job.NodeId, job.Attempts, job.Error, job.Created, job.Started, job.Finished,
|
||||
job.CreatedBy, job.NodeId, job.Attempts, job.Error, job.Created, job.Started, job.Finished, job.ScheduleId,
|
||||
], cancellationToken).ConfigureAwait(false);
|
||||
|
||||
public Task<Job?> GetAsync(Guid id, CancellationToken cancellationToken = default) =>
|
||||
@@ -173,6 +177,73 @@ public sealed class JobRepository
|
||||
$"SELECT count(*) FROM jobs WHERE state = {(int)JobState.Queued} AND kind = $1 AND target_ids = $2",
|
||||
[(int)kind, targetId.ToString()], cancellationToken).ConfigureAwait(false) > 0;
|
||||
|
||||
/// <summary>
|
||||
/// True when a job of one of <paramref name="kinds"/> is queued, running or paused; with <paramref name="targetIds"/>,
|
||||
/// only such a job that targets at least one of them (an archive, sequences) counts. A schedule does not queue its
|
||||
/// action again while one waits (owner review 2, R10).
|
||||
/// </summary>
|
||||
public async Task<bool> HasPendingAsync(IReadOnlyCollection<JobKind> kinds, IReadOnlyCollection<Guid>? targetIds = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(kinds);
|
||||
if (kinds.Count == 0 || targetIds is { Count: 0 }) return false;
|
||||
var p = new SqlParameters();
|
||||
var sql = $"SELECT count(*) FROM jobs WHERE state IN ({PendingStates}) AND kind IN ({KindList(kinds)})";
|
||||
// Target ids are stored comma-joined as lower-case text, which never holds a LIKE wildcard.
|
||||
if (targetIds is not null)
|
||||
sql += $" AND ({string.Join(" OR ", targetIds.Distinct().Select(id => $"target_ids LIKE {p.Add(SqlDialect.ContainsPattern(id.ToString()))} ESCAPE '\\'"))})";
|
||||
return await _db.ScalarAsync<long>(sql, p.ToArray(), cancellationToken).ConfigureAwait(false) > 0;
|
||||
}
|
||||
|
||||
/// <summary>The targets of the queued, running and paused jobs of <paramref name="kind"/> (the newest 5000 such jobs).</summary>
|
||||
public Task<HashSet<Guid>> ListPendingTargetsAsync(JobKind kind, CancellationToken cancellationToken = default) =>
|
||||
_db.RunAsync(async session =>
|
||||
{
|
||||
var rows = await session.QueryListAsync(
|
||||
$"SELECT target_ids FROM jobs WHERE kind = $1 AND state IN ({PendingStates}) ORDER BY created DESC, id DESC LIMIT 5000",
|
||||
[(int)kind], r => r.GetString(0), cancellationToken).ConfigureAwait(false);
|
||||
var ids = new HashSet<Guid>();
|
||||
foreach (var text in rows)
|
||||
{
|
||||
if (text.Length == 0) continue;
|
||||
foreach (var id in text.Split(',')) ids.Add(Guid.Parse(id));
|
||||
}
|
||||
return ids;
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>The jobs the schedule <paramref name="scheduleId"/> queued, newest first.</summary>
|
||||
public Task<IReadOnlyList<Job>> ListByScheduleAsync(Guid scheduleId, int limit = 20, CancellationToken cancellationToken = default) =>
|
||||
_db.ListAsync($"SELECT {Columns} FROM jobs WHERE schedule_id = $1 ORDER BY created DESC, id DESC LIMIT $2",
|
||||
[scheduleId, Cursor.ClampLimit(limit)], Map, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// The jobs the schedule <paramref name="scheduleId"/> queued, newest first, a page at a time (the runs a schedule's
|
||||
/// row lists in the admin panel); <paramref name="cursor"/> continues after the previous page.
|
||||
/// </summary>
|
||||
public Task<Page<Job>> PageByScheduleAsync(Guid scheduleId, int limit = 20, string? cursor = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var p = new SqlParameters();
|
||||
var where = $"schedule_id = {p.Add(scheduleId)}";
|
||||
if (cursor is not null)
|
||||
{
|
||||
var (value, id) = Cursor.Decode("schedule-runs", cursor);
|
||||
where += " AND " + Cursor.After("created", "id", descending: true, value, id, p.Add);
|
||||
}
|
||||
var take = Cursor.ClampLimit(limit);
|
||||
var sql = $"SELECT {Columns} FROM jobs WHERE {where} ORDER BY created DESC, id DESC LIMIT {p.Add(take + 1)}";
|
||||
return _db.RunAsync(async session =>
|
||||
{
|
||||
var jobs = await session.QueryListAsync(sql, p.ToArray(), Map, cancellationToken).ConfigureAwait(false);
|
||||
string? next = null;
|
||||
if (jobs.Count > take)
|
||||
{
|
||||
jobs.RemoveAt(take);
|
||||
next = Cursor.Encode("schedule-runs", jobs[^1].Created, jobs[^1].Id);
|
||||
}
|
||||
return new Page<Job>(jobs, next);
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Claims one specific queued job for <paramref name="nodeId"/> (render nodes pick by capability); null when another
|
||||
/// claimer was faster or the job is no longer queued.
|
||||
@@ -245,5 +316,6 @@ public sealed class JobRepository
|
||||
Created = r.GetDateTimeOffset(17),
|
||||
Started = r.GetDateTimeOffsetOrNull(18),
|
||||
Finished = r.GetDateTimeOffsetOrNull(19),
|
||||
ScheduleId = r.GetGuidOrNull(20),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user