feat: add async Sprite job manager with progress polling
- Add SpriteJobManager and job RPCs (start/get/list) for connect, launch, create, and sync operations - Time-box status RPC with 5s remote timeout and lazy remote fetching - Cache remote sprites, raw token, and host/agent lists to avoid redundant API calls - Harden sprite host exec timeouts, JSON parsing, and npm install with --no-audit --no-fund - UI: poll active jobs with phase updates in workers screen - Reduce token exchange and OpenBao request timeout to 8s
This commit is contained in:
1 parent
32c02cb3cd
commit
36d88ef168
8 files changed
+1142
-186
No files matched your search
@@ -0,0 +1,150 @@
|
||||
import { randomBytes } from "node:crypto";
|
||||
import type { SpriteJob, SpriteJobKind, SpriteJobResult } from "../shared/contracts";
|
||||
|
||||
interface InternalJob extends SpriteJob {
|
||||
dedupeKey?: string;
|
||||
}
|
||||
|
||||
const FINISHED_JOB_TTL_MS = 15 * 60_000;
|
||||
const MAX_STORED_JOBS = 50;
|
||||
|
||||
function errMsg(error: unknown): string {
|
||||
return error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
|
||||
function toPublicJob(job: InternalJob): SpriteJob {
|
||||
return {
|
||||
jobId: job.jobId,
|
||||
kind: job.kind,
|
||||
spriteName: job.spriteName,
|
||||
status: job.status,
|
||||
phase: job.phase,
|
||||
startedAt: job.startedAt,
|
||||
updatedAt: job.updatedAt,
|
||||
finishedAt: job.finishedAt,
|
||||
error: job.error,
|
||||
result: job.result,
|
||||
};
|
||||
}
|
||||
|
||||
export class SpriteJobManager {
|
||||
private readonly jobs = new Map<string, InternalJob>();
|
||||
|
||||
startJob<T extends SpriteJobResult>(options: {
|
||||
kind: SpriteJobKind;
|
||||
spriteName?: string | null;
|
||||
dedupeKey?: string;
|
||||
initialPhase: string;
|
||||
run: (ctx: {
|
||||
setPhase: (phase: string, spriteName?: string) => void;
|
||||
}) => Promise<T>;
|
||||
}): SpriteJob {
|
||||
this.prune();
|
||||
|
||||
if (options.dedupeKey) {
|
||||
for (const existing of this.jobs.values()) {
|
||||
if (existing.status === "running" && existing.dedupeKey === options.dedupeKey) {
|
||||
return toPublicJob(existing);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const job: InternalJob = {
|
||||
jobId: `job_${randomBytes(6).toString("hex")}`,
|
||||
kind: options.kind,
|
||||
spriteName: options.spriteName?.trim() || null,
|
||||
status: "running",
|
||||
phase: options.initialPhase,
|
||||
startedAt: now,
|
||||
updatedAt: now,
|
||||
finishedAt: null,
|
||||
error: null,
|
||||
result: null,
|
||||
dedupeKey: options.dedupeKey,
|
||||
};
|
||||
|
||||
this.jobs.set(job.jobId, job);
|
||||
console.log(
|
||||
`[fly-sprites] job ${job.jobId} (${job.kind}${job.spriteName ? `:${job.spriteName}` : ""}) started: ${job.phase}`,
|
||||
);
|
||||
|
||||
const setPhase = (phase: string, spriteName?: string) => {
|
||||
if (job.status !== "running") return;
|
||||
job.phase = phase;
|
||||
if (spriteName?.trim()) {
|
||||
job.spriteName = spriteName.trim();
|
||||
}
|
||||
job.updatedAt = new Date().toISOString();
|
||||
};
|
||||
|
||||
void options
|
||||
.run({ setPhase })
|
||||
.then((result) => {
|
||||
const finishedAt = new Date().toISOString();
|
||||
job.status = "succeeded";
|
||||
job.phase = "Done";
|
||||
job.result = result;
|
||||
if ("spriteName" in result && result.spriteName) {
|
||||
job.spriteName = result.spriteName;
|
||||
} else if (result.kind === "create_sprite") {
|
||||
job.spriteName = result.sprite.name;
|
||||
}
|
||||
job.updatedAt = finishedAt;
|
||||
job.finishedAt = finishedAt;
|
||||
console.log(
|
||||
`[fly-sprites] job ${job.jobId} (${job.kind}${job.spriteName ? `:${job.spriteName}` : ""}) succeeded`,
|
||||
);
|
||||
})
|
||||
.catch((error) => {
|
||||
const finishedAt = new Date().toISOString();
|
||||
const message = errMsg(error);
|
||||
job.status = "failed";
|
||||
job.phase = "Failed";
|
||||
job.error = message;
|
||||
job.updatedAt = finishedAt;
|
||||
job.finishedAt = finishedAt;
|
||||
console.error(
|
||||
`[fly-sprites] job ${job.jobId} (${job.kind}${job.spriteName ? `:${job.spriteName}` : ""}) failed: ${message}`,
|
||||
);
|
||||
});
|
||||
|
||||
return toPublicJob(job);
|
||||
}
|
||||
|
||||
getJob(jobId: string): SpriteJob | null {
|
||||
const job = this.jobs.get(jobId);
|
||||
return job ? toPublicJob(job) : null;
|
||||
}
|
||||
|
||||
listJobs(): SpriteJob[] {
|
||||
this.prune();
|
||||
return [...this.jobs.values()]
|
||||
.sort((a, b) => b.startedAt.localeCompare(a.startedAt))
|
||||
.map(toPublicJob);
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.jobs.clear();
|
||||
}
|
||||
|
||||
private prune(): void {
|
||||
const now = Date.now();
|
||||
for (const [id, job] of this.jobs.entries()) {
|
||||
if (job.status !== "running" && job.finishedAt) {
|
||||
const age = now - Date.parse(job.finishedAt);
|
||||
if (age > FINISHED_JOB_TTL_MS) {
|
||||
this.jobs.delete(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this.jobs.size <= MAX_STORED_JOBS) return;
|
||||
const finished = [...this.jobs.values()]
|
||||
.filter((j) => j.status !== "running")
|
||||
.sort((a, b) => (a.finishedAt ?? a.updatedAt).localeCompare(b.finishedAt ?? b.updatedAt));
|
||||
for (const job of finished) {
|
||||
if (this.jobs.size <= MAX_STORED_JOBS) break;
|
||||
this.jobs.delete(job.jobId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user