From 49ae20f0ed4b4fdac51db2883a632ce0e07a8bd8 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Fri, 10 May 2024 15:57:39 -0400 Subject: [PATCH] fix: truncate staging only once --- src/tasks/sync-registries.js | 52 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/tasks/sync-registries.js b/src/tasks/sync-registries.js index ce47df21..cba338cf 100644 --- a/src/tasks/sync-registries.js +++ b/src/tasks/sync-registries.js @@ -383,37 +383,37 @@ const syncOrganizationAudit = async (organization) => { mirrorTransaction, }); } + } - if (organization.orgUid === homeOrg?.orgUid) { - afterCommitCallbacks.push(async () => { - logger.info(`ATTEMPTING TO TRUNCATE STAGING TABLE`); - - let success = false; - let attempts = 0; - const maxAttempts = 5; // Set a maximum number of attempts to avoid infinite loops - - while (!success && attempts < maxAttempts) { - try { - await Staging.truncate(); - success = true; // If truncate succeeds, set success to true to exit the loop - logger.info('STAGING TABLE TRUNCATED SUCCESSFULLY'); - } catch (error) { - attempts++; + if (organization.orgUid === homeOrg?.orgUid) { + afterCommitCallbacks.push(async () => { + logger.info(`ATTEMPTING TO TRUNCATE STAGING TABLE`); + + let success = false; + let attempts = 0; + const maxAttempts = 5; // Set a maximum number of attempts to avoid infinite loops + + while (!success && attempts < maxAttempts) { + try { + await Staging.truncate(); + success = true; // If truncate succeeds, set success to true to exit the loop + logger.info('STAGING TABLE TRUNCATED SUCCESSFULLY'); + } catch (error) { + attempts++; + logger.error( + `TRUNCATION FAILED ON ATTEMPT ${attempts}: ${error.message}`, + ); + if (attempts < maxAttempts) { + logger.info('WAITING 1 SECOND BEFORE RETRYING...'); + await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second + } else { logger.error( - `TRUNCATION FAILED ON ATTEMPT ${attempts}: ${error.message}`, + 'MAXIMUM TRUNCATION ATTEMPTS REACHED, GIVING UP', ); - if (attempts < maxAttempts) { - logger.info('WAITING 1 SECOND BEFORE RETRYING...'); - await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second - } else { - logger.error( - 'MAXIMUM TRUNCATION ATTEMPTS REACHED, GIVING UP', - ); - } } } - }); - } + } + }); } // Create the Audit record