Skip to content

Commit

Permalink
[RETRY TASK] Add Axios optimization: retry handling (#287)
Browse files Browse the repository at this point in the history
* [RETRY TASK] default retries=5 attempts + 6s interval

* [RETRY TASK] list of processing statuses:409,429,502,503

* [RETRY TASK] small update to adapter tests: timeout increased to 20 seconds

* [RETRY TASK] move axios timouts to constant file
  • Loading branch information
mykhailiukVitalii authored Feb 5, 2024
1 parent b51066d commit a2f83cb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const os = require('os');
const path = require('path');

const APP_PREFIX = chalk.gray('[TESTOMATIO]');
const AXIOS_TIMEOUT = 20 * 1000; // sum = 20sec
const AXIOS_RETRY_TIMEOUT = 5 * 1000; // sum = 5sec

const TESTOMAT_TMP_STORAGE_DIR = path.join(os.tmpdir(), 'testomatio_tmp');

Expand Down Expand Up @@ -32,5 +34,7 @@ module.exports = {
TESTOMAT_TMP_STORAGE_DIR,
CSV_HEADERS,
STATUS,
HTML_REPORT
HTML_REPORT,
AXIOS_TIMEOUT,
AXIOS_RETRY_TIMEOUT
}
34 changes: 31 additions & 3 deletions lib/pipe/testomatio.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const debug = require('debug')('@testomatio/reporter:pipe:testomatio');
const chalk = require('chalk');
// Retry interceptor function
const axiosRetry = require('axios-retry');
// Default axios instance
const axios = require('axios');
const JsonCycle = require('json-cycle');
const { APP_PREFIX, STATUS } = require('../constants');

const { APP_PREFIX, STATUS, AXIOS_TIMEOUT, AXIOS_RETRY_TIMEOUT } = require('../constants');
const { isValidUrl, foundedTestLog } = require('../utils/utils');
const { parseFilterParams, generateFilterRequestParams, setS3Credentials, } = require('../utils/pipe_utils');
const { TESTOMATIO } = require('../config');
Expand Down Expand Up @@ -34,10 +38,31 @@ class TestomatioPipe {
this.groupTitle = params.groupTitle || process.env.TESTOMATIO_RUNGROUP_TITLE;
this.env = process.env.TESTOMATIO_ENV;
this.label = process.env.TESTOMATIO_LABEL;

// Create a new instance of axios with a custom config
this.axios = axios.create({
baseURL: `${this.url.trim()}`,
timeout: 30000
timeout: AXIOS_TIMEOUT,
});
// Pass the axios instance to the retry function
axiosRetry(this.axios, {
retries: 3, // Number of retries (Defaults to 3)
shouldResetTimeout: true,
retryCondition: (error) => {
// Conditional check the error status code
switch (error.response.status) {
case 409:
case 429:
case 502:
case 503:
return true; // Retry request with response status code 409, 429, 502, 503
default:
return false; // Do not retry the others
}
},
retryDelay: (retryCount) => retryCount * AXIOS_RETRY_TIMEOUT, // sum = 15sec
onRetry: (retryCount) => {
debug(`Retry attempt #${retryCount} failed. Retrying again...`);
},
});

this.isEnabled = true;
Expand Down Expand Up @@ -150,10 +175,13 @@ class TestomatioPipe {
maxContentLength: Infinity,
maxBodyLength: Infinity,
});

this.runId = resp.data.uid;
this.runUrl = `${this.url}/${resp.data.url.split('/').splice(3).join('/')}`;
this.runPublicUrl = resp.data.public_url;

if (resp.data.artifacts) setS3Credentials(resp.data.artifacts);

this.store.runUrl = this.runUrl;
this.store.runPublicUrl = this.runPublicUrl;
this.store.runId = this.runId;
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"@aws-sdk/lib-storage": "^3.279.0",
"@octokit/rest": "^19.0.5",
"aws-sdk": "^2.1072.0",
"axios": "^0.25.0",
"axios": "^1.6.2",
"axios-retry": "^3.9.1",
"callsite-record": "^4.1.4",
"chalk": "^4.1.0",
"commander": "^4.1.1",
Expand All @@ -30,8 +31,8 @@
"lodash.memoize": "^4.1.2",
"lodash.merge": "^4.6.2",
"minimatch": "^9.0.3",
"uuid": "^9.0.0",
"promise-retry": "^2.0.1"
"promise-retry": "^2.0.1",
"uuid": "^9.0.0"
},
"files": [
"bin",
Expand Down
4 changes: 2 additions & 2 deletions tests/adapter/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Adapters', () => {
describe(adapterName, () => {
describe('positive tests', () => {
before(function (done) {
this.timeout(10000);
this.timeout(20000);

registerHandlers(server, RUN_ID);

Expand Down Expand Up @@ -127,7 +127,7 @@ describe('Adapters', () => {
describe('negative tests', () => {
before(function (done) {
process.env.TESTOMATIO = '';
this.timeout(10000);
this.timeout(20000);

registerHandlers(server, RUN_ID);

Expand Down

0 comments on commit a2f83cb

Please # to comment.