fffleet 2.0.0 → 2.1.1

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
@@ -53,9 +53,25 @@ export interface JobSpecInput {
53
53
  [section: string]: unknown;
54
54
  }
55
55
 
56
- export interface JobSpec extends Required<Omit<JobSpecInput, 'id' | 'ffmpeg'>> {
56
+ /** A validated spec with every default filled in (what parseSpec returns). */
57
+ export interface JobSpec {
58
+ contract: 1;
57
59
  id?: string;
60
+ kind: JobKind;
61
+ type: string;
62
+ class: string;
63
+ priority: number;
64
+ owner: string;
65
+ requires: string[];
66
+ labels: Record<string, string>;
67
+ timeoutMs: number | null;
68
+ stdin: boolean;
69
+ stdout: boolean;
70
+ inputs: Endpoint[];
71
+ outputs: Endpoint[];
58
72
  ffmpeg?: { args: string[]; durationMs: number | null };
73
+ /** Sections of other executor types, kept as submitted. */
74
+ [section: string]: unknown;
59
75
  }
60
76
 
61
77
  export interface Progress {
@@ -142,6 +158,8 @@ export interface ExecutorRuntime {
142
158
  signal: AbortSignal;
143
159
  setState(state: JobState, extra?: object): void;
144
160
  progress(progress: Progress): void;
161
+ /** Reports the newest stderr lines while the job runs (called often; the manager throttles). */
162
+ stderr?(tail: string): void;
145
163
  attachStdin(stdin: Writable | null): void;
146
164
  attachStdout?(stdout: import('node:stream').Readable): void;
147
165
  ffmpegPath?: string;
@@ -215,6 +233,8 @@ export class JobHandle extends EventEmitter {
215
233
  readonly id: string;
216
234
  readonly where: 'local' | 'remote';
217
235
  state: JobState;
236
+ /** The newest stderr lines ffmpeg wrote while the job runs (null until it writes any). */
237
+ stderrTail: string | null;
218
238
  snapshot: JobSnapshot | null;
219
239
  /** Resolves with the final snapshot whatever the outcome; check `state`. */
220
240
  readonly done: Promise<JobSnapshot>;
@@ -227,6 +247,7 @@ export class JobHandle extends EventEmitter {
227
247
  on(event: 'event', listener: (event: JobEvent) => void): this;
228
248
  on(event: 'state', listener: (state: JobState, event: JobEvent) => void): this;
229
249
  on(event: 'progress', listener: (progress: Progress, event: JobEvent) => void): this;
250
+ on(event: 'stderr', listener: (stderrTail: string, event: JobEvent) => void): this;
230
251
  on(event: string | symbol, listener: (...args: any[]) => void): this;
231
252
  }
232
253
 
@@ -282,6 +303,8 @@ export function followJobEvents(opts: {
282
303
 
283
304
  export class JobRecord {
284
305
  constructor(spec: JobSpec & { id: string });
306
+ /** Rebuilds a record from saved fields (state, history, order, ...). */
307
+ static restore(spec: JobSpec & { id: string }, saved: Record<string, unknown>): JobRecord;
285
308
  readonly id: string;
286
309
  readonly final: boolean;
287
310
  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.1",
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;