From c90f121711d39b7140fc038e9a1e8b371f82764c Mon Sep 17 00:00:00 2001 From: Rafa Mel Date: Mon, 6 May 2019 13:05:17 +0200 Subject: [PATCH] feat(utils/ps-manager): adds child processes manager --- src/utils/ps-manager.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/utils/ps-manager.ts diff --git a/src/utils/ps-manager.ts b/src/utils/ps-manager.ts new file mode 100644 index 0000000..0e627ea --- /dev/null +++ b/src/utils/ps-manager.ts @@ -0,0 +1,30 @@ +import globals from '~/globals'; +import { IOfType } from '~/types'; +import logger from './logger'; + +const processes: IOfType> = globals('processses', {}).get(); + +function remove(pid: number): void { + try { + delete processes[pid]; + } catch (_) { + logger.error(`Removal of child process ${pid} failed`); + } +} + +export default { + add(pid: number, promise: Promise): void { + processes[pid] = promise.then(() => remove(pid)).catch(() => remove(pid)); + }, + kill(signal: string): void { + Object.keys(processes) + .map(Number) + .forEach((pid) => process.kill(pid, signal)); + }, + isDone(): boolean { + return !Object.keys(processes).length; + }, + async promise(): Promise { + await Promise.all(Object.values(processes)); + } +};