-
-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improves kill-logic of child processes.
- Loading branch information
1 parent
8c14769
commit bc749bc
Showing
5 changed files
with
93 additions
and
44 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 was deleted.
Oops, something went wrong.
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,59 @@ | ||
import cp from "child_process"; | ||
|
||
// List of child processes that are running currently. | ||
const children = []; | ||
|
||
/** | ||
* Removes this process from the children pool. | ||
* @this ChildProcess | ||
*/ | ||
function removeFromPool() { | ||
const index = children.indexOf(this); | ||
if (index !== -1) { | ||
children.splice(index, 1); | ||
} | ||
} | ||
|
||
/** | ||
* Kills this process and sub processes with the process group ID. | ||
* @this ChildProcess | ||
*/ | ||
function kill() { | ||
process.kill(-this.pid); | ||
} | ||
|
||
/** | ||
* Launches a new process with the given command. | ||
* This is almost same as `child_process.spawn`. | ||
* | ||
* This detaches the new process to make new process group. | ||
* And if this process exited before the new process exits, this kills the new process. | ||
* | ||
* This returns a `ChildProcess` instance. | ||
* `kill` method of the instance kills the new process and its sub processes with the process group ID. | ||
* | ||
* @param {string} command - The command to run. | ||
* @param {string[]} args - List of string arguments. | ||
* @param {object} options - Options. | ||
* @returns {ChildProcess} A ChildProcess instance of new process. | ||
*/ | ||
export default function spawn(command, args, options) { | ||
options.detached = true; // eslint-disable-line no-param-reassign | ||
|
||
const child = cp.spawn(command, args, options); | ||
child.on("exit", removeFromPool); | ||
child.on("error", removeFromPool); | ||
child.kill = kill; | ||
|
||
// Add to the pool to kill on exit. | ||
children.push(child); | ||
|
||
return child; | ||
} | ||
|
||
// Kill all child processes on exit. | ||
process.on("exit", () => { | ||
for (const child of children) { | ||
child.kill(); | ||
} | ||
}); |
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,28 @@ | ||
import cp from "child_process"; | ||
|
||
/** | ||
* Kills the new process and its sub processes forcibly. | ||
* @this ChildProcess | ||
*/ | ||
function kill() { | ||
spawn("taskkill", ["/F", "/T", "/PID", this.pid]); | ||
} | ||
|
||
/** | ||
* Launches a new process with the given command. | ||
* This is almost same as `child_process.spawn`. | ||
* | ||
* This returns a `ChildProcess` instance. | ||
* `kill` method of the instance kills the new process and its sub processes forcibly. | ||
* | ||
* @param {string} command - The command to run. | ||
* @param {string[]} args - List of string arguments. | ||
* @param {object} options - Options. | ||
* @returns {ChildProcess} A ChildProcess instance of new process. | ||
*/ | ||
export default function spawn(command, args, options) { | ||
const child = cp.spawn(command, args, options); | ||
child.kill = kill; | ||
|
||
return child; | ||
} |