mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-10 12:27:31 -08:00
35 lines
849 B
TypeScript
35 lines
849 B
TypeScript
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;
|
|
}
|
|
};
|
|
}
|