send request options as json
parent
36669c3483
commit
32418bd064
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
43
src/main.ts
43
src/main.ts
|
@ -34,6 +34,9 @@ async function getBlacksmithAPIUrl(): Promise<AxiosInstance> {
|
||||||
let apiUrl = process.env.PETNAME?.includes('staging')
|
let apiUrl = process.env.PETNAME?.includes('staging')
|
||||||
? 'https://stagingapi.blacksmith.sh'
|
? 'https://stagingapi.blacksmith.sh'
|
||||||
: 'https://api.blacksmith.sh'
|
: 'https://api.blacksmith.sh'
|
||||||
|
core.info(`Using API URL: ${apiUrl}`);
|
||||||
|
core.info(`Using token: ${process.env.BLACKSMITH_STICKYDISK_TOKEN}`);
|
||||||
|
core.info(`Using repo name: ${process.env.GITHUB_REPO_NAME}`);
|
||||||
return axios.create({
|
return axios.create({
|
||||||
baseURL: apiUrl,
|
baseURL: apiUrl,
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -90,6 +93,32 @@ async function reportBuildFailed() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function postWithRetryToBlacksmithAPI(client: AxiosInstance, url: string, requestOptions: any, retryCondition: (error: AxiosError) => boolean): Promise<AxiosResponse> {
|
||||||
|
const maxRetries = 5;
|
||||||
|
const retryDelay = 100;
|
||||||
|
|
||||||
|
core.info(`Request options: ${JSON.stringify(requestOptions)}`);
|
||||||
|
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||||
|
try {
|
||||||
|
return await client.post(url, JSON.stringify(requestOptions), {
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-Github-Repo-Name': process.env.GITHUB_REPO_NAME || '',
|
||||||
|
'Authorization': `Bearer ${process.env.BLACKSMITH_CACHE_TOKEN}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (attempt === maxRetries || !retryCondition(error as AxiosError)) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
core.warning(`Request failed, retrying (${attempt}/${maxRetries})...`);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error('Max retries reached');
|
||||||
|
}
|
||||||
|
|
||||||
async function postWithRetry(client: AxiosInstance, url: string, formData: FormData, retryCondition: (error: AxiosError) => boolean): Promise<AxiosResponse> {
|
async function postWithRetry(client: AxiosInstance, url: string, formData: FormData, retryCondition: (error: AxiosError) => boolean): Promise<AxiosResponse> {
|
||||||
const maxRetries = 5;
|
const maxRetries = 5;
|
||||||
const retryDelay = 100;
|
const retryDelay = 100;
|
||||||
|
@ -261,16 +290,16 @@ async function getNumCPUs(): Promise<number> {
|
||||||
|
|
||||||
async function reportBlacksmithBuilderFailed(stickydiskKey: string) {
|
async function reportBlacksmithBuilderFailed(stickydiskKey: string) {
|
||||||
const client = await getBlacksmithAPIUrl();
|
const client = await getBlacksmithAPIUrl();
|
||||||
const formData = new FormData();
|
const requestOptions = {
|
||||||
const arch = process.env.PETNAME?.includes('arm') ? 'arm64' : 'amd64';
|
region: process.env.BLACKSMITH_REGION || 'eu-central',
|
||||||
formData.append('region', process.env.BLACKSMITH_REGION || 'eu-central');
|
stickyDiskKey: stickydiskKey,
|
||||||
formData.append('stickyDiskKey', stickydiskKey);
|
repoName: process.env.GITHUB_REPO_NAME || '',
|
||||||
formData.append('repoName', process.env.GITHUB_REPO_NAME || '');
|
arch: process.env.PETNAME?.includes('arm') ? 'arm64' : 'amd64'
|
||||||
formData.append('arch', arch);
|
};
|
||||||
const retryCondition = (error: AxiosError) => {
|
const retryCondition = (error: AxiosError) => {
|
||||||
return error.response?.status ? error.response.status > 500 : false;
|
return error.response?.status ? error.response.status > 500 : false;
|
||||||
};
|
};
|
||||||
const response = await postWithRetry(client, '/stickydisks/report-failed', formData, retryCondition);
|
const response = await postWithRetryToBlacksmithAPI(client, '/stickydisks/report-failed', requestOptions, retryCondition);
|
||||||
return response.data;
|
return response.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue