From 7354c2000f25d526f8c3fd97c6d4d22054388755 Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Wed, 7 Jul 2021 15:51:43 +0200 Subject: [PATCH] refactor(Telemetry): Report all interruption signals --- package.json | 1 + scripts/serverless.js | 53 ++++++++++++++++++++++++------------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 365b37be659..848686c4dcf 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "promise-queue": "^2.2.5", "replaceall": "^0.1.6", "semver": "^7.3.5", + "signal-exit": "^3.0.3", "tabtab": "^3.0.2", "tar": "^6.1.0", "timers-ext": "^0.1.7", diff --git a/scripts/serverless.js b/scripts/serverless.js index 0f7df3095ee..1d511f59493 100755 --- a/scripts/serverless.js +++ b/scripts/serverless.js @@ -51,29 +51,36 @@ process.once('uncaughtException', (error) => { }); }); -process.once('SIGINT', () => { - clearTimeout(keepAliveTimer); - // If there's another SIGINT listener (command that works as deamon or reads stdin input) - // then let the other listener decide how process will exit - const isOtherSigintListener = Boolean(process.listenerCount('SIGINT')); - if ( - commandSchema && - !hasTelemetryBeenReported && - !isTelemetryDisabled && - (serverless ? serverless.isTelemetryReportedExternally : true) - ) { - const telemetryPayload = generateTelemetryPayload({ - command, - options, - commandSchema, - serviceDir, - configuration, - serverless, - commandUsage, - }); - storeTelemetryLocally({ ...telemetryPayload, outcome: 'interrupt' }); - } - if (!isOtherSigintListener) process.exit(130); +require('signal-exit/signals').forEach((signal) => { + process.once(signal, () => { + clearTimeout(keepAliveTimer); + // If there's another listener (e.g. we're in deamon context or reading stdin input) + // then let the other listener decide how process will exit + const isOtherSigintListener = Boolean(process.listenerCount(signal)); + if ( + commandSchema && + !hasTelemetryBeenReported && + !isTelemetryDisabled && + (serverless ? serverless.isTelemetryReportedExternally : true) + ) { + const telemetryPayload = generateTelemetryPayload({ + command, + options, + commandSchema, + serviceDir, + configuration, + serverless, + commandUsage, + }); + storeTelemetryLocally({ ...telemetryPayload, outcome: 'interrupt', interruptSignal: signal }); + } + + if (isOtherSigintListener) return; + // Follow recommendation from signal-exit: + // https://github.com/tapjs/signal-exit/blob/654117d6c9035ff6a805db4d4acf1f0c820fcb21/index.js#L97-L98 + if (process.platform === 'win32' && signal === 'SIGHUP') signal = 'SIGINT'; + process.kill(process.pid, signal); + }); }); const humanizePropertyPathKeys = require('../lib/configuration/variables/humanize-property-path-keys');