Read-only mirror — the public repositories of the Forgejo instance, refreshed every 10 minutes. The live instance (fg.dieschoenewolke.de) is reachable only over the VPN; clone here with https://git.dieschoenewolke.de/git/<owner>/<repo>.git.
summaryrefslogtreecommitdiff
path: root/src/retry-helper.ts
blob: efb852b8bcb5d1ebee957894243df1f26bef8db5 (plain)
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
35
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))
}