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.

Billing: a run on the workers reports only their measured time

Adarsh5f895bdacd6e
diff --git a/docs/billing-hook.md b/docs/billing-hook.md
index 4e97b6d3..bf4513c9 100644
--- a/docs/billing-hook.md
+++ b/docs/billing-hook.md
@@ -120,8 +120,8 @@ every task the workers that may run this one would serve first, whatever its mod
many jobs or chain steps it has (`waiting`: none of its rows has started).
A report's `gpuMs` is the time the workers' ComfyUI spent running the row's jobs, as they measured it (no waits, no
files, no upload). A job that ran more than once counts once, for the attempt whose result was kept. `device` is the
-GPU's name. Elsewhere `gpuMs` is the row's time from its start to its end, and
-`device` is null.
+GPU's name. A row on the workers none of whose jobs finished there has `gpuMs` and `device` null: its time in line
+never counts. Elsewhere `gpuMs` is the row's time from its start to its end, and `device` is null.
Example (arbitrary numbers):
diff --git a/src/server/billing-outbox.ts b/src/server/billing-outbox.ts
index d9c8e3cf..ba943fa6 100644
--- a/src/server/billing-outbox.ts
+++ b/src/server/billing-outbox.ts
@@ -35,6 +35,8 @@ interface DoneRow {
*/
exec_ms: number | null;
exec_device: string | null;
+ /** 1 when the run's jobs went to the workers, whether or not one of them finished there. */
+ on_workers: number;
}
/** Where a row stands in the output check, for its report: hidden, never checked in time, or nothing to say. */
@@ -59,7 +61,8 @@ export function queueReports(now = Date.now(), limit = 200): number {
(SELECT state FROM output_checks c WHERE c.generation_id = g.id AND c.state IN ('hidden', 'unchecked') LIMIT 1) AS check_state,
(SELECT SUM(j.exec_ms) FROM worker_jobs j WHERE j.generation_id = g.id AND j.event = 'finished' AND j.exec_ms IS NOT NULL
AND j.id = (SELECT MAX(k.id) FROM worker_jobs k WHERE k.task_id = j.task_id AND k.event = 'finished' AND k.exec_ms IS NOT NULL)) AS exec_ms,
- (SELECT j.device FROM worker_jobs j WHERE j.generation_id = g.id AND j.event = 'finished' AND j.device IS NOT NULL ORDER BY j.id DESC LIMIT 1) AS exec_device
+ (SELECT j.device FROM worker_jobs j WHERE j.generation_id = g.id AND j.event = 'finished' AND j.device IS NOT NULL ORDER BY j.id DESC LIMIT 1) AS exec_device,
+ EXISTS (SELECT 1 FROM relay_tasks t WHERE t.generation_id = g.id AND t.pools IS NOT NULL) AS on_workers
FROM generations g LEFT JOIN generation_runs r ON r.id = g.id
WHERE json_extract(g.request_json, '$.hold') IS NOT NULL
AND g.status IN ('succeeded', 'failed', 'canceled')
@@ -76,8 +79,9 @@ export function queueReports(now = Date.now(), limit = 200): number {
for (const r of rows) {
const n = (count.get(r.hold) as { n: number }).n;
// A run on the workers: the time their ComfyUI spent on its jobs, as they measured it (no waits, no files, no
- // upload). Anything else: from its start to its end, as before.
- const gpuMs = r.exec_ms !== null ? r.exec_ms : r.started_at && r.completed_at && r.completed_at > r.started_at ? r.completed_at - r.started_at : null;
+ // upload), never its time in line; null when none of its jobs finished there. Anything else: from its start to its
+ // end, as before.
+ const gpuMs = r.on_workers || r.exec_ms !== null ? r.exec_ms : r.started_at && r.completed_at && r.completed_at > r.started_at ? r.completed_at - r.started_at : null;
const device = r.exec_device ?? null;
const outputs = (() => {
try {
diff --git a/tests/worker-queue.test.ts b/tests/worker-queue.test.ts
index b4d12d6b..2a375b6a 100644
--- a/tests/worker-queue.test.ts
+++ b/tests/worker-queue.test.ts
@@ -206,6 +206,19 @@ describe("the workers' measured run reaches the billing service", () => {
expect(report.gpuMs).toBe(42_000);
expect(report.device).toBe("Test GPU B");
});
+
+ it("a run whose jobs never finished on a worker reports no time, never its time in line", () => {
+ const now = Date.now();
+ const t = accountTask();
+ db().prepare("UPDATE generations SET status = 'failed', error = 'x', completed_at = ?, request_json = ? WHERE id = ?").run(now, JSON.stringify({ account: accountId, place: "pool", hold: `hold-${t.id}` }), t.generationId);
+ // It waited in line for 40 minutes: its start and end are far apart, and no worker measured anything.
+ db().prepare("INSERT OR REPLACE INTO generation_runs (id, submitted_at, started_at) VALUES (?, ?, ?)").run(t.generationId, now - 40 * MIN, now - 40 * MIN);
+ tasks.logWorkerJob("claimed", { id: t.id, generation_id: t.generationId, principal: `acct:${accountId}`, lane: "small" }, "w-1", now - 5 * MIN);
+ queueReports(now);
+ const report = JSON.parse((db().prepare("SELECT payload_json FROM billing_outbox WHERE key = ?").get(t.generationId) as { payload_json: string }).payload_json) as { gpuMs: number | null; device: string | null };
+ expect(report.gpuMs).toBeNull();
+ expect(report.device).toBeNull();
+ });
});
describe("the workers' log", () => {