mirror of
https://github.com/whyour/qinglong.git
synced 2026-09-21 01:32:44 +08:00
43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import type { ChildProcess } from 'child_process';
|
|
import type { LegacyExecutionObservation } from '../ports/legacyExecutionObserver';
|
|
|
|
export interface LegacyProcessObservationOptions {
|
|
now?: () => number;
|
|
logArtifactId?: string;
|
|
}
|
|
|
|
export function observeLegacyChildProcess(
|
|
child: ChildProcess,
|
|
observation: LegacyExecutionObservation,
|
|
options: LegacyProcessObservationOptions = {},
|
|
): void {
|
|
const now = options.now ?? Date.now;
|
|
child.once('spawn', () => {
|
|
const atMs = now();
|
|
observation.spawned({
|
|
atMs,
|
|
...(child.pid === undefined ? {} : { pid: child.pid }),
|
|
...(child.pid === undefined
|
|
? {}
|
|
: { executorHandle: `legacy-local:${child.pid}` }),
|
|
...(options.logArtifactId === undefined
|
|
? {}
|
|
: { logArtifactId: options.logArtifactId }),
|
|
});
|
|
observation.running({ atMs });
|
|
});
|
|
child.on('error', () => {
|
|
observation.startFailed({
|
|
atMs: now(),
|
|
errorCode: 'LEGACY_PROCESS_ERROR',
|
|
});
|
|
});
|
|
child.once('exit', (exitCode, signal) => {
|
|
observation.exited({
|
|
atMs: now(),
|
|
exitCode,
|
|
...(signal === null ? {} : { signal }),
|
|
});
|
|
});
|
|
}
|