From fbfa28b23a7152ac3df9f7c6ad70f5d4e81aec4a Mon Sep 17 00:00:00 2001 From: erwanMarmelab Date: Mon, 9 Oct 2023 09:51:14 +0200 Subject: [PATCH 1/2] catch Ctrl+C --- src/commands/analyze.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/commands/analyze.ts b/src/commands/analyze.ts index 7ab21fc..831f187 100644 --- a/src/commands/analyze.ts +++ b/src/commands/analyze.ts @@ -11,6 +11,7 @@ import logErrorOnSentry from '../services/errors/Sentry'; import { DEFAULT_SAMPLES } from '../constants'; export const DEFAULT_CONFIG_FILE = './.greenframe.yml'; +import checkGreenFrameSecretToken from '../tasks/checkGreenFrameSecretToken'; import createNewAnalysis from '../tasks/createNewAnalysis'; import detectDockerVersion from '../tasks/detectDockerVersion'; import detectKubernetesVersion from '../tasks/detectKubernetesVersion'; @@ -20,7 +21,6 @@ import initializeKubeClient from '../tasks/initializeKubeClient'; import retrieveGitInformations from '../tasks/retrieveGitInformations'; import retrieveGreenFrameProject from '../tasks/retrieveGreenFrameProject'; import runScenarioAndSaveResults from '../tasks/runScenariosAndSaveResult'; -import checkGreenFrameSecretToken from '../tasks/checkGreenFrameSecretToken'; class AnalyzeCommand extends Command { static args = [ { @@ -122,6 +122,12 @@ class AnalyzeCommand extends Command { async run() { let analysisId; + + process.on('SIGINT', async () => { + console.log("Vous avez appuyé sur Ctrl + C. L'événement a été déclenché."); + this.exit(0); + }); + try { const commandParams = await this.parse(AnalyzeCommand); const configFilePath = From 90cc38fa8bb5b62c17bd60ebd13cd35e7866ed0d Mon Sep 17 00:00:00 2001 From: erwanMarmelab Date: Wed, 11 Oct 2023 10:52:33 +0200 Subject: [PATCH 2/2] save failed results --- src/commands/analyze.ts | 31 +++++++++++++++++++++++-------- src/services/api/analyses.ts | 2 +- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/commands/analyze.ts b/src/commands/analyze.ts index 831f187..b0df33b 100644 --- a/src/commands/analyze.ts +++ b/src/commands/analyze.ts @@ -21,6 +21,26 @@ import initializeKubeClient from '../tasks/initializeKubeClient'; import retrieveGitInformations from '../tasks/retrieveGitInformations'; import retrieveGreenFrameProject from '../tasks/retrieveGreenFrameProject'; import runScenarioAndSaveResults from '../tasks/runScenariosAndSaveResult'; + +let analysisId: string | null = null; + +process.on('SIGINT' || 'SIGKILL' || 'SIGTERM' || 'SIGQUIT', async function () { + if (analysisId !== null) { + try { + await saveFailedAnalysis(analysisId, { + errorCode: ERROR_CODES.UNKNOWN_ERROR, + errorMessage: 'Analysis stopped with Ctrl+C', + }); + } catch (error) { + console.log('error :', error); + } + } + + setTimeout(() => { + process.exit(1); + }, 299); +}); + class AnalyzeCommand extends Command { static args = [ { @@ -121,13 +141,6 @@ class AnalyzeCommand extends Command { }; async run() { - let analysisId; - - process.on('SIGINT', async () => { - console.log("Vous avez appuyé sur Ctrl + C. L'événement a été déclenché."); - this.exit(0); - }); - try { const commandParams = await this.parse(AnalyzeCommand); const configFilePath = @@ -210,6 +223,7 @@ class AnalyzeCommand extends Command { }); const tasks = task.newListr(tasksDefinition, { rendererOptions: { collapse: false }, + registerSignalListeners: false, }); return tasks; }, @@ -217,6 +231,7 @@ class AnalyzeCommand extends Command { ], { renderer: process.env.DEBUG ? 'verbose' : 'default', + registerSignalListeners: false, } ); const { result } = await tasks.run(); @@ -231,7 +246,7 @@ class AnalyzeCommand extends Command { } try { - if (analysisId) { + if (analysisId !== null) { await saveFailedAnalysis(analysisId, { errorCode: error.errorCode || ERROR_CODES.UNKNOWN_ERROR, errorMessage: error.response?.data || error.message, diff --git a/src/services/api/analyses.ts b/src/services/api/analyses.ts index b812d46..f0ffbe1 100644 --- a/src/services/api/analyses.ts +++ b/src/services/api/analyses.ts @@ -59,7 +59,7 @@ export const saveFailedAnalysis = async ( { errorCode, errorMessage }: { errorCode: string; errorMessage: string } ) => { debug('saveFailedAnalysis', analysisId); - return instance.post(`/analyses/${analysisId}/fail`, { + return instance.post(`/analyses/${analysisId}/failed`, { errorCode, errorMessage: errorMessage.toString(), });