perf: no try-catch block on cloudflare cache purge

This commit is contained in:
Bobby Wibowo 2022-07-14 14:41:55 +07:00
parent edbb6ef9f0
commit ac63f8b76d
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -225,14 +225,27 @@ const cloudflarePurgeCacheQueue = cloudflareAuth && fastq.promise(async chunk =>
logger.log(`${prefix}: ${message}`) logger.log(`${prefix}: ${message}`)
} }
try { const response = await fetch(url, {
const purge = await fetch(url, {
method: 'POST', method: 'POST',
body: JSON.stringify({ files: chunk }), body: JSON.stringify({ files: chunk }),
headers headers
}) })
const response = await purge.json() .then(res => res.json())
.catch(error => error)
// If fetch errors out, instead of API responding with API errors
if (response instanceof Error) {
const errorString = response.toString()
if (i < MAX_TRIES - 1) {
_log(`${errorString}. Retrying in 5 seconds\u2026`)
await new Promise(resolve => setTimeout(resolve, 5000))
continue
}
result.errors = [errorString]
break
}
// If API reponds with API errors
const hasErrorsArray = Array.isArray(response.errors) && response.errors.length const hasErrorsArray = Array.isArray(response.errors) && response.errors.length
if (hasErrorsArray) { if (hasErrorsArray) {
const rateLimit = response.errors.find(error => /rate limit/i.test(error.message)) const rateLimit = response.errors.find(error => /rate limit/i.test(error.message))
@ -243,20 +256,11 @@ const cloudflarePurgeCacheQueue = cloudflareAuth && fastq.promise(async chunk =>
} }
} }
// If succeeds or out of retries
result.success = response.success result.success = response.success
result.errors = hasErrorsArray result.errors = hasErrorsArray
? response.errors.map(error => `${error.code}: ${error.message}`) ? response.errors.map(error => `${error.code}: ${error.message}`)
: [] : []
} catch (error) {
const errorString = error.toString()
if (i < MAX_TRIES - 1) {
_log(`${errorString}. Retrying in 5 seconds\u2026`)
await new Promise(resolve => setTimeout(resolve, 5000))
continue
}
result.errors = [errorString]
}
break break
} }
@ -742,9 +746,10 @@ self.purgeCloudflareCache = async (names, uploads, thumbs) => {
} }
const results = [] const results = []
await Promise.all(chunks.map(async chunk => for (const chunk of chunks) {
results.push(await cloudflarePurgeCacheQueue.push(chunk)) const result = await cloudflarePurgeCacheQueue.push(chunk)
)) results.push(result)
}
return results return results
} }