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
Workers: a stop from the system ends the worker cleanly
Adarshd85d7402232c
diff --git a/bin/fuobe.mjs b/bin/fuobe.mjsindex c877f3e4..01d96000 100755--- a/bin/fuobe.mjs+++ b/bin/fuobe.mjs@@ -4,6 +4,7 @@import os from "node:os";import readline from "node:readline";import { main } from "./lib/cli.mjs";+import { onSignals, STOP_SIGNALS } from "./lib/signals.mjs";/** Ask on the terminal; `hidden` keeps a pasted token off the screen. */function ask(question, { hidden = false } = {}) {@@ -62,10 +63,9 @@ process.exitCode = await main(process.argv.slice(2), {signal?.addEventListener("abort", () => (clearTimeout(timer), resolve()), { once: true });}),/** Ctrl+C while waiting stops the wait (the job keeps running on the server). */- onInterrupt: (fn) => {- process.on("SIGINT", fn);- return () => process.off("SIGINT", fn);- },+ onInterrupt: (fn) => onSignals(fn),+ /** Ctrl+C, and a stop from the system (SIGTERM): a long-running command ends its own way, cleaning up as it goes. */+ onStop: (fn) => onSignals(fn, STOP_SIGNALS),ask,readStdin,});diff --git a/bin/lib/signals.mjs b/bin/lib/signals.mjsnew file mode 100644index 00000000..9f307c32--- /dev/null+++ b/bin/lib/signals.mjs@@ -0,0 +1,13 @@+// Stop signals for the command line (bin/fuobe.mjs). Ctrl+C (SIGINT) ends a wait. A long-running command, such as+// `fuobe worker run`, also ends its own way on a stop from the system (SIGTERM), so it cleans up before it exits.++/** The signals a long-running command ends on. */+export const STOP_SIGNALS = ["SIGINT", "SIGTERM"];++/** Run `fn` on each of `signals`. Returns the function that stops listening. */+export function onSignals(fn, signals = ["SIGINT"], proc = process) {+ for (const s of signals) proc.on(s, fn);+ return () => {+ for (const s of signals) proc.off(s, fn);+ };+}diff --git a/bin/lib/worker/index.mjs b/bin/lib/worker/index.mjsindex aae7eeeb..7f2410f5 100644--- a/bin/lib/worker/index.mjs+++ b/bin/lib/worker/index.mjs@@ -40,7 +40,9 @@ export async function worker(args, io) {return 2;}const ctrl = new AbortController();- const off = io.onInterrupt?.(() => ctrl.abort());+ // Ctrl+C, or a stop from the system: the worker stops its ComfyUI and cleans up before it exits (finished results+ // stay for its next start).+ const off = (io.onStop ?? io.onInterrupt)?.(() => ctrl.abort());say(`Fuobe worker for ${cfg.site}. ${cfg.port ? `Its own ComfyUI uses port ${cfg.port}` : "Its own ComfyUI takes a free local port at each start"}. Ctrl+C stops it.`);let why;try {diff --git a/docs/gpu-workers.md b/docs/gpu-workers.mdindex e97fe7ec..30907bc4 100644--- a/docs/gpu-workers.md+++ b/docs/gpu-workers.md@@ -273,7 +273,9 @@ The worker keeps its jobs going by itself:found yet. A drive that comes back after a restart is noticed, and the studio gets the new list.- **When the studio stops the worker.** If the studio no longer knows the worker's token, `fuobe worker run` ends withexit code 3; any other stop that you did not ask for ends with exit code 1. The sign-in task that- `fuobe worker install` adds then starts the worker again after a minute. Ctrl+C ends with 0.+ `fuobe worker install` adds then starts the worker again after a minute. Ctrl+C ends with 0. On Ctrl+C, and on a+ stop from the system (SIGTERM), the worker stops its ComfyUI and empties its folders before it ends. Kept results+ stay for its next start.### The protocoldiff --git a/tests/worker-parts.test.ts b/tests/worker-parts.test.tsindex c5637a7e..3e75dd10 100644--- a/tests/worker-parts.test.ts+++ b/tests/worker-parts.test.ts@@ -1,6 +1,7 @@/*** The worker program's smaller parts: its one retry policy for calls to the studio (bin/lib/worker/retry.mjs), keeping* the PC awake while it works (awake.mjs), how it makes clips and sound again (media.mjs), and the studio's address.+ * It also covers the stop signals the command line ends on (bin/lib/signals.mjs).*/import { EventEmitter } from "node:events";import { describe, expect, it } from "vitest";@@ -8,6 +9,7 @@ import { helperCommand, StayAwake } from "../bin/lib/worker/awake.mjs";import { encodeArgs, mediaKind } from "../bin/lib/worker/media.mjs";import { RETRY, retrying } from "../bin/lib/worker/retry.mjs";import { siteProblem } from "../bin/lib/worker/run.mjs";+import { onSignals, STOP_SIGNALS } from "../bin/lib/signals.mjs";/** A clock that moves only when the policy sleeps. */function clock() {@@ -186,3 +188,23 @@ describe("the studio's address", () => {expect(siteProblem("studio")).toMatch(/web address/);});});++describe("stop signals", () => {+ it("a long-running command ends its own way on Ctrl+C and on a stop from the system; a wait only on Ctrl+C", () => {+ const proc = new EventEmitter();+ const seen: string[] = [];+ const off = onSignals(() => seen.push("stop"), STOP_SIGNALS, proc as unknown as NodeJS.Process);+ proc.emit("SIGTERM");+ proc.emit("SIGINT");+ expect(seen).toEqual(["stop", "stop"]);+ off();+ proc.emit("SIGTERM");+ expect(seen).toHaveLength(2);+ expect(proc.listenerCount("SIGINT") + proc.listenerCount("SIGTERM")).toBe(0);+ const offWait = onSignals(() => seen.push("wait"), undefined, proc as unknown as NodeJS.Process);+ proc.emit("SIGTERM");+ proc.emit("SIGINT");+ expect(seen.slice(2)).toEqual(["wait"]);+ offWait();+ });+});