Queue durable Studio 2 jobs with sticky identity and saved metadata

This commit is contained in:
Towsty
2026-09-10 20:56:37 -05:00
parent bd82f5bd5f
commit 70b6b70fcd
12 changed files with 374 additions and 10 deletions
+14
View File
@@ -0,0 +1,14 @@
import { mkdirSync, readFileSync, writeFileSync, renameSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
export function studio2Root() { const dir = join(String(useRuntimeConfig().libraryDir), 'studio2'); mkdirSync(dir, { recursive: true }); return dir; }
export function saveRecord(record: any) { const path = join(studio2Root(), `${record.id}.json`); writeFileSync(path + '.tmp', JSON.stringify(record)); renameSync(path + '.tmp', path); }
export function readRecord(owner: string, id: string) { if (!/^[\w-]+$/.test(id))
throw createError({ statusCode: 400, statusMessage: 'Invalid job ID' }); const r = JSON.parse(readFileSync(join(studio2Root(), `${id}.json`), 'utf8')); if (r.owner !== owner)
throw createError({ statusCode: 404, statusMessage: 'Job not found' }); return r; }
export function records(owner?: string) { return readdirSync(studio2Root()).filter(n => n.endsWith('.json')).flatMap(n => { try {
const r = JSON.parse(readFileSync(join(studio2Root(), n), 'utf8'));
return !owner || r.owner === owner ? [r] : [];
}
catch {
return [];
} }).sort((a, b) => b.queuedAt - a.queuedAt); }