async pollTransformation()

in server/aws-lsp-codewhisperer/src/language-server/netTransform/transformHandler.ts [304:377]


    async pollTransformation(request: GetTransformRequest, validExitStatus: string[], failureStates: string[]) {
        let timer = 0
        let getTransformAttempt = 0
        let getTransformMaxAttempts = 3
        const getCodeTransformationRequest = {
            transformationJobId: request.TransformationJobId,
        } as GetTransformationRequest
        let response = await this.serviceManager
            .getCodewhispererService()
            .codeModernizerGetCodeTransformation(getCodeTransformationRequest)
        this.logging.log('Start polling for transformation plan.')
        this.logging.log('The valid status to exit polling are: ' + validExitStatus)
        this.logging.log('The failure status are: ' + failureStates)

        this.logging.log('Transformation status: ' + response.transformationJob?.status)
        let status = response?.transformationJob?.status ?? PollTransformationStatus.NOT_FOUND

        while (status != PollTransformationStatus.TIMEOUT && !failureStates.includes(status)) {
            try {
                if (this.cancelPollingEnabled) {
                    // Reset the flag
                    this.cancelPollingEnabled = false
                    return {
                        TransformationJob: response.transformationJob,
                    } as GetTransformResponse
                }
                const apiStartTime = Date.now()

                const getCodeTransformationRequest = {
                    transformationJobId: request.TransformationJobId,
                } as GetTransformationRequest
                response = await this.serviceManager
                    .getCodewhispererService()
                    .codeModernizerGetCodeTransformation(getCodeTransformationRequest)
                this.logging.log('Transformation status: ' + response.transformationJob?.status)

                if (validExitStatus.includes(status)) {
                    this.logging.log('Exiting polling for transformation plan with transformation status: ' + status)
                    break
                }

                status = response.transformationJob.status!
                await this.sleep(10 * 1000)
                timer += 10

                if (timer > 24 * 3600 * 1000) {
                    status = PollTransformationStatus.TIMEOUT
                    break
                }
                getTransformAttempt = 0 // a successful polling will reset attempt
            } catch (e: any) {
                const errorMessage = (e as Error).message ?? 'Error in GetTransformation API call'
                this.logging.log('Error polling transformation job from the server: ' + errorMessage)

                getTransformAttempt += 1
                if (getTransformAttempt >= getTransformMaxAttempts) {
                    this.logging.log(`GetTransformation failed after ${getTransformMaxAttempts} attempts.`)
                    status = PollTransformationStatus.NOT_FOUND
                    break
                }

                const expDelayMs = this.getExpDelayForApiRetryMs(getTransformAttempt)
                this.logging.log(
                    `Attempt ${getTransformAttempt}/${getTransformMaxAttempts} to get transformation plan failed, retry in ${expDelayMs} seconds`
                )
                await this.sleep(expDelayMs * 1000)
            }
        }
        this.logging.log('Returning response from server : ' + JSON.stringify(response))
        this.logSuggestionForFailureResponse(request, response.transformationJob, failureStates)
        return {
            TransformationJob: response.transformationJob,
        } as GetTransformResponse
    }