Skip to content

Commit c5ef7ea

Browse files
authored
Update final retry call and add async retry tests (#3844)
* update last err msg thrown * also update in findCIDInNetwork * update msg to not include multihash bc alrdy in err msg
1 parent eac4bb7 commit c5ef7ea

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

creator-node/src/fileManager.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ async function fetchFileFromNetworkAndWriteToDisk({
144144

145145
try {
146146
await asyncRetry({
147-
asyncFn: async (bail) => {
147+
asyncFn: async (bail, num) => {
148148
try {
149149
await fetchFileFromTargetGatewayAndWriteToDisk({
150150
contentUrl,
@@ -181,9 +181,16 @@ async function fetchFileFromNetworkAndWriteToDisk({
181181
}
182182

183183
// Re-throw any other error to continue with retry logic
184-
throw new Error(
185-
`Failed to fetch content=${multihash} with statusCode=${e.response?.status}. Retrying..`
186-
)
184+
if (num === numRetries + 1) {
185+
// Final error thrown
186+
throw new Error(
187+
`Failed to fetch content with statusCode=${e.response?.status} after ${num} retries`
188+
)
189+
} else {
190+
throw new Error(
191+
`Failed to fetch content with statusCode=${e.response?.status}. Retrying..`
192+
)
193+
}
187194
}
188195
},
189196
logger,

creator-node/src/utils/asyncRetry.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const { logger: genericLogger } = require('../logging')
1212
* @param {func} param.asyncFn the fn to asynchronously retry
1313
* @param {Object} param.options optional options. defaults to the params listed below if not explicitly passed in
1414
* @param {number} [param.options.factor=2] the exponential factor
15-
* @param {number} [param.options.retries=5] the max number of retries. defaulted to 5
15+
* @param {number} [param.options.retries=5] the max number of retries. defaulted to 5. So, this will attempt once and retry 5 times for a total of 6 tries.
1616
* @param {number} [param.options.minTimeout=1000] minimum number of ms to wait after first retry. defaulted to 1000ms
1717
* @param {number} [param.options.maxTimeout=5000] maximum number of ms between two retries. defaulted to 5000ms
1818
* @param {func} [param.options.onRetry] fn that gets called per retry

creator-node/src/utils/cidUtils.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export async function findCIDInNetwork(
6767

6868
try {
6969
found = await asyncRetry({
70-
asyncFn: async (bail: (e: Error) => void) => {
70+
asyncFn: async (bail: (e: Error) => void, num: number) => {
7171
let response
7272
try {
7373
response = await axios({
@@ -98,9 +98,16 @@ export async function findCIDInNetwork(
9898
return
9999
}
100100

101-
throw new Error(
102-
`Failed to fetch content with statusCode=${e.response?.status}. Retrying..`
103-
)
101+
if (num === numRetries + 1) {
102+
// Final error thrown
103+
throw new Error(
104+
`Failed to fetch content with statusCode=${e.response?.status} after ${num} retries`
105+
)
106+
} else {
107+
throw new Error(
108+
`Failed to fetch content with statusCode=${e.response?.status}. Retrying..`
109+
)
110+
}
104111
}
105112

106113
if (!response || !response.data) {

creator-node/test/asyncRetry.test.js

+51
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,59 @@ describe('test asyncRetry', function () {
1616
minTimeout: 0,
1717
maxTimeout: 100
1818
})
19+
assert.fail('Should have thrown')
1920
} catch (e) {
2021
assert.deepStrictEqual(e.message, 'Will not retry after 3 retries')
2122
}
2223
})
24+
25+
it('on certain retry, do something', async function () {
26+
try {
27+
let name = ''
28+
await asyncRetry({
29+
asyncFn: async (bail, num) => {
30+
if (num === 3) {
31+
name = 'vicky'
32+
return
33+
}
34+
35+
name = 'not vicky'
36+
throw new Error(`Test ${num}`)
37+
},
38+
options: {
39+
minTimeout: 0,
40+
maxTimeout: 100
41+
}
42+
})
43+
44+
assert.deepStrictEqual(name, 'vicky')
45+
} catch (e) {
46+
assert.fail('Should not have thrown')
47+
}
48+
})
49+
50+
it('final error message should be what is specified in num retry check', async function () {
51+
try {
52+
const retries = 3
53+
await asyncRetry({
54+
asyncFn: async (bail, num) => {
55+
console.log('Test num', num)
56+
if (num === retries + 1) {
57+
throw new Error('vicky')
58+
}
59+
60+
throw new Error(`Test ${num}`)
61+
},
62+
options: {
63+
minTimeout: 0,
64+
maxTimeout: 100,
65+
retries
66+
}
67+
})
68+
69+
assert.fail('Should have thrown')
70+
} catch (e) {
71+
assert.deepStrictEqual(e.message, 'vicky')
72+
}
73+
})
2374
})

0 commit comments

Comments
 (0)