From 374759f59285b2e7e7b9c4e329f50b803ef36837 Mon Sep 17 00:00:00 2001 From: Rohit Sharma Date: Fri, 23 Feb 2024 13:26:51 +0530 Subject: [PATCH] implement retry functionality when HTTP request fails for Damage-Detection TASKs --- packages/corejs/package.json | 7 ++-- packages/corejs/src/tasks/index.ts | 56 ++++++++++++++++++++++++++++-- yarn.lock | 13 +++++++ 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/packages/corejs/package.json b/packages/corejs/package.json index a7d85c30c..874df7a2e 100644 --- a/packages/corejs/package.json +++ b/packages/corejs/package.json @@ -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", diff --git a/packages/corejs/src/tasks/index.ts b/packages/corejs/src/tasks/index.ts index e5bd60479..1b2a3fc2a 100644 --- a/packages/corejs/src/tasks/index.ts +++ b/packages/corejs/src/tasks/index.ts @@ -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'; @@ -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. * @@ -82,12 +86,58 @@ export async function updateOne( name: TaskName, updateTask: UpdateTask, ): Promise { - const axiosResponse = await axios.request>({ + /** + * 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>(apiConfig); const id = axiosResponse.data[idAttribute]; const updatedTask: UpdatedTask = { diff --git a/yarn.lock b/yarn.lock index 878e57221..5c2427d88 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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== + is-root@2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/is-root/-/is-root-2.1.0.tgz#809e18129cf1129644302a4f8544035d51984a9c"