Skip to content

Commit a58778e

Browse files
committed
retry seinding request in case request failed due to network connection
1 parent 1f3c842 commit a58778e

3 files changed

Lines changed: 114 additions & 30 deletions

File tree

QencodeApiClient.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
const axios = require('axios');
22
const querystring = require('querystring');
3-
const axiosRetry = require('axios-retry');
43

54
const TranscodingTask = require('./Classes/TranscodingTask');
65

7-
// Set the retry logic
8-
axiosRetry(axios, {
9-
retries: 20,
10-
retryDelay: (retryCount) => {
11-
return 3000; // time interval between retries
12-
},
13-
// retry on Network Error and 5xx responses
14-
retryCondition: (error) => {
15-
return axiosRetry.isNetworkOrIdempotentRequestError(error) || error.code === 'ECONNABORTED';
16-
},
17-
});
6+
function delay(time) {
7+
return new Promise(resolve => setTimeout(resolve, time));
8+
}
189

1910
class QencodeApiClient {
2011
constructor(options) {
@@ -93,9 +84,10 @@ class QencodeApiClient {
9384
}
9485
return new TranscodingTask(this, response.task_token, response.upload_url);
9586
}
96-
97-
async Request(path, parameters, statusUrl){
98-
87+
88+
89+
async Request(path, parameters, statusUrl){
90+
9991
this.lastResponseRaw = null;
10092
this.lastResponse = null;
10193

@@ -117,22 +109,31 @@ class QencodeApiClient {
117109
parameters = querystring.stringify(parameters);
118110
}
119111

120-
try {
121-
this.lastResponseRaw = await axios.post(
122-
requestUrl,
123-
parameters,
124-
{
125-
headers: {
126-
'Content-Type': 'application/x-www-form-urlencoded'
127-
},
128-
timeout: this.ConnectTimeout * 1000
129-
}
112+
let retry = 20;
113+
const retryDelay = 3000;
130114

131-
);
132-
} catch (err) {
133-
throw new Error("Error executing request to url: " + requestUrl, err);
115+
for (let i = 0; i < retry; i++) {
116+
try {
117+
this.lastResponseRaw = await axios.post(
118+
requestUrl,
119+
parameters,
120+
{
121+
headers: {
122+
'Content-Type': 'application/x-www-form-urlencoded'
123+
},
124+
timeout: this.ConnectTimeout * 1000
125+
}
126+
);
127+
break;
128+
} catch (err) {
129+
if(i === retry - 1) {
130+
throw new Error("All attempts to execute request failed.");
131+
}
132+
console.error(`Error executing request to url: ${requestUrl} on attempt ${i + 1}. Retrying in ${retryDelay / 1000} seconds...`);
133+
await delay(retryDelay);
134+
}
134135
}
135-
136+
136137
let response = this.lastResponseRaw.data;
137138

138139
if (response == null || response.error == null){

package-lock.json

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"bundleDependencies": [],
2727
"dependencies": {
2828
"axios": "^1.2.1",
29-
"axios-retry": "^3.5.1",
29+
"qencode-api": "^3.0.0",
3030
"querystring": "^0.2.1"
3131
},
3232
"deprecated": false,

0 commit comments

Comments
 (0)