From 046b4f568960e96505fb08c212a788a751ad5d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lz=2Esir=CE=94rthurmoney=28=29?= Date: Fri, 23 Feb 2024 09:55:06 -0800 Subject: [PATCH] Updating backoff policy to use retry in backoffOptions --- .../src/oapp/typescript/typescript.ts | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/packages/ua-devtools-evm-hardhat/src/oapp/typescript/typescript.ts b/packages/ua-devtools-evm-hardhat/src/oapp/typescript/typescript.ts index 52298196e..a34febef7 100644 --- a/packages/ua-devtools-evm-hardhat/src/oapp/typescript/typescript.ts +++ b/packages/ua-devtools-evm-hardhat/src/oapp/typescript/typescript.ts @@ -460,36 +460,24 @@ export const generateLzConfig = async ( * @returns {Promise} - A promise resolving to the result of the operation if successful. * @throws {Error} - Throws an error if all attempts fail. */ -async function basicRetryPolicy( - fn: () => Promise, - maxAttempts: number = 3, - baseDelay: number = 1000, - maxDelay: number = 15000 -): Promise { +async function basicRetryPolicy(fn: () => Promise, maxAttempts: number = 3): Promise { const operation = async () => { return await fn() } const backoffOptions = { numOfAttempts: maxAttempts, - startingDelay: baseDelay, - delayFirstAttempt: true, - maxDelay: maxDelay, - factor: 2, - randomisationFactor: 0.5, - } - - for (let attempt = 0; attempt < maxAttempts; attempt++) { - try { - logger.verbose(`Attempt ${attempt + 1}/${maxAttempts}`) - return await backOff(operation, backoffOptions) - } catch (error) { - logger.info(`Attempt ${attempt + 1} failed with error: ${error}`) - if (attempt < maxAttempts - 1) { + retry: (e: any, attemptNumber: number) => { + logger.info(`Attempt ${attemptNumber}/${maxAttempts} failed with error: ${e}`) + if (attemptNumber < maxAttempts) { logger.info('Retrying...') + return true + } else { + logger.info(`All ${maxAttempts} attempts failed.`) + return false } - } + }, } - throw new Error(`All ${maxAttempts} attempts failed`) + return await backOff(operation, backoffOptions) }