-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils/ps-manager): adds child processes manager
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}; |