2020-07-10 15:09:32 +00:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
import {HttpCodes} from '@actions/http-client'
|
|
|
|
import {
|
|
|
|
IHttpClientResponse,
|
|
|
|
ITypedResponse
|
|
|
|
} from '@actions/http-client/interfaces'
|
2020-08-17 20:32:04 +00:00
|
|
|
import {RetryDelay} from './constants'
|
2020-07-10 15:09:32 +00:00
|
|
|
|
|
|
|
export function isSuccessStatusCode(statusCode?: number): boolean {
|
|
|
|
if (!statusCode) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return statusCode >= 200 && statusCode < 300
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isServerErrorStatusCode(statusCode?: number): boolean {
|
|
|
|
if (!statusCode) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return statusCode >= 500
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isRetryableStatusCode(statusCode?: number): boolean {
|
|
|
|
if (!statusCode) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const retryableStatusCodes = [
|
|
|
|
HttpCodes.BadGateway,
|
|
|
|
HttpCodes.ServiceUnavailable,
|
|
|
|
HttpCodes.GatewayTimeout
|
|
|
|
]
|
|
|
|
return retryableStatusCodes.includes(statusCode)
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:32:04 +00:00
|
|
|
async function sleep(milliseconds: number): Promise<void> {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, milliseconds))
|
|
|
|
}
|
|
|
|
|
2020-07-10 15:09:32 +00:00
|
|
|
export async function retry<T>(
|
|
|
|
name: string,
|
|
|
|
method: () => Promise<T>,
|
|
|
|
getStatusCode: (arg0: T) => number | undefined,
|
2020-08-17 15:48:15 +00:00
|
|
|
maxAttempts = 2,
|
2020-08-17 20:32:04 +00:00
|
|
|
delay = RetryDelay,
|
2020-08-17 15:48:15 +00:00
|
|
|
onError: ((arg0: Error) => T | undefined) | undefined = undefined
|
2020-07-10 15:09:32 +00:00
|
|
|
): Promise<T> {
|
|
|
|
let errorMessage = ''
|
|
|
|
let attempt = 1
|
|
|
|
|
|
|
|
while (attempt <= maxAttempts) {
|
2020-08-17 15:48:15 +00:00
|
|
|
let response: T | undefined = undefined
|
|
|
|
let statusCode: number | undefined = undefined
|
|
|
|
let isRetryable = false
|
|
|
|
|
2020-07-10 15:09:32 +00:00
|
|
|
try {
|
|
|
|
response = await method()
|
2020-08-17 15:48:15 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (onError) {
|
|
|
|
response = onError(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
isRetryable = true
|
|
|
|
errorMessage = error.message
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response) {
|
2020-07-10 15:09:32 +00:00
|
|
|
statusCode = getStatusCode(response)
|
|
|
|
|
|
|
|
if (!isServerErrorStatusCode(statusCode)) {
|
|
|
|
return response
|
|
|
|
}
|
2020-08-17 15:48:15 +00:00
|
|
|
}
|
2020-07-10 15:09:32 +00:00
|
|
|
|
2020-08-17 15:48:15 +00:00
|
|
|
if (statusCode) {
|
2020-07-10 15:09:32 +00:00
|
|
|
isRetryable = isRetryableStatusCode(statusCode)
|
|
|
|
errorMessage = `Cache service responded with ${statusCode}`
|
|
|
|
}
|
2020-08-17 20:32:04 +00:00
|
|
|
|
2020-07-10 15:09:32 +00:00
|
|
|
core.debug(
|
|
|
|
`${name} - Attempt ${attempt} of ${maxAttempts} failed with error: ${errorMessage}`
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!isRetryable) {
|
|
|
|
core.debug(`${name} - Error is not retryable`)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:32:04 +00:00
|
|
|
await sleep(delay)
|
2020-07-10 15:09:32 +00:00
|
|
|
attempt++
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Error(`${name} failed: ${errorMessage}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function retryTypedResponse<T>(
|
|
|
|
name: string,
|
|
|
|
method: () => Promise<ITypedResponse<T>>,
|
|
|
|
maxAttempts = 2
|
|
|
|
): Promise<ITypedResponse<T>> {
|
|
|
|
return await retry(
|
|
|
|
name,
|
|
|
|
method,
|
|
|
|
(response: ITypedResponse<T>) => response.statusCode,
|
2020-08-17 15:48:15 +00:00
|
|
|
maxAttempts,
|
2020-08-17 20:32:04 +00:00
|
|
|
RetryDelay,
|
2020-08-17 15:48:15 +00:00
|
|
|
// If the error object contains the statusCode property, extract it and return
|
2020-08-17 20:32:04 +00:00
|
|
|
// an ITypedResponse<T> so it can be processed by the retry logic.
|
2020-08-17 15:48:15 +00:00
|
|
|
(e: Error) => {
|
2020-08-17 20:32:04 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const error = e as any
|
2020-08-17 15:48:15 +00:00
|
|
|
if (error['statusCode']) {
|
|
|
|
return {
|
|
|
|
statusCode: error['statusCode'],
|
|
|
|
result: null,
|
|
|
|
headers: {}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 15:09:32 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function retryHttpClientResponse<T>(
|
|
|
|
name: string,
|
|
|
|
method: () => Promise<IHttpClientResponse>,
|
|
|
|
maxAttempts = 2
|
|
|
|
): Promise<IHttpClientResponse> {
|
|
|
|
return await retry(
|
|
|
|
name,
|
|
|
|
method,
|
|
|
|
(response: IHttpClientResponse) => response.message.statusCode,
|
2020-08-17 20:32:04 +00:00
|
|
|
maxAttempts,
|
|
|
|
RetryDelay
|
2020-07-10 15:09:32 +00:00
|
|
|
)
|
|
|
|
}
|