Skip to content

Commit

Permalink
Merge pull request #956 from HubSpot/br-fix-dev-mode-termination-cleanup
Browse files Browse the repository at this point in the history
Fix project local dev exit cleanup
  • Loading branch information
brandenrodgers authored Nov 14, 2023
2 parents a9af83d + 9c362a9 commit 30cfa00
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/cli/commands/project/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ exports.handler = async options => {

await LocalDev.start();

handleExit(LocalDev.stop);
handleExit(({ isSIGHUP }) => LocalDev.stop(!isSIGHUP));
};

exports.builder = yargs => {
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
27 changes: 16 additions & 11 deletions packages/cli/lib/LocalDevManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
30 changes: 24 additions & 6 deletions packages/cli/lib/process.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
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 = [
'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;

terminationSignals.forEach(signal => {
process.removeAllListeners(signal);

process.on(signal, async () => {
await callback();
// Prevent duplicate exit handling
if (!exitInProgress) {
exitInProgress = true;
const isSIGHUP = signal === 'SIGHUP';

// Prevent logs when terminal closes
if (isSIGHUP) {
setLogLevel(LOG_LEVEL.NONE);
}

logger.debug(i18n(`${i18nKey}.exitDebug`, { signal }));
await callback({ isSIGHUP });
}
});
});
};
Expand Down

0 comments on commit 30cfa00

Please sign in to comment.