mirror of
https://git.mirrors.martin98.com/https://github.com/cyberman54/curl
synced 2025-08-22 02:59:13 +08:00
add retry feature
This commit is contained in:
parent
9f85844a4a
commit
7bcf97e139
@ -29,6 +29,8 @@ inputs:
|
|||||||
is_debug:
|
is_debug:
|
||||||
description: 'show debug message of response'
|
description: 'show debug message of response'
|
||||||
default: false
|
default: false
|
||||||
|
retry:
|
||||||
|
default: 1/1
|
||||||
custom-config:
|
custom-config:
|
||||||
description: custom config
|
description: custom config
|
||||||
|
|
||||||
|
11
src/index.ts
11
src/index.ts
@ -1,11 +1,9 @@
|
|||||||
|
|
||||||
import axios from 'axios'
|
import axios, { AxiosRequestConfig } from 'axios'
|
||||||
import config from './requestconf'
|
import config from './requestconf'
|
||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import { AxiosRequestConfig } from 'axios'
|
|
||||||
import setOutput from './output'
|
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
|
import { sendRequestWithRetry } from './util'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if(core.getInput('custom-config')){
|
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')
|
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;
|
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{
|
}else{
|
||||||
axios(config).then(res => setOutput(res)).catch(err => core.setFailed(err.message))
|
sendRequestWithRetry(config)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
core.setFailed(err.message);
|
core.setFailed(err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
31
src/util.ts
31
src/util.ts
@ -1,5 +1,6 @@
|
|||||||
import * as core from '@actions/core'
|
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 => {
|
export const validateStatusCode = (actualStatusCode: string): void => {
|
||||||
const acceptedStatusCode: string[] = core.getInput('accept')
|
const acceptedStatusCode: string[] = core.getInput('accept')
|
||||||
@ -18,4 +19,32 @@ export const buildOutput = (res: AxiosResponse <any>): 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user