feat(data): server queries for uploads, jobs, render nodes and sequence edits

Additive repository methods the server needs: upload state transitions,
pending bytes, per-day and per-user upload counts, render-result uploads;
job claim by id, node progress and completion, priority, queue counts and
requeue of numbered workers; sequence edits with settings_version
optimistic concurrency; audit filter by action; latest token per user.
Migration 0005 adds session_key, job_id and render_json to uploads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 11:31:07 +00:00
1 parent 446daeae2b
commit 9a38057169
9 files changed
+282 -6

No files matched your search

+87 -1
View File
@@ -3,6 +3,9 @@ using Titano.Core.Model;
namespace Titano.Data.Repositories;
/// <summary>Jobs per state; <see cref="DoneSince"/> and <see cref="FailedSince"/> count those finished since a given time.</summary>
public sealed record JobCounts(int Queued, int Running, int Paused, int DoneSince, int FailedSince);
/// <summary>
/// The persistent job queue (BRIEF §14.6): workers claim the oldest job of the highest priority atomically,
/// write checkpoints while running, and on startup requeue what a crash left running.
@@ -74,11 +77,16 @@ public sealed class JobRepository
}
/// <summary>Jobs, newest first, optionally of one state.</summary>
public Task<Page<Job>> ListAsync(JobState? state = null, int limit = 50, string? cursor = null, CancellationToken cancellationToken = default)
public Task<Page<Job>> ListAsync(JobState? state = null, int limit = 50, string? cursor = null, CancellationToken cancellationToken = default) =>
ListAsync(state, kind: null, limit, cursor, cancellationToken);
/// <summary>Jobs, newest first, optionally of one state and one kind.</summary>
public Task<Page<Job>> ListAsync(JobState? state, JobKind? kind, int limit = 50, string? cursor = null, CancellationToken cancellationToken = default)
{
var p = new SqlParameters();
var where = new List<string>();
if (state is { } s) where.Add($"state = {p.Add((int)s)}");
if (kind is { } k) where.Add($"kind = {p.Add((int)k)}");
if (cursor is not null)
{
var (value, id) = Cursor.Decode("jobs", cursor);
@@ -116,6 +124,84 @@ public sealed class JobRepository
(nodeId is null ? string.Empty : " AND node_id = $1"),
nodeId is null ? [] : [nodeId], cancellationToken);
/// <summary>
/// Crash recovery of one job engine: requeues running jobs claimed by <paramref name="workerId"/> or by its numbered
/// workers (<c>workerId#n</c>), whatever their number was in the previous run.
/// </summary>
public Task<int> RequeueRunningWorkersAsync(string workerId, CancellationToken cancellationToken = default) =>
_db.ExecuteAsync(
$"UPDATE jobs SET state = {(int)JobState.Queued}, node_id = NULL, started = NULL WHERE state = {(int)JobState.Running} " +
"AND (node_id = $1 OR node_id LIKE $2 ESCAPE '\\')",
[workerId, SqlDialect.ContainsPattern(workerId)[1..^1] + "#%"], cancellationToken);
public async Task<bool> SetPriorityAsync(Guid id, JobPriority priority, CancellationToken cancellationToken = default) =>
await _db.ExecuteAsync("UPDATE jobs SET priority = $2 WHERE id = $1", [id, (int)priority], cancellationToken).ConfigureAwait(false) > 0;
/// <summary>Queued jobs of one kind in claim order (highest priority, then oldest).</summary>
public Task<IReadOnlyList<Job>> ListQueuedAsync(JobKind kind, int limit = 50, CancellationToken cancellationToken = default) =>
_db.ListAsync(
$"SELECT {Columns} FROM jobs WHERE state = {(int)JobState.Queued} AND kind = $1 ORDER BY priority DESC, created, id LIMIT $2",
[(int)kind, Cursor.ClampLimit(limit)], Map, cancellationToken);
/// <summary>Running jobs claimed by <paramref name="nodeId"/>.</summary>
public Task<IReadOnlyList<Job>> ListRunningByNodeAsync(string nodeId, CancellationToken cancellationToken = default) =>
_db.ListAsync($"SELECT {Columns} FROM jobs WHERE state = {(int)JobState.Running} AND node_id = $1 ORDER BY started, id",
[nodeId], Map, cancellationToken);
/// <summary>True when a job of <paramref name="kind"/> for exactly <paramref name="targetId"/> is waiting in the queue.</summary>
public async Task<bool> IsQueuedAsync(JobKind kind, Guid targetId, CancellationToken cancellationToken = default) =>
await _db.ScalarAsync<long>(
$"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>
/// 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.
/// </summary>
public Task<Job?> TryClaimAsync(Guid id, string nodeId, CancellationToken cancellationToken = default) =>
_db.InTransactionAsync(async session =>
{
var claimed = await session.ExecuteAsync(
$"UPDATE jobs SET state = {(int)JobState.Running}, node_id = $2, started = $3, finished = NULL, error = NULL, attempts = attempts + 1 " +
$"WHERE id = $1 AND state = {(int)JobState.Queued}",
[id, nodeId, DateTimeOffset.UtcNow], cancellationToken).ConfigureAwait(false);
if (claimed == 0) return null;
return await session.QuerySingleOrDefaultAsync($"SELECT {Columns} FROM jobs WHERE id = $1", [id], Map, cancellationToken).ConfigureAwait(false);
}, cancellationToken);
/// <summary>Progress of a job a render node runs; false when the job is no longer running on that node.</summary>
public async Task<bool> UpdateNodeProgressAsync(Guid id, string nodeId, long done, long total, double? itemsPerSecond, TimeSpan? eta,
string? logTail, CancellationToken cancellationToken = default) =>
await _db.ExecuteAsync(
"UPDATE jobs SET progress_done = $3, progress_total = $4, items_per_second = $5, eta_seconds = $6, log_tail = $7 " +
$"WHERE id = $1 AND node_id = $2 AND state = {(int)JobState.Running}",
[id, nodeId, done, total, itemsPerSecond, eta?.TotalSeconds, logTail], cancellationToken).ConfigureAwait(false) > 0;
/// <summary>Ends a job a render node runs (done or failed); false when it is no longer running on that node.</summary>
public async Task<bool> FinishNodeJobAsync(Guid id, string nodeId, JobState state, string? error, string? logTail, CancellationToken cancellationToken = default)
{
if (state is not (JobState.Done or JobState.Failed)) throw new ArgumentOutOfRangeException(nameof(state), state, "Only done or failed end a node job.");
return await _db.ExecuteAsync(
"UPDATE jobs SET state = $3, error = $4, finished = $5, log_tail = COALESCE($6, log_tail) " +
$"WHERE id = $1 AND node_id = $2 AND state = {(int)JobState.Running}",
[id, nodeId, (int)state, error, DateTimeOffset.UtcNow, logTail], cancellationToken).ConfigureAwait(false) > 0;
}
/// <summary>Queue and history counters for dashboards; finished counts are those since <paramref name="finishedSince"/>.</summary>
public Task<JobCounts> CountAsync(DateTimeOffset finishedSince, CancellationToken cancellationToken = default) =>
_db.RunAsync(async session =>
{
var row = await session.QuerySingleAsync(
"SELECT " +
$"COALESCE(SUM(CASE WHEN state = {(int)JobState.Queued} THEN 1 ELSE 0 END), 0), " +
$"COALESCE(SUM(CASE WHEN state = {(int)JobState.Running} THEN 1 ELSE 0 END), 0), " +
$"COALESCE(SUM(CASE WHEN state = {(int)JobState.Paused} THEN 1 ELSE 0 END), 0), " +
$"COALESCE(SUM(CASE WHEN state = {(int)JobState.Done} AND finished >= $1 THEN 1 ELSE 0 END), 0), " +
$"COALESCE(SUM(CASE WHEN state = {(int)JobState.Failed} AND finished >= $1 THEN 1 ELSE 0 END), 0) " +
"FROM jobs", [finishedSince], cancellationToken).ConfigureAwait(false);
return new JobCounts(row.GetInt32(0), row.GetInt32(1), row.GetInt32(2), row.GetInt32(3), row.GetInt32(4));
}, cancellationToken);
private static Job Map(DbRow r) => new()
{
Id = r.GetGuid(0),