From ea64d5ce08892f941cb58c78ffbe837d2226951a Mon Sep 17 00:00:00 2001 From: 13bfrancis <40218571+13bfrancis@users.noreply.github.com> Date: Tue, 28 Jan 2025 11:52:54 -0500 Subject: [PATCH] try --- lib/lambda/processEmails.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/lambda/processEmails.ts b/lib/lambda/processEmails.ts index 8d0a9d80b..1dc891331 100644 --- a/lib/lambda/processEmails.ts +++ b/lib/lambda/processEmails.ts @@ -221,7 +221,27 @@ export async function processAndSendEmails( }); const sec = await getSecret(config.emailAddressLookupSecretName); - const item = await os.getItem(config.osDomain, getOsNamespace("main"), id); + async function retry(fn: () => Promise, retries: number, delay: number): Promise { + for (let attempt = 0; attempt < retries; attempt++) { + try { + return await fn(); // Try to execute the function + } catch (error) { + if (attempt < retries - 1) { + console.warn(`Attempt ${attempt + 1} failed. Retrying in ${delay}ms...`); + await new Promise((res) => setTimeout(res, delay)); // Wait before retrying + } else { + throw error; // Re-throw error after exhausting retries + } + } + } + throw new Error("Retry mechanism failed unexpectedly."); // Safety guard + } + + const item = await retry( + () => os.getItem(config.osDomain, getOsNamespace("main"), id), + 10, + 10 * 1000, + ); if (!item?.found || !item?._source) { console.log(`The package was not found for id: ${id}. Doing nothing.`);