-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dev-admin cleans child process automatically (#1633)
- Loading branch information
Showing
5 changed files
with
64 additions
and
9 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
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,47 @@ | ||
"use strict"; | ||
|
||
/* eslint-disable no-console, no-process-exit */ | ||
const { psChildren } = require("ps-get"); | ||
|
||
/** | ||
* Attempt to clean up any child process remaining | ||
*/ | ||
let cleaned = false; | ||
|
||
const doCleanup = async () => { | ||
if (cleaned) { | ||
return; | ||
} | ||
cleaned = true; | ||
const children = await psChildren(process.pid); | ||
children.reverse().forEach(c => { | ||
try { | ||
console.log( | ||
"detected child process left from dev-admin, PID:", | ||
c.pid, | ||
"- trying to terminate it" | ||
); | ||
process.kill(c.pid, "SIGINT"); | ||
} catch { | ||
// | ||
} | ||
}); | ||
}; | ||
|
||
["uncaughtException", "unhandledRejection"].forEach(event => { | ||
process.on(event, async err => { | ||
console.log("dev-admin failure", event, err.stack); | ||
await doCleanup(); | ||
process.exit(process.exitCode); | ||
}); | ||
}); | ||
|
||
["SIGTERM", "SIGINT", "SIGHUP"].forEach(sig => { | ||
process.on(sig, async name => { | ||
console.log("dev-admin received signal:", name); | ||
await doCleanup(); | ||
process.exit(process.exitCode); | ||
}); | ||
}); | ||
|
||
module.exports = { doCleanup }; |
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
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
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