n8n/packages/cli/src/runners/node-process-oom-detector.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
849 B
TypeScript
Raw Normal View History

import * as a from 'node:assert/strict';
import type { ChildProcess } from 'node:child_process';
/**
* Class to monitor a nodejs process and detect if it runs out of
* memory (OOMs).
*/
export class NodeProcessOomDetector {
public get didProcessOom() {
return this._didProcessOom;
}
private _didProcessOom = false;
constructor(processToMonitor: ChildProcess) {
this.monitorProcess(processToMonitor);
}
private monitorProcess(processToMonitor: ChildProcess) {
a.ok(processToMonitor.stderr, "Can't monitor a process without stderr");
processToMonitor.stderr.on('data', this.onStderr);
processToMonitor.once('exit', () => {
processToMonitor.stderr?.off('data', this.onStderr);
});
}
private onStderr = (data: Buffer) => {
if (data.includes('JavaScript heap out of memory')) {
this._didProcessOom = true;
}
};
}