/*! * @license * Copyright Squiz Australia Pty Ltd. All Rights Reserved. */ import { Logger } from '@squiz/dx-logger-lib'; import { AxiosInstance } from 'axios'; import color from 'cli-color'; import { JobManifestV1Model } from '../../../../manifest'; import { handleResponse, isAxiosResponse } from './responseHandler'; import { checkIfVersionExists } from './versionChecker'; /** * Poll for upload result and handle retries * @param apiClient - Axios client for API calls * @param uploadId - Upload ID to check * @param manifest - Job manifest data * @param logger - Logger instance * @returns Upload result */ export async function pollForResult( apiClient: AxiosInstance, uploadId: string, manifest: JobManifestV1Model, logger: Logger, ): Promise { let result = await handleResponse(apiClient.post(`/upload-job/next`, { id: uploadId, manifest: manifest })); if (!isAxiosResponse(result)) { let retriesAllowed = 12; while (retriesAllowed > 0) { logger.info( `deployment id: ${uploadId} status: ${color.yellow( `unknown, retrying to check the upload status ${retriesAllowed} more times`, )}`, ); if (await checkIfVersionExists(apiClient, `/job/${manifest.name}/${manifest.version}`)) { result = { status: 'successful', }; break; } await new Promise((r) => setTimeout(r, 10000)); retriesAllowed--; } if (retriesAllowed === 0) { logger.error(`deployment id: ${uploadId} status: ${color.red('failed to check the upload status')}`); throw new Error(result.toString()); } } return result; }