Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/kg-eslint' into kg-eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgo committed Oct 20, 2020
2 parents 8a71241 + 730ca07 commit 85036f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
24 changes: 13 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import deb from "debug"
import psTree from "ps-tree"
import util from "util"
const debug = deb("end-child-processes")
const psTreeA = util.promisify(psTree)
import deb from "debug";
import psTree from "ps-tree";
import util from "util";
const debug = deb("end-child-processes");
const psTreeA = util.promisify(psTree);

export async function endChildProcesses(): Promise<void> {
const children = await psTreeA(process.pid)
const children = await psTreeA(process.pid);
for (const child of children) {
if (child.COMMAND === "ps") {
continue
continue;
}
const processID = parseInt(child.PID, 10)
debug(`ending child process ${processID}: ${child.COMMAND}`)
const processID = parseInt(child.PID, 10);
debug(`ending child process ${processID}: ${child.COMMAND}`);
try {
process.kill(processID)
process.kill(processID);
} catch (e) {
debug(`cannot kill process ${processID} (${child.COMMAND}): ${e.message}`)
debug(
`cannot kill process ${processID} (${child.COMMAND}): ${e.message}`
);
}
}
}
36 changes: 18 additions & 18 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { strict as assert } from "assert"
import childProcess from "child_process"
import { test } from "mocha"
import psTree from "ps-tree"
import util from "util"
import { strict as assert } from "assert";
import childProcess from "child_process";
import { test } from "mocha";
import psTree from "ps-tree";
import util from "util";

import { endChildProcesses } from "./index"
const psTreeA = util.promisify(psTree)
import { endChildProcesses } from "./index";
const psTreeA = util.promisify(psTree);

test("end-child-processes", async function () {
// start some child processes
childProcess.exec(blockingCommand())
let count = await runningProcessCount()
assert.equal(count, 2, "should have 2 child processes")
childProcess.exec(blockingCommand());
let count = await runningProcessCount();
assert.equal(count, 2, "should have 2 child processes");

// stop the child processes
await endChildProcesses()
count = await runningProcessCount()
assert.equal(count, 0, "should have 0 child processes now")
})
await endChildProcesses();
count = await runningProcessCount();
assert.equal(count, 0, "should have 0 child processes now");
});

/**
* Returns a command that runs on the current platform and blocks
*/
function blockingCommand() {
if (process.platform === "win32") {
return "cmd /c pause"
return "cmd /c pause";
} else {
return "(read foo)"
return "(read foo)";
}
}

async function runningProcessCount() {
const children = await psTreeA(process.pid)
return children.length - 1
const children = await psTreeA(process.pid);
return children.length - 1;
}

0 comments on commit 85036f0

Please sign in to comment.