mirror of
https://github.com/actions/checkout.git
synced 2025-12-26 17:14:41 +00:00
.
This commit is contained in:
36
src/retry-helper.ts
Normal file
36
src/retry-helper.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import * as core from '@actions/core'
|
||||
|
||||
const maxAttempts = 3
|
||||
const minSeconds = 10
|
||||
const maxSeconds = 20
|
||||
|
||||
export async function execute<T>(action: () => Promise<T>): Promise<T> {
|
||||
let attempt = 1
|
||||
while (attempt < maxAttempts) {
|
||||
// Try
|
||||
try {
|
||||
return await action()
|
||||
} catch (err) {
|
||||
core.info(err.message)
|
||||
}
|
||||
|
||||
// Sleep
|
||||
const seconds = getRandomIntInclusive(minSeconds, maxSeconds)
|
||||
core.info(`Waiting ${seconds} before trying again`)
|
||||
await sleep(seconds * 1000)
|
||||
attempt++
|
||||
}
|
||||
|
||||
// Last attempt
|
||||
return await action()
|
||||
}
|
||||
|
||||
function getRandomIntInclusive(minimum: number, maximum: number): number {
|
||||
minimum = Math.floor(minimum)
|
||||
maximum = Math.floor(maximum)
|
||||
return Math.floor(Math.random() * (maximum - minimum + 1)) + minimum
|
||||
}
|
||||
|
||||
async function sleep(milliseconds): Promise<void> {
|
||||
return new Promise(resolve => setTimeout(resolve, milliseconds))
|
||||
}
|
||||
Reference in New Issue
Block a user