fffleet 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,6 +26,8 @@ await job.cancel();
26
26
  const final = await job.done; // final snapshot: state, error, outputs, exitCode, stderrTail
27
27
  ```
28
28
 
29
+ While a job runs, `job.on('stderr', tail => ...)` and `job.stderrTail` give ffmpeg's newest stderr lines, which is how you diagnose a stream job that never exits.
30
+
29
31
  `job.where` is `'local'` or `'remote'`. `fleet.capabilities()` reports slots and capabilities.
30
32
 
31
33
  The job contract, routes and error codes are documented in the [repository README](https://github.com/jsilvanus/fffleet#the-job-contract-v1). The building blocks (`JobManager`, `validateSpec`, `followJobEvents`, …) are exported too. `fffleet/server` holds the HTTP API that the worker and the orchestrator use.
package/index.d.ts CHANGED
@@ -142,6 +142,8 @@ export interface ExecutorRuntime {
142
142
  signal: AbortSignal;
143
143
  setState(state: JobState, extra?: object): void;
144
144
  progress(progress: Progress): void;
145
+ /** Reports the newest stderr lines while the job runs (called often; the manager throttles). */
146
+ stderr?(tail: string): void;
145
147
  attachStdin(stdin: Writable | null): void;
146
148
  attachStdout?(stdout: import('node:stream').Readable): void;
147
149
  ffmpegPath?: string;
@@ -215,6 +217,8 @@ export class JobHandle extends EventEmitter {
215
217
  readonly id: string;
216
218
  readonly where: 'local' | 'remote';
217
219
  state: JobState;
220
+ /** The newest stderr lines ffmpeg wrote while the job runs (null until it writes any). */
221
+ stderrTail: string | null;
218
222
  snapshot: JobSnapshot | null;
219
223
  /** Resolves with the final snapshot whatever the outcome; check `state`. */
220
224
  readonly done: Promise<JobSnapshot>;
@@ -227,6 +231,7 @@ export class JobHandle extends EventEmitter {
227
231
  on(event: 'event', listener: (event: JobEvent) => void): this;
228
232
  on(event: 'state', listener: (state: JobState, event: JobEvent) => void): this;
229
233
  on(event: 'progress', listener: (progress: Progress, event: JobEvent) => void): this;
234
+ on(event: 'stderr', listener: (stderrTail: string, event: JobEvent) => void): this;
230
235
  on(event: string | symbol, listener: (...args: any[]) => void): this;
231
236
  }
232
237
 
@@ -282,6 +287,8 @@ export function followJobEvents(opts: {
282
287
 
283
288
  export class JobRecord {
284
289
  constructor(spec: JobSpec & { id: string });
290
+ /** Rebuilds a record from saved fields (state, history, order, ...). */
291
+ static restore(spec: JobSpec & { id: string }, saved: Record<string, unknown>): JobRecord;
285
292
  readonly id: string;
286
293
  readonly final: boolean;
287
294
  state: JobState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fffleet",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Run ffmpeg jobs locally, on one worker or across a pool, through one client and one job contract",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/client.js CHANGED
@@ -10,7 +10,7 @@ import { followJobEvents } from './sse.js';
10
10
 
11
11
  /**
12
12
  * A handle to one submitted job, local or remote.
13
- * Events: 'event' (every JobEvent), 'state' (state, event), 'progress' (progress, event).
13
+ * Events: 'event' (every JobEvent), 'state' (state, event), 'progress' (progress, event), 'stderr' (newest ffmpeg stderr lines, event).
14
14
  * `done` resolves with the final snapshot whatever the outcome; check `state`.
15
15
  */
16
16
  export class JobHandle extends EventEmitter {
@@ -22,6 +22,8 @@ export class JobHandle extends EventEmitter {
22
22
  this.state = 'queued';
23
23
  /** @type {import('./types.js').JobSnapshot | null} */
24
24
  this.snapshot = null;
25
+ /** The newest stderr lines ffmpeg wrote while the job runs (null until it writes any). */
26
+ this.stderrTail = null;
25
27
  let resolve, reject;
26
28
  /** @type {Promise<import('./types.js').JobSnapshot>} */
27
29
  this.done = new Promise((res, rej) => {
@@ -36,6 +38,10 @@ export class JobHandle extends EventEmitter {
36
38
  _event(event) {
37
39
  this.emit('event', event);
38
40
  if (event.progress) this.emit('progress', event.progress, event);
41
+ if (event.stderrTail) {
42
+ this.stderrTail = event.stderrTail;
43
+ this.emit('stderr', event.stderrTail, event);
44
+ }
39
45
  if (event.state !== this.state) {
40
46
  this.state = event.state;
41
47
  this.emit('state', event.state, event);
package/src/executor.js CHANGED
@@ -241,6 +241,7 @@ function runProcess(cmd, args, spec, rt) {
241
241
  child.stderr.setEncoding('utf8');
242
242
  child.stderr.on('data', d => {
243
243
  tail = (tail + d).slice(-STDERR_TAIL_BYTES);
244
+ rt.stderr?.(tail.trim());
244
245
  });
245
246
  if (child.stdin) {
246
247
  child.stdin.on('error', () => {});
@@ -248,6 +249,13 @@ function runProcess(cmd, args, spec, rt) {
248
249
  }
249
250
 
250
251
  const onAbort = () => {
252
+ if (process.platform === 'win32') {
253
+ // SIGTERM is TerminateProcess here, which stops only the process we started. An ffmpeg started
254
+ // through a launcher shim (Chocolatey, Scoop) would keep running with our pipes open and the
255
+ // job would never end, so stop the whole tree.
256
+ killTree(child);
257
+ return;
258
+ }
251
259
  child.kill('SIGTERM');
252
260
  killTimer = setTimeout(() => child.kill('SIGKILL'), KILL_GRACE_MS);
253
261
  killTimer.unref?.();
@@ -273,3 +281,12 @@ function runProcess(cmd, args, spec, rt) {
273
281
  });
274
282
  });
275
283
  }
284
+
285
+ /** Windows: ends a process and everything it started. Falls back to killing just the child. */
286
+ function killTree(child) {
287
+ try {
288
+ spawn('taskkill', ['/pid', String(child.pid), '/T', '/F'], { stdio: 'ignore', windowsHide: true }).once('error', () => child.kill());
289
+ } catch {
290
+ child.kill();
291
+ }
292
+ }
@@ -224,6 +224,7 @@ export class JobManager extends EventEmitter {
224
224
  }
225
225
 
226
226
  let lastProgressAt = 0;
227
+ let lastStderrAt = 0;
227
228
  const workDir = join(this.workRoot, record.id.replace(/[^A-Za-z0-9._-]/g, '_'));
228
229
  const runtime = {
229
230
  workDir,
@@ -245,6 +246,16 @@ export class JobManager extends EventEmitter {
245
246
  lastProgressAt = now;
246
247
  if (!abort.signal.aborted) record.push({ progress: p });
247
248
  },
249
+ // The newest stderr lines of a running job, so a failing stream job can be diagnosed before it ends.
250
+ stderr: tail => {
251
+ const now = Date.now();
252
+ if (now - lastStderrAt < this.progressIntervalMs) {
253
+ record.stderrTail = tail;
254
+ return;
255
+ }
256
+ lastStderrAt = now;
257
+ if (!abort.signal.aborted) record.push({ stderrTail: tail });
258
+ },
248
259
  attachStdin: s => {
249
260
  run.stdin = s;
250
261
  },
package/src/job-record.js CHANGED
@@ -51,6 +51,25 @@ export class JobRecord {
51
51
  return isFinal(this.state);
52
52
  }
53
53
 
54
+ /**
55
+ * Rebuilds a record from a saved one (an orchestrator restoring its jobs after a restart).
56
+ * Subscribers start from the saved history.
57
+ * @param {import('./types.js').JobSpec & { id: string }} spec
58
+ * @param {Record<string, any>} saved
59
+ */
60
+ static restore(spec, saved) {
61
+ const job = new JobRecord(spec);
62
+ for (const key of ['state', 'workerId', 'createdAt', 'startedAt', 'finishedAt', 'progress', 'exitCode', 'error', 'outputs', 'stderrTail', 'seq']) {
63
+ if (saved[key] !== undefined) job[key] = saved[key];
64
+ }
65
+ if (Array.isArray(saved.history) && saved.history.length) job.history = saved.history;
66
+ if (Number.isInteger(saved.order)) {
67
+ job.order = saved.order;
68
+ order = Math.max(order, saved.order);
69
+ }
70
+ return job;
71
+ }
72
+
54
73
  /**
55
74
  * Appends an event. A state change or a final result updates the snapshot too.
56
75
  * Events after the final one are ignored, so a job is final exactly once.
@@ -63,6 +82,7 @@ export class JobRecord {
63
82
  if (state === 'running' && !this.startedAt) this.startedAt = now;
64
83
  if (fields.workerId !== undefined) this.workerId = fields.workerId;
65
84
  if (fields.progress) this.progress = fields.progress;
85
+ if (fields.stderrTail !== undefined && !isFinal(state)) this.stderrTail = fields.stderrTail;
66
86
  this.state = state;
67
87
  if (isFinal(state)) {
68
88
  this.finishedAt = now;