Skip to content

Commit

Permalink
feat(utils/ps-manager): adds child processes manager
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 6, 2019
1 parent 5458c3a commit c90f121
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/utils/ps-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import globals from '~/globals';
import { IOfType } from '~/types';
import logger from './logger';

const processes: IOfType<Promise<void>> = 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>): 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<void> {
await Promise.all(Object.values(processes));
}
};

0 comments on commit c90f121

Please sign in to comment.