Skip to content

Commit

Permalink
refactor(Telemetry): Report all interruption signals
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jul 7, 2021
1 parent dff2799 commit 7354c20
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
53 changes: 30 additions & 23 deletions scripts/serverless.js
Expand Up @@ -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');
Expand Down

0 comments on commit 7354c20

Please sign in to comment.