Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Workers: the queue block, and three small safety fixes

Adarsh5cd47ed02ffe
diff --git a/src/app/api/admin/workers/download/route.ts b/src/app/api/admin/workers/download/route.ts
index 33fd0d36..34acb698 100644
--- a/src/app/api/admin/workers/download/route.ts
+++ b/src/app/api/admin/workers/download/route.ts
@@ -34,6 +34,7 @@ export async function GET(request: Request) {
if (fs.existsSync(workerDir)) {
const walk = (dir: string, prefix: string) => {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
+ if (entry.isSymbolicLink()) continue;
const rel = `${prefix}/${entry.name}`;
if (entry.isDirectory()) walk(path.join(dir, entry.name), rel);
else files.push({ name: rel, data: fs.readFileSync(path.join(dir, entry.name)) });
diff --git a/src/app/api/generations/[id]/route.ts b/src/app/api/generations/[id]/route.ts
index c1d8d8b2..0df61598 100644
--- a/src/app/api/generations/[id]/route.ts
+++ b/src/app/api/generations/[id]/route.ts
@@ -3,12 +3,15 @@ import { getGeneration, setFavorite } from "@/server/generations";
import { deleteForever, setPinned, setReview, trashGenerations } from "@/server/library";
import { isReviewStatus } from "@/lib/library";
import { requestAccount } from "@/server/auth";
+import { queueOfGeneration } from "@/server/computer/pool";
export async function GET(_req: Request, ctx: RouteContext<"/api/generations/[id]">) {
await connection();
const { id } = await ctx.params;
const g = getGeneration(id);
- return g ? Response.json(g) : Response.json({ error: "Not found" }, { status: 404 });
+ if (!g) return Response.json({ error: "Not found" }, { status: 404 });
+ const queue = queueOfGeneration(id);
+ return Response.json(queue ? { ...g, queue } : g);
}
/** Favorite, pin (`pinned`) or set the review status (`review`, null clears it). */
diff --git a/src/server/computer/pool.ts b/src/server/computer/pool.ts
index 60c121a1..e3db034a 100644
--- a/src/server/computer/pool.ts
+++ b/src/server/computer/pool.ts
@@ -157,7 +157,9 @@ export function workerBeat(w: WorkerRow, b: WorkerBeat, now = Date.now()): BeatA
const state = workerState(fresh, now);
d.prepare("UPDATE relay_workers SET state = ? WHERE id = ?").run(state.state, w.id);
const h = hoursAt(hoursOf(fresh), now);
- const waiting = (d.prepare("SELECT COUNT(*) AS n FROM relay_tasks WHERE pools IS NOT NULL AND state = 'waiting' AND (',' || pools || ',') LIKE ?").get(`%,${fresh.pool},%`) as { n: number }).n;
+ // Pool names are validated at creation (/^[a-z0-9-]{1,32}$/), so they never contain commas. INSTR on the
+ // CSV avoids the LIKE-wildcard class of risk (%, _) entirely.
+ const waiting = (d.prepare("SELECT COUNT(*) AS n FROM relay_tasks WHERE pools IS NOT NULL AND state = 'waiting' AND INSTR(',' || pools || ',', ?) > 0").get(`,${fresh.pool},`) as { n: number }).n;
return {
runner: eligible(fresh, now),
heldElsewhere: false,
@@ -295,8 +297,8 @@ function beforeWindowEnds(t: TaskRow, w: WorkerRow, now: number): boolean {
function pick(w: WorkerRow, now: number): TaskRow | null {
const rows = db()
- .prepare("SELECT * FROM relay_tasks WHERE pools IS NOT NULL AND state = 'waiting' AND stop = 0 AND (',' || pools || ',') LIKE ? ORDER BY rank_at LIMIT ?")
- .all(`%,${w.pool},%`, LOOK_AT) as TaskRow[];
+ .prepare("SELECT * FROM relay_tasks WHERE pools IS NOT NULL AND state = 'waiting' AND stop = 0 AND INSTR(',' || pools || ',', ?) > 0 ORDER BY rank_at LIMIT ?")
+ .all(`,${w.pool},`, LOOK_AT) as TaskRow[];
const ok = rows.filter((t) => fits(t, w, now) && retryOk(t, w, now) && beforeWindowEnds(t, w, now));
if (!ok.length) return null;
const warm = w.warm_lane ? ok.find((t) => t.lane === w.warm_lane) : undefined;
@@ -399,6 +401,12 @@ export function poolFields(workflowId: string, o: { account?: string; boostMs?:
};
}
+/** The queue block for GET /api/generations/<id>: the task's place in its lane and the estimated wait. Null when no pool task waits. */
+export function queueOfGeneration(generationId: string, now = Date.now()): { place: number; etaSeconds: number | null } | null {
+ const t = db().prepare("SELECT * FROM relay_tasks WHERE generation_id = ? AND pools IS NOT NULL AND state = 'waiting' LIMIT 1").get(generationId) as TaskRow | undefined;
+ return t ? placeInLine(t, now) : null;
+}
+
/** FUOBE_POOL_WAIT_MIN: how long a task for workers waits before it fails (30 minutes). */
export const poolWaitMs = () => {
const m = Number(process.env.FUOBE_POOL_WAIT_MIN);
diff --git a/src/server/computer/workers.ts b/src/server/computer/workers.ts
index 3cfe6f5b..4a0f93a2 100644
--- a/src/server/computer/workers.ts
+++ b/src/server/computer/workers.ts
@@ -205,9 +205,8 @@ interface PairingRow {
}
function newCode(): string {
- const bytes = crypto.randomBytes(8);
let s = "";
- for (let i = 0; i < 8; i++) s += CODE_ALPHABET[bytes[i] % CODE_ALPHABET.length];
+ for (let i = 0; i < 8; i++) s += CODE_ALPHABET[crypto.randomInt(CODE_ALPHABET.length)];
return `${s.slice(0, 4)}-${s.slice(4)}`;
}