Skip to content

Commit

Permalink
fix(cli): add sigterm handler and refactor (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepoviola authored Apr 13, 2023
1 parent dfab031 commit 9935861
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions javascript/packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@ const debug = require("debug")("zombie-cli");
const program = new Command("zombienet");

let network: Network | undefined;
let alreadyTryToStop = false;

async function handleTermination(userInterrupted = false) {
process.env.terminating = "1";
if (network && !alreadyTryToStop) {
alreadyTryToStop = true;
if (userInterrupted) console.log("Ctrl+c detected...");
debug("removing namespace: " + network.namespace);
await network.dumpLogs();
await network.stop();
}
}

// Ensure to log the uncaught exceptions
// to debug the problem, also exit because we don't know
// what happens there.
process.on("uncaughtException", async (err) => {
if (network) {
debug("removing namespace: " + network.namespace);
await network.stop();
}
await handleTermination();
console.log(`uncaughtException`);
console.log(err);
debug(err);
Expand All @@ -31,10 +40,7 @@ process.on("uncaughtException", async (err) => {
// accidentally don't have a 'catch' for.
// http://www.hacksrus.net/blog/2015/08/a-solution-to-swallowed-exceptions-in-es6s-promises/
process.on("unhandledRejection", async (err) => {
if (network) {
debug("removing namespace: " + network.namespace);
await network.stop();
}
await handleTermination();
debug(err);
console.log(
`\n${decorators.red("UnhandledRejection: ")} \t ${decorators.bright(
Expand All @@ -45,27 +51,18 @@ process.on("unhandledRejection", async (err) => {
});

// Handle ctrl+c to trigger `exit`.
let alreadyTry = false;
process.on("SIGINT", async function () {
process.env.terminating = "1";
if (network && !alreadyTry) {
alreadyTry = true;
const msg = "Ctrl+c ... removing namespace: " + network.namespace;
console.log(decorators.magenta(msg));
debug(msg);
await network.stop();
}
process.exit(2);
await handleTermination();
process.exit();
});

process.on("SIGTERM", async function () {
await handleTermination();
process.exit();
});

process.on("exit", async function () {
process.env.terminating = "1";
if (network && !alreadyTry) {
alreadyTry = true;
debug("removing namespace: " + network.namespace);
await network.dumpLogs();
await network.stop();
}
await handleTermination();
const exitCode = process.exitCode !== undefined ? process.exitCode : 2;
// use exitCode set by mocha or 2 as default.
process.exit(exitCode);
Expand Down

0 comments on commit 9935861

Please sign in to comment.