-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmaintenance-mode.ts
More file actions
34 lines (30 loc) · 1.04 KB
/
maintenance-mode.ts
File metadata and controls
34 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const RETRY_DELAY_KEY = Symbol('retryDelay')
export const onError = axios => async (error) => {
const { config, response, request } = error
const responseURL = request?.responseURL
const status = response?.status
const headers = response?.headers
/**
* Retry requests if they failed due to maintenance mode
*
* The delay is exponential. It starts at 2s and then doubles
* until a final retry after 32s. This results in roughly 1m of
* retries until we give up and throw the axios error towards
* the caller.
*/
if (status === 503
&& headers['x-nextcloud-maintenance-mode'] === '1'
&& config.retryIfMaintenanceMode
&& (!config[RETRY_DELAY_KEY] || config[RETRY_DELAY_KEY] <= 32)) {
const retryDelay = (config[RETRY_DELAY_KEY] ?? 1) * 2
console.warn(`Request to ${responseURL} failed because of maintenance mode. Retrying in ${retryDelay}s`)
await new Promise((resolve, _) => {
setTimeout(resolve, retryDelay*1000)
})
return axios({
...config,
[RETRY_DELAY_KEY]: retryDelay,
})
}
return Promise.reject(error)
}