fix(db): jobs.update writes only patched columns; late progress cannot resurrect a finished job
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4636273d55
commit
f061e36092
2 files changed
+39
-6
No files matched your search
@@ -44,13 +44,21 @@ export function jobsRepo(db: Db) {
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Single UPDATE writing only the patched columns (plus updated_at). A progress-only patch
|
||||
* (progress and/or message) is guarded by status = 'running', so a late report on a
|
||||
* finished job is a no-op.
|
||||
*/
|
||||
async update(id: string, patch: JobPatch): Promise<void> {
|
||||
const cur = await get(id);
|
||||
if (!cur) return;
|
||||
const next = { ...cur, ...patch };
|
||||
await db.sql`UPDATE jobs SET status = ${next.status}, progress = ${next.progress}, message = ${next.message ?? null},
|
||||
result_json = ${next.result === null || next.result === undefined ? null : JSON.stringify(next.result)},
|
||||
error = ${next.error ?? null}, updated_at = ${new Date().toISOString()} WHERE id = ${id}`;
|
||||
const set: Record<string, unknown> = { updated_at: new Date().toISOString() };
|
||||
if (patch.status !== undefined) set.status = patch.status;
|
||||
if (patch.progress !== undefined) set.progress = patch.progress;
|
||||
if (patch.message !== undefined) set.message = patch.message;
|
||||
if (patch.result !== undefined) set.result_json = patch.result === null ? null : JSON.stringify(patch.result);
|
||||
if (patch.error !== undefined) set.error = patch.error;
|
||||
const progressOnly = patch.status === undefined && patch.result === undefined && patch.error === undefined;
|
||||
if (progressOnly) await db.sql`UPDATE jobs SET ${db.sql(set)} WHERE id = ${id} AND status = 'running'`;
|
||||
else await db.sql`UPDATE jobs SET ${db.sql(set)} WHERE id = ${id}`;
|
||||
},
|
||||
|
||||
async list(kind?: string): Promise<JobRecord[]> {
|
||||
|
||||
Reference in new issue
Block a user