mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-05 19:40:09 +00:00
try cf purge up to 3 times
with preset delay in between (60 secs if rate limited, 5 secs for unexpected errors) an alternative to global queue mechanism in #342 not perfect, but easier to implement closes #342
This commit is contained in:
parent
3a0810b0be
commit
d7b11e7e56
@ -525,8 +525,8 @@ self.purgeCloudflareCache = async (names, uploads, thumbs) => {
|
|||||||
|
|
||||||
// Split array into multiple arrays with max length of 30 URLs
|
// Split array into multiple arrays with max length of 30 URLs
|
||||||
// https://api.cloudflare.com/#zone-purge-files-by-url
|
// https://api.cloudflare.com/#zone-purge-files-by-url
|
||||||
// TODO: Handle API rate limits
|
|
||||||
const MAX_LENGTH = 30
|
const MAX_LENGTH = 30
|
||||||
|
const MAX_TRIES = 3 // only for rate limit and unexpected errors
|
||||||
const chunks = []
|
const chunks = []
|
||||||
while (names.length) {
|
while (names.length) {
|
||||||
chunks.push(names.splice(0, MAX_LENGTH))
|
chunks.push(names.splice(0, MAX_LENGTH))
|
||||||
@ -542,7 +542,6 @@ self.purgeCloudflareCache = async (names, uploads, thumbs) => {
|
|||||||
errors: []
|
errors: []
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
const headers = {
|
const headers = {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
}
|
||||||
@ -555,22 +554,49 @@ self.purgeCloudflareCache = async (names, uploads, thumbs) => {
|
|||||||
headers['X-Auth-Email'] = config.cloudflare.email
|
headers['X-Auth-Email'] = config.cloudflare.email
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < MAX_TRIES; i++) {
|
||||||
|
const _log = message => {
|
||||||
|
let prefix = `[CF]: ${i + 1}/${MAX_TRIES}: ${path.basename(chunk[0])}`
|
||||||
|
if (chunk.length > 1) prefix += ',\u2026'
|
||||||
|
logger.log(`${prefix}: ${message}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const purge = 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()
|
const response = await purge.json()
|
||||||
result.success = response.success
|
|
||||||
if (Array.isArray(response.errors) && response.errors.length) {
|
const hasErrorsArray = Array.isArray(response.errors) && response.errors.length
|
||||||
result.errors = response.errors.map(error => `${error.code}: ${error.message}`)
|
if (hasErrorsArray) {
|
||||||
|
const rateLimit = response.errors.find(error => /rate limit/i.test(error.message))
|
||||||
|
if (rateLimit && i < MAX_TRIES - 1) {
|
||||||
|
_log(`${rateLimit.code}: ${rateLimit.message}. Retrying in a minute\u2026`)
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 60000))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result.success = response.success
|
||||||
|
result.errors = hasErrorsArray
|
||||||
|
? response.errors.map(error => `${error.code}: ${error.message}`)
|
||||||
|
: []
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
result.errors = [error.toString()]
|
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]
|
||||||
}
|
}
|
||||||
|
|
||||||
results.push(result)
|
results.push(result)
|
||||||
|
break
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
Loading…
Reference in New Issue
Block a user