Skip to content

Commit

Permalink
Merge pull request #658 from CleverCloud/add-retry-on-new-logs
Browse files Browse the repository at this point in the history
Fix bugs on clever logs and enable retry
  • Loading branch information
hsablonniere authored Feb 7, 2024
2 parents 676a598 + a4ec4b9 commit 0a2255d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"scripts/*.sh"
],
"dependencies": {
"@clevercloud/client": "^8.0.2",
"@clevercloud/client": "^8.1.0",
"clf-date": "^0.2.0",
"cliparse": "^0.3.3",
"colors": "1.4.0",
Expand Down
14 changes: 11 additions & 3 deletions src/models/log-v4.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getHostAndTokens } = require('./send-to-api.js');
const { getHostAndTokens, processError } = require('./send-to-api.js');
const colors = require('colors/safe');
const { Deferred } = require('./utils.js');
const Logger = require('../logger.js');
Expand All @@ -9,6 +9,11 @@ const { ApplicationLogStream } = require('@clevercloud/client/cjs/streams/applic
const THROTTLE_ELEMENTS = 2000;
const THROTTLE_PER_IN_MILLISECONDS = 100;

const retryConfiguration = {
enabled: true,
maxRetryCount: 6,
};

async function displayLogs (params) {

const deferred = params.deferred || new Deferred();
Expand All @@ -20,6 +25,8 @@ async function displayLogs (params) {
tokens,
ownerId,
appId,
connectionTimeout: 10_000,
retryConfiguration,
since,
until,
deploymentId,
Expand All @@ -33,10 +40,10 @@ async function displayLogs (params) {

logStream
.on('open', (event) => {
Logger.debug(`stream opened! ${JSON.stringify({ appId, filter, deploymentId })}`);
Logger.debug(colors.blue(`Logs stream (open) ${JSON.stringify({ appId, filter, deploymentId })}`));
})
.on('error', (event) => {
Logger.error(`an error occured: ${event.detail}`);
Logger.debug(colors.red(`Logs stream (error) ${event.error.message}`));
})
.onLog((log) => {
Logger.println(formatLogLine(log));
Expand All @@ -45,6 +52,7 @@ async function displayLogs (params) {
// start() is blocking until end of stream
logStream.start()
.then((reason) => deferred.resolve())
.catch(processError)
.catch((error) => deferred.reject(error));

return logStream;
Expand Down
20 changes: 15 additions & 5 deletions src/models/send-to-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,22 @@ async function sendToApi (requestParams) {
.then(prefixUrl(conf.API_HOST))
.then(addOauthHeader(tokens))
.then((requestParams) => {
if (process.env.CLEVER_VERBOSE) {
Logger.debug(`${requestParams.method.toUpperCase()} ${requestParams.url} ? ${JSON.stringify(requestParams.queryParams)}`);
}
Logger.debug(`${requestParams.method.toUpperCase()} ${requestParams.url} ? ${JSON.stringify(requestParams.queryParams)}`);
return requestParams;
})
.then((requestParams) => request(requestParams, { retry: 1 }));
.then(request)
.catch(processError);
}

function processError (error) {
const code = error.code ?? error?.cause?.code;
if (code === 'EAI_AGAIN') {
throw new Error('Cannot reach the Clever Cloud API, please check your internet connection.', { cause: error });
}
if (code === 'ECONNRESET') {
throw new Error('The connection to the Clever Cloud API was closed abruptly, please try again.', { cause: error });
}
throw error;
}

function sendToWarp10 (requestParams) {
Expand All @@ -45,4 +55,4 @@ async function getHostAndTokens () {
};
}

module.exports = { sendToApi, sendToWarp10, getHostAndTokens };
module.exports = { sendToApi, sendToWarp10, getHostAndTokens, processError };

0 comments on commit 0a2255d

Please sign in to comment.