Skip to content

Commit

Permalink
implement retry functionality when HTTP request fails for Damage-Dete…
Browse files Browse the repository at this point in the history
…ction TASKs
  • Loading branch information
rohitsharma120582 committed Feb 23, 2024
1 parent 0aa89c5 commit 374759f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
7 changes: 4 additions & 3 deletions packages/corejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@
},
"dependencies": {
"@reduxjs/toolkit": "^1.6.2",
"@sentry/browser": "^7.36.0",
"@sentry/tracing": "^7.36.0",
"axios": "^0.26.1",
"axios-retry": "^3.9.0",
"lodash": "^4.17.21",
"map-keys-deep-lodash": "^1.2.4",
"normalizr": "^3.6.1",
"@sentry/browser": "^7.36.0",
"@sentry/tracing": "^7.36.0"
"normalizr": "^3.6.1"
},
"devDependencies": {
"@types/jest": "^27.4.1",
Expand Down
56 changes: 53 additions & 3 deletions packages/corejs/src/tasks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createEntityAdapter, createSlice, EntityState, PayloadAction } from '@reduxjs/toolkit';
import axios from 'axios';
import axios, { AxiosRequestConfig } from 'axios';
import axiosRetry from 'axios-retry';
import { camelCase, isEmpty, isNil, omitBy, snakeCase } from 'lodash';
import mapKeysDeep from 'map-keys-deep-lodash';

Expand All @@ -22,6 +23,9 @@ import { TaskPayloadTypes } from './reduxTypes';

import schema, { idAttribute, key } from './schema';

// Define the maximum number of retry attempts
const MAX_RETRY_ATTEMPTS: number = 4;

/**
* Get the details of a task in a specific inspection.
*
Expand Down Expand Up @@ -82,12 +86,58 @@ export async function updateOne(
name: TaskName,
updateTask: UpdateTask,
): Promise<UpdateOneTaskResponse> {
const axiosResponse = await axios.request<IdResponse<'id'>>({
/**
* Define the retry configuration for this specific call
*/
const retryConfig = {
retries: MAX_RETRY_ATTEMPTS,
retryDelay: (retryCount) => {
return retryCount * 1000;
},
shouldResetTimeout: true,
retryCondition: (error) => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error('Response:', error.response);
console.error('Data:', error.response.data);
console.error('Status:', error.response.status);
console.error('Headers:', error.response.headers);
} else if (error.request) {
// The request was made but no response was received
console.error('Request:', error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.error('Error message:', error.message);
}
console.error('Config:', error.config);
// Check if the request should be retried
/**
* You can specify a condition for retry here
* For example, retry on network errors or 5xx status codes
*/
return axiosRetry.isNetworkOrIdempotentRequestError(error) || error.response.status === 500;
}
};

/**
* Define the configuration for this specific call
*/
const apiConfig: AxiosRequestConfig = {
...config.axiosConfig,
method: 'patch',
url: `/inspections/${inspectionId}/tasks/${name}`,
data: mapKeysDeep(updateTask, (v, k) => snakeCase(k)),
});
'axios-retry': retryConfig,
};

// Apply axios-retry to the axios object
axiosRetry(axios, apiConfig['axios-retry']);

/**
* Make the request with axios.request
*/
const axiosResponse = await axios.request<IdResponse<'id'>>(apiConfig);

const id = axiosResponse.data[idAttribute];
const updatedTask: UpdatedTask = {
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4082,6 +4082,14 @@ axe-core@^4.6.2:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.7.2.tgz#040a7342b20765cb18bb50b628394c21bccc17a0"
integrity sha512-zIURGIS1E1Q4pcrMjp+nnEh+16G56eG/MUllJH8yEvw7asDo7Ac9uhC9KIH5jzpITueEZolfYglnCGIuSBz39g==

axios-retry@^3.9.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-3.9.1.tgz#c8924a8781c8e0a2c5244abf773deb7566b3830d"
integrity sha512-8PJDLJv7qTTMMwdnbMvrLYuvB47M81wRtxQmEdV5w4rgbTXTt+vtPkXwajOfOdSyv/wZICJOC+/UhXH4aQ/R+w==
dependencies:
"@babel/runtime" "^7.15.4"
is-retry-allowed "^2.2.0"

axios@^0.24.0:
version "0.24.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.24.0.tgz#804e6fa1e4b9c5288501dd9dff56a7a0940d20d6"
Expand Down Expand Up @@ -9654,6 +9662,11 @@ is-resolvable@^1.0.0:
resolved "https://registry.yarnpkg.com/is-resolvable/-/is-resolvable-1.1.0.tgz#fb18f87ce1feb925169c9a407c19318a3206ed88"
integrity sha512-qgDYXFSR5WvEfuS5dMj6oTMEbrrSaM0CrFk2Yiq/gXnBvD9pMa2jGXxyhGLfvhZpuMZe18CJpFxAt3CRs42NMg==

is-retry-allowed@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-2.2.0.tgz#88f34cbd236e043e71b6932d09b0c65fb7b4d71d"
integrity sha512-XVm7LOeLpTW4jV19QSH38vkswxoLud8sQ57YwJVTPWdiaI9I8keEhGFpBlslyVsgdQy4Opg8QOLb8YRgsyZiQg==

[email protected]:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-root/-/is-root-2.1.0.tgz#809e18129cf1129644302a4f8544035d51984a9c"
Expand Down

0 comments on commit 374759f

Please sign in to comment.