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.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
import "server-only";
import type { GenerationRequest } from "@/lib/catalog/types";
import { accountById } from "./auth";
import { accountFacts, billingConfigured, forgetBalance, HOLD_READY_MS, keptPolicy, policy as billingPolicy, policyGiveUpMs, reserve, serviceAnsweredAt, type Policy } from "./billing";
import { db } from "./db";
import { cancelGeneration, failWaiting, previewGeneration, restampRows, sendHeld } from "./generations";
import { queueFacts, runOnPool } from "./hosted";
import { heldStamp, moderateRun, renewalAt, type ReserveFacts } from "./hosted-run";
import { dropGone, dropRun, endRunLease, expiredReadyHolds, heldRunHeads, leaseRun, runHolds, setRunHold, startReason, takeRun, type HoldRow } from "./pool-holds";
import { lineRoom, poolRunCounts } from "./pool-limits";
import { stampOf, withoutStamp, withRunStamp } from "./run-stamp";
/**
* The release loop of runs made but waiting to enter the line on this install's GPUs (pool-holds.ts). It runs in the
* generations runner's turn (every app process, every 1.5 s), only while hosted runs are on and something waits:
*
* 1. A run waits until its `start_at` (the day's GPU time renews, an hourly limit, free generations renew).
* 2. A run the billing or moderation service didn't answer for is checked and reserved, under its press's own request id
* (so a hold an unanswered try made at the press is the one it gets): tried again with a growing wait (1 s, doubling,
* at most 30 s), and at once after the service answers any call. An answer with a later start waits for it; a refusal
* ends the run in the service's words (its tile says it plainly).
* 3. It enters when the account has room among its runs going (the policy's runs at once and in line), its presses in the
* order they were made; a run that counted already (one cut off by a deploy or a crash) needs no room.
* 4. It enters the line at its press time (the system's reasons) or at the back (the account's own), and its rows are
* sent from what is stored, as a run is sent again after its GPU lost it.
*
* Each run is leased to one app process while it is released (pool-holds.ts leaseRun), so two processes during a deploy
* never release one run twice, and a process that stops mid-way leaves it to the other once its lease ends.
*/
/** How often a run held until the service says ready asks again. */
export const READY_ASK_MS = 5000;
/** Service calls one turn of the loop makes at most: a service that hangs holds up nothing else for long. */
const SERVICE_TRIES_PER_TURN = 3;
/** How long a run waiting for a service waits before it asks again (ms): 1 s, doubling, at most 30 s. */
export const holdBackoffMs = (tries: number): number => Math.min(30_000, 1000 * 2 ** Math.min(Math.max(0, tries), 5));
/** After how long runs waiting for the billing service are said in the owner's log (the service can't say so itself). */
export const SERVICE_WAIT_SAID_MS = 5 * 60_000;
/** The admin log's action for runs that wait for the billing or moderation service to answer (actor "system"). */
export const SERVICE_WAIT_ACTION = "runs.waiting";
declare global {
var __fuobeHoldServiceSaid: number | undefined;
}
/**
* The owner's logs, at most every SERVICE_WAIT_SAID_MS: how many runs have waited longer than that for the billing or
* moderation service to answer (counts only: no prompt, no account). The server log says it from each app process; the
* admin log keeps one row (SERVICE_WAIT_ACTION) for all of them. A run waiting for a time (free generations that renew)
* counts from that time, and one waiting because the service said runs are paused (the owner's own switch) doesn't count.
*/
function sayServiceWait(heads: HoldRow[], now: number): void {
const since = (h: HoldRow) => Math.max(h.pressed_at, h.start_at ?? 0);
const long = heads.filter((h) => (!h.reserved || !h.checked) && !h.paused && now - since(h) > SERVICE_WAIT_SAID_MS);
if (!long.length || now - (globalThis.__fuobeHoldServiceSaid ?? 0) < SERVICE_WAIT_SAID_MS) return;
globalThis.__fuobeHoldServiceSaid = now;
const oldest = Math.round((now - Math.min(...long.map(since))) / 60_000);
const billing = long.filter((h) => !h.reserved).length;
const moderation = long.filter((h) => !h.checked).length;
console.warn(`[fuobe] runs waiting for the billing or moderation service: ${long.length} (oldest ${oldest} min)`);
try {
const d = db();
const said = d.prepare("SELECT 1 FROM admin_audit WHERE action = ? AND at > ? LIMIT 1").get(SERVICE_WAIT_ACTION, now - SERVICE_WAIT_SAID_MS);
if (!said) {
d.prepare("INSERT INTO admin_audit (at, actor, token_id, token_name, action, target, detail_json, status) VALUES (?, 'system', NULL, NULL, ?, NULL, ?, 200)").run(
now,
SERVICE_WAIT_ACTION,
JSON.stringify({ waiting: long.length, billing, moderation, oldestMin: oldest }),
);
}
} catch (e) {
console.warn(`[fuobe] Couldn't write the admin log: ${(e as Error).message}`);
}
}
/**
* The account's policy for its room among its runs going (pool-limits.ts lineRoom: held runs don't count), read once a
* turn. Null when it isn't known (the service is quiet and nothing is kept, here or across restarts): its runs wait, as
* they are.
*/
async function policyFor(accountId: string, kept: Map<string, Policy | null>): Promise<Policy | null> {
if (kept.has(accountId)) return kept.get(accountId)!;
const p = (await billingPolicy(accountId)) ?? keptPolicy(accountId);
kept.set(accountId, p);
return p;
}
/** Ask again later: one more try, and the next one after a growing wait (`paused`: the service said runs are paused). */
function later(h: HoldRow, now: number, paused?: boolean): void {
setRunHold(h.run_id, { tries: h.tries + 1, tried_at: now, next_at: now + holdBackoffMs(h.tries), ...(paused !== undefined && { paused: paused ? 1 : 0 }) });
}
/** A service refused the run once it answered: its rows end in its words (the tile says it plainly), its holds go. */
function end(h: HoldRow, ids: string[], error: string): "ended" {
failWaiting(ids, error || "This run couldn't be made.");
dropRun(h.run_id);
return "ended";
}
/**
* The checks a run that waited for the services still needs: the moderation service's, then the billing service's hold.
* "ready" once both are done; "wait" for a later start or another try; "down" when the service gave no answer at all
* (no more calls this turn); "ended" when one refused it.
*/
async function serviceStep(h: HoldRow, now: number): Promise<"ready" | "wait" | "down" | "ended"> {
const rows = runHolds(h.run_id);
const ids = rows.map((r) => r.generation_id);
const account = accountById(h.account_id);
if (!account) return "wait";
const first = rows.find((r) => r.job !== null) ?? rows[0];
const row = db().prepare("SELECT request_json FROM generations WHERE id = ?").get(first.generation_id) as { request_json: string } | undefined;
if (!row) return "wait";
const stored = JSON.parse(row.request_json) as GenerationRequest;
if (!h.checked) {
const req = withoutStamp(stored) as GenerationRequest;
let planned: ReturnType<typeof previewGeneration>;
try {
planned = runOnPool(account.id, () => withRunStamp(stampOf(stored), () => previewGeneration(req)));
} catch (e) {
return end(h, ids, (e as Error).message);
}
const mod = await moderateRun(account, req, planned, "pool");
if (!mod.ok) {
if (mod.refusal.body.code !== "paused") return end(h, ids, mod.refusal.body.error);
later(h, now);
return "down";
}
if (mod.checkOutputs) restampRows(ids, { checkOutputs: true });
setRunHold(h.run_id, { checked: 1 });
}
if (!h.reserved) {
if (!billingConfigured()) {
setRunHold(h.run_id, { reserved: 1, paused: 0 });
return "ready";
}
const facts = h.facts_json ? (JSON.parse(h.facts_json) as ReserveFacts) : null;
if (!facts || !h.request_id) return end(h, ids, "");
const queue = queueFacts(account.id, facts.workflowId);
const a = await reserve({ requestId: h.request_id, account: accountFacts(account), run: facts.run, queue, shown: facts.shown, timing: facts.timing }, { forMs: 0 });
if (!a) {
setRunHold(h.run_id, { reserved: 1, paused: 0 });
return "ready";
}
if (!a.ok) {
// No answer (or one that may have made a hold: the same request id gets it next time), or runs paused there.
if (a.code === "paused" || a.down || a.unsure) {
later(h, now, a.code === "paused" && !a.down);
return a.down ? "down" : "wait";
}
// Still held until the service says ready: asked again later, calmly (it isn't the service that is slow).
if (a.hold === true && h.ready_json) {
setRunHold(h.run_id, { tries: h.tries + 1, tried_at: now, next_at: now + READY_ASK_MS });
return "wait";
}
// No free generations left until they renew: it waits for them, and reserves then.
const renews = await renewalAt(a, account, facts.run, queue, now);
if (renews !== null) {
setRunHold(h.run_id, { reason: "credits", start_at: renews, rank_rule: "release", tries: 0, next_at: null, paused: 0 });
return "wait";
}
return end(h, ids, a.text);
}
forgetBalance(account.id);
restampRows(ids, heldStamp(a, policyGiveUpMs(await billingPolicy(account.id))));
const at = Number(a.startAt);
if (Number.isFinite(at) && at > Date.now()) {
// Its hold is made, and it starts later (the day's GPU time, an hourly limit).
const reason = startReason(at);
setRunHold(h.run_id, { reserved: 1, paused: 0, reason, start_at: at, ...(reason === "hour" && { rank_rule: "release" as const }) });
return "wait";
}
setRunHold(h.run_id, { reserved: 1, paused: 0 });
}
return "ready";
}
/**
* One turn of the release loop: every held run whose reason cleared enters the line. Returns how many runs entered.
* Holds whose rows no longer wait (stopped, deleted) go first.
*/
export async function releaseHeldRuns(now = Date.now()): Promise<number> {
// Held until ready for longer than HOLD_READY_MS: cancelled quietly (no failure on the tile, nothing charged).
for (const run of expiredReadyHolds(HOLD_READY_MS, now)) {
const ids = runHolds(run).map((r) => r.generation_id);
dropRun(run);
for (const id of ids) await cancelGeneration(id).catch(() => undefined);
}
dropGone();
const heads = heldRunHeads();
if (!heads.length) return 0;
sayServiceWait(heads, now);
const policies = new Map<string, Policy | null>();
// Accounts whose earlier press couldn't enter this turn (no room, or another app process is releasing it): their later
// presses wait too, so they enter in the order they were made.
const blocked = new Set<string>();
let released = 0;
let serviceCalls = 0;
let serviceDown = false;
for (const h of heads) {
// Not before its time.
if (h.start_at !== null && h.start_at > now) continue;
if (!h.counted && blocked.has(h.account_id)) continue;
const needsService = !h.reserved || !h.checked;
if (needsService) {
if (serviceDown || serviceCalls >= SERVICE_TRIES_PER_TURN) continue;
const due = h.next_at === null || h.next_at <= now || (h.tried_at !== null && serviceAnsweredAt() > h.tried_at);
if (!due) continue;
}
if (!leaseRun(h.run_id, now)) {
blocked.add(h.account_id);
continue;
}
try {
if (needsService) {
serviceCalls++;
const step = await serviceStep(h, now);
if (step === "down") serviceDown = true;
if (step !== "ready") continue;
}
let p: Policy | null = null;
if (!h.counted) {
p = await policyFor(h.account_id, policies);
// No policy to count by: it waits as it is, its tile unchanged.
if (!p) {
blocked.add(h.account_id);
continue;
}
if (lineRoom(p, poolRunCounts(h.account_id)) <= 0) {
// Its own reason cleared, but the account's runs going fill its room: it waits behind them, as its tile says.
if (h.reason !== "mine") setRunHold(h.run_id, { reason: "mine", start_at: null, paused: 0 });
blocked.add(h.account_id);
continue;
}
}
const limits = p;
// The room is counted again in the step that takes the run (another app process may have released one meanwhile).
const taken = takeRun(h.run_id, Date.now(), limits ? () => lineRoom(limits, poolRunCounts(h.account_id)) > 0 : undefined);
if (!taken) {
if (!h.counted) blocked.add(h.account_id);
continue;
}
for (const s of taken.send) sendHeld(s.id, s.job, taken.rankAt);
released++;
} finally {
endRunLease(h.run_id);
}
}
return released;
}