From 1018797b1cebcf036c7bf39ff262aa018b18d58d Mon Sep 17 00:00:00 2001 From: Javier Viola Date: Thu, 13 Apr 2023 12:10:25 -0300 Subject: [PATCH 1/2] fix(cli): add sigterm handler and refactor --- javascript/packages/cli/src/cli.ts | 47 ++++++++++++++---------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/javascript/packages/cli/src/cli.ts b/javascript/packages/cli/src/cli.ts index 2d6f8bf83..947cd1fce 100644 --- a/javascript/packages/cli/src/cli.ts +++ b/javascript/packages/cli/src/cli.ts @@ -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); @@ -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( @@ -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); From 5028de0b77d0fe3e38ef1d904aa6549abe668e01 Mon Sep 17 00:00:00 2001 From: wirednkod Date: Thu, 13 Apr 2023 18:21:36 +0300 Subject: [PATCH 2/2] fix linter --- javascript/packages/cli/src/cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascript/packages/cli/src/cli.ts b/javascript/packages/cli/src/cli.ts index 947cd1fce..415e883d8 100644 --- a/javascript/packages/cli/src/cli.ts +++ b/javascript/packages/cli/src/cli.ts @@ -18,7 +18,7 @@ async function handleTermination(userInterrupted = false) { process.env.terminating = "1"; if (network && !alreadyTryToStop) { alreadyTryToStop = true; - if(userInterrupted) console.log("Ctrl+c detected..."); + if (userInterrupted) console.log("Ctrl+c detected..."); debug("removing namespace: " + network.namespace); await network.dumpLogs(); await network.stop();