Skip to content

Commit 4e6dda5

Browse files
committed
fix: terminate telemetry services before process.exit calls
1 parent 0043fec commit 4e6dda5

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

lib/addNewMask.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
/* eslint-disable promise/catch-or-return */
2+
13
// ↓ Should be imported first
2-
require('@codefresh-io/cf-telemetry/init');
4+
const { terminate } = require('@codefresh-io/cf-telemetry/init');
35
// ↓ Keep one blank line below to prevent automatic import reordering
46

57
const { Logger } = require('@codefresh-io/cf-telemetry/logs');
@@ -43,14 +45,14 @@ async function updateMasks(secret) {
4345
if (response.statusCode === 201) {
4446
logger.log(`successfully updated masks with secret: ${secret.key}`);
4547
exitWithError = false;
46-
process.exit(exitCodes.success);
48+
terminate().finally(() => process.exit(exitCodes.success));
4749
} else {
4850
logger.error(`could not create mask for secret: ${secret.key}. Server responded with: ${response.statusCode}\n\n${response.body}`);
49-
process.exit(exitCodes.error);
51+
terminate().finally(() => process.exit(exitCodes.error));
5052
}
5153
} catch (error) {
5254
logger.error(`could not create mask for secret: ${secret.key}. Error: ${error}`);
53-
process.exit(exitCodes.error);
55+
terminate().finally(() => process.exit(exitCodes.error));
5456
}
5557
}
5658

@@ -60,7 +62,7 @@ if (require.main === module) {
6062
// first argument is the secret key second argument is the secret value
6163
if (process.argv.length < 4) {
6264
logger.log('not enough arguments, need secret key and secret value');
63-
process.exit(exitCodes.missingArguments);
65+
terminate().finally(() => process.exit(exitCodes.missingArguments));
6466
}
6567
const key = process.argv[2];
6668
const value = process.argv[3];

lib/helpers.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/* eslint-disable promise/catch-or-return */
2+
3+
// ↓ Should be imported first
4+
const { terminate } = require('@codefresh-io/cf-telemetry/init');
5+
// ↓ Keep one blank line below to prevent automatic import reordering
6+
17
const { stat, writeFile, readFile } = require('node:fs/promises');
28
const path = require('path');
39
const { Logger } = require('@codefresh-io/cf-telemetry/logs');
@@ -60,11 +66,11 @@ const getServerAddress = async () => {
6066
*/
6167
const registerExitHandlers = () => {
6268
process.on('SIGTERM', () => {
63-
process.exit(143); // default exit code for SIGTERM
69+
terminate().finally(() => process.exit(143)); // default exit code for SIGTERM
6470
});
6571

6672
process.on('SIGINT', () => {
67-
process.exit(130); // default exit code for SIGINT
73+
terminate().finally(() => process.exit(130)); // default exit code for SIGINT
6874
});
6975
};
7076

lib/isReady.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
/* eslint-disable promise/catch-or-return */
2+
13
// ↓ Should be imported first
2-
require('@codefresh-io/cf-telemetry/init');
4+
const { terminate } = require('@codefresh-io/cf-telemetry/init');
35
// ↓ Keep one blank line below to prevent automatic import reordering
46

57
const { readFileSync } = require('fs');
@@ -41,5 +43,5 @@ function isContainerLoggerReady(state) {
4143
isReady = isContainerLoggerReady(state);
4244
}
4345

44-
process.exit(isReady ? 0 : 1);
46+
terminate().finally(() => process.exit(isReady ? 0 : 1));
4547
})();

lib/logger.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/* eslint-disable promise/catch-or-return */
2+
3+
// ↓ Should be imported first
4+
const { terminate } = require('@codefresh-io/cf-telemetry/init');
5+
// ↓ Keep one blank line below to prevent automatic import reordering
6+
17
const fs = require('fs');
28
const { EventEmitter } = require('events');
39
const _ = require('lodash');
@@ -151,7 +157,7 @@ class Logger {
151157
*/
152158
_error(err) {
153159
logger.error(err.toString());
154-
process.exit(1);
160+
terminate().finally(() => process.exit(1));
155161
}
156162

157163
/**

0 commit comments

Comments
 (0)