diff --git a/action.yml b/action.yml index b377869..dcc89e9 100644 --- a/action.yml +++ b/action.yml @@ -29,6 +29,8 @@ inputs: is_debug: description: 'show debug message of response' default: false + retry: + default: 1/1 custom-config: description: custom config diff --git a/src/index.ts b/src/index.ts index 0abfc52..69467c0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,9 @@ -import axios from 'axios' +import axios, { AxiosRequestConfig } from 'axios' import config from './requestconf' import * as core from '@actions/core' -import { AxiosRequestConfig } from 'axios' -import setOutput from './output' import * as fs from 'fs' - +import { sendRequestWithRetry } from './util' try { if(core.getInput('custom-config')){ @@ -20,10 +18,11 @@ try { throw new Error('Config file not found, meybe you need to use action/checkout before this step or there is typo on file name') } let customConfig: AxiosRequestConfig = JSON.parse(fs.readFileSync(path).toString()) as AxiosRequestConfig; - axios(customConfig).then(res => setOutput(res)).catch(err => core.setFailed(err.message)) + sendRequestWithRetry(customConfig) }else{ - axios(config).then(res => setOutput(res)).catch(err => core.setFailed(err.message)) + sendRequestWithRetry(config) } } catch (err) { core.setFailed(err.message); } + diff --git a/src/util.ts b/src/util.ts index 17e83c9..5f9a772 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,5 +1,6 @@ import * as core from '@actions/core' -import { AxiosResponse } from 'axios' +import axios, { AxiosResponse, AxiosRequestConfig } from 'axios' +import setOutput from './output' export const validateStatusCode = (actualStatusCode: string): void => { const acceptedStatusCode: string[] = core.getInput('accept') @@ -18,4 +19,32 @@ export const buildOutput = (res: AxiosResponse ): string => { }) } +export const sendRequestWithRetry = (config: AxiosRequestConfig): void => { + var exit = false + var countRetry = 0 + const retryArr: string[] = core.getInput('retry').split('/') + const numberOfRetry: number = Number(retryArr[0]) + const backoff: number = Number(retryArr[1]) + do{ + axios(config) + .then(res => { + exit = true + setOutput(res) + }) + .catch(err => { + countRetry += 1 + core.info(`retry: ${countRetry}`) + if(countRetry <= numberOfRetry){ + sleep(backoff) + }else{ + exit = true + core.setFailed(err) + } + }) + }while(!exit) +} + +function sleep(ms: number) { + return new Promise(resolve => setTimeout(resolve, ms)); + }