From 150e4230819301195861587f9f525ed78f1402a5 Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Tue, 14 Nov 2023 12:22:02 -0500 Subject: [PATCH 1/5] Fixing graceful exit for project local dev --- packages/cli/commands/project/dev.js | 2 +- packages/cli/lib/process.js | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/cli/commands/project/dev.js b/packages/cli/commands/project/dev.js index ff638939d..58c23d35f 100644 --- a/packages/cli/commands/project/dev.js +++ b/packages/cli/commands/project/dev.js @@ -338,7 +338,7 @@ exports.handler = async options => { await LocalDev.start(); - handleExit(LocalDev.stop); + handleExit(() => LocalDev.stop()); }; exports.builder = yargs => { diff --git a/packages/cli/lib/process.js b/packages/cli/lib/process.js index 5d8bbf6d7..874372740 100644 --- a/packages/cli/lib/process.js +++ b/packages/cli/lib/process.js @@ -1,4 +1,5 @@ const readline = require('readline'); +const { logger } = require('@hubspot/cli-lib/logger'); const handleExit = callback => { const terminationSignals = [ @@ -10,11 +11,18 @@ const handleExit = callback => { 'SIGTERM', 'SIGHUP', ]; + let exitInProgress = false; + terminationSignals.forEach(signal => { process.removeAllListeners(signal); process.on(signal, async () => { - await callback(); + logger.debug(`Attempting to gracefully exit. Triggered by ${signal}`); + // Prevent duplicate exit handling + if (!exitInProgress) { + exitInProgress = true; + await callback(); + } }); }); }; From 79ab97ac4eeb69bc385746a190732691c775557b Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Tue, 14 Nov 2023 13:09:15 -0500 Subject: [PATCH 2/5] handle exit on terminal window close --- packages/cli/commands/project/dev.js | 2 +- packages/cli/lib/LocalDevManager.js | 27 ++++++++++++++++----------- packages/cli/lib/process.js | 12 ++++++------ 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/packages/cli/commands/project/dev.js b/packages/cli/commands/project/dev.js index 58c23d35f..b2dd15479 100644 --- a/packages/cli/commands/project/dev.js +++ b/packages/cli/commands/project/dev.js @@ -338,7 +338,7 @@ exports.handler = async options => { await LocalDev.start(); - handleExit(() => LocalDev.stop()); + handleExit(({ isSIGHUP }) => LocalDev.stop(!isSIGHUP)); }; exports.builder = yargs => { diff --git a/packages/cli/lib/LocalDevManager.js b/packages/cli/lib/LocalDevManager.js index 010bf0056..3c8ea144f 100644 --- a/packages/cli/lib/LocalDevManager.js +++ b/packages/cli/lib/LocalDevManager.js @@ -141,25 +141,30 @@ class LocalDevManager { this.compareLocalProjectToDeployed(runnableComponents); } - async stop() { - SpinniesManager.add('cleanupMessage', { - text: i18n(`${i18nKey}.exitingStart`), - }); - + async stop(showProgress = true) { + if (showProgress) { + SpinniesManager.add('cleanupMessage', { + text: i18n(`${i18nKey}.exitingStart`), + }); + } await this.stopWatching(); const cleanupSucceeded = await this.devServerCleanup(); if (!cleanupSucceeded) { - SpinniesManager.fail('cleanupMessage', { - text: i18n(`${i18nKey}.exitingFail`), - }); + if (showProgress) { + SpinniesManager.fail('cleanupMessage', { + text: i18n(`${i18nKey}.exitingFail`), + }); + } process.exit(EXIT_CODES.ERROR); } - SpinniesManager.succeed('cleanupMessage', { - text: i18n(`${i18nKey}.exitingSucceed`), - }); + if (showProgress) { + SpinniesManager.succeed('cleanupMessage', { + text: i18n(`${i18nKey}.exitingSucceed`), + }); + } process.exit(EXIT_CODES.SUCCESS); } diff --git a/packages/cli/lib/process.js b/packages/cli/lib/process.js index 874372740..83372d07a 100644 --- a/packages/cli/lib/process.js +++ b/packages/cli/lib/process.js @@ -4,12 +4,12 @@ const { logger } = require('@hubspot/cli-lib/logger'); const handleExit = callback => { const terminationSignals = [ 'beforeExit', - 'SIGINT', - 'SIGUSR1', - 'SIGUSR2', + 'SIGINT', // Terminal trying to interrupt (Ctrl + C) + 'SIGUSR1', // Start Debugger User-defined signal 1 + 'SIGUSR2', // User-defined signal 2 'uncaughtException', - 'SIGTERM', - 'SIGHUP', + 'SIGTERM', // Represents a graceful termination + 'SIGHUP', // Parent terminal has been closed ]; let exitInProgress = false; @@ -21,7 +21,7 @@ const handleExit = callback => { // Prevent duplicate exit handling if (!exitInProgress) { exitInProgress = true; - await callback(); + await callback({ isSIGHUP: signal === 'SIGHUP' }); } }); }); From 028297eb2a0526b53b76420125068a0d3861e276 Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Tue, 14 Nov 2023 13:12:48 -0500 Subject: [PATCH 3/5] prevent all logs when terminal closes --- packages/cli/lib/process.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/cli/lib/process.js b/packages/cli/lib/process.js index 83372d07a..7c780cd0d 100644 --- a/packages/cli/lib/process.js +++ b/packages/cli/lib/process.js @@ -1,5 +1,5 @@ const readline = require('readline'); -const { logger } = require('@hubspot/cli-lib/logger'); +const { logger, setLogLevel, LOG_LEVEL } = require('@hubspot/cli-lib/logger'); const handleExit = callback => { const terminationSignals = [ @@ -21,7 +21,14 @@ const handleExit = callback => { // Prevent duplicate exit handling if (!exitInProgress) { exitInProgress = true; - await callback({ isSIGHUP: signal === 'SIGHUP' }); + const isSIGHUP = 'SIGHUP'; + + // Prevent logs when terminal closes + if (isSIGHUP) { + setLogLevel(LOG_LEVEL.NONE); + } + + await callback({ isSIGHUP }); } }); }); From c8e2bff252044a3aebf1c38fa9274387d7b8bbb2 Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Tue, 14 Nov 2023 13:29:37 -0500 Subject: [PATCH 4/5] log after updating the log level --- packages/cli/lib/process.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/lib/process.js b/packages/cli/lib/process.js index 7c780cd0d..626940d2b 100644 --- a/packages/cli/lib/process.js +++ b/packages/cli/lib/process.js @@ -17,7 +17,6 @@ const handleExit = callback => { process.removeAllListeners(signal); process.on(signal, async () => { - logger.debug(`Attempting to gracefully exit. Triggered by ${signal}`); // Prevent duplicate exit handling if (!exitInProgress) { exitInProgress = true; @@ -28,6 +27,7 @@ const handleExit = callback => { setLogLevel(LOG_LEVEL.NONE); } + logger.debug(`Attempting to gracefully exit. Triggered by ${signal}`); await callback({ isSIGHUP }); } }); From 9c362a9f35ef5f028a73d2fc66ee19e044a500eb Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Tue, 14 Nov 2023 13:40:25 -0500 Subject: [PATCH 5/5] fix bug and move copy to lang file --- packages/cli/lang/en.lyaml | 2 ++ packages/cli/lib/process.js | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/cli/lang/en.lyaml b/packages/cli/lang/en.lyaml index a911ece8e..02f0586f7 100644 --- a/packages/cli/lang/en.lyaml +++ b/packages/cli/lang/en.lyaml @@ -885,6 +885,8 @@ en: options: describe: "Options to pass to javascript fields files" lib: + process: + exitDebug: "Attempting to gracefully exit. Triggered by {{ signal }}" DevServerManager: portConflict: "The port {{ port }} is already in use." notInitialized: "The Dev Server Manager must be initialized before it is started." diff --git a/packages/cli/lib/process.js b/packages/cli/lib/process.js index 626940d2b..2af41b974 100644 --- a/packages/cli/lib/process.js +++ b/packages/cli/lib/process.js @@ -1,5 +1,8 @@ const readline = require('readline'); const { logger, setLogLevel, LOG_LEVEL } = require('@hubspot/cli-lib/logger'); +const { i18n } = require('./lang'); + +const i18nKey = 'cli.lib.process'; const handleExit = callback => { const terminationSignals = [ @@ -20,14 +23,14 @@ const handleExit = callback => { // Prevent duplicate exit handling if (!exitInProgress) { exitInProgress = true; - const isSIGHUP = 'SIGHUP'; + const isSIGHUP = signal === 'SIGHUP'; // Prevent logs when terminal closes if (isSIGHUP) { setLogLevel(LOG_LEVEL.NONE); } - logger.debug(`Attempting to gracefully exit. Triggered by ${signal}`); + logger.debug(i18n(`${i18nKey}.exitDebug`, { signal })); await callback({ isSIGHUP }); } });