diff --git a/src/tasks/sync-registries.js b/src/tasks/sync-registries.js index 88ecd1c5..ce47df21 100644 --- a/src/tasks/sync-registries.js +++ b/src/tasks/sync-registries.js @@ -385,23 +385,34 @@ const syncOrganizationAudit = async (organization) => { } if (organization.orgUid === homeOrg?.orgUid) { - const stagingUuid = [ - 'unit', - 'project', - 'units', - 'projects', - ].includes(modelKey) - ? primaryKeyValue - : undefined; - - if (stagingUuid) { - afterCommitCallbacks.push(async () => { - logger.info(`DELETING STAGING: ${stagingUuid}`); - await Staging.destroy({ - where: { uuid: stagingUuid }, - }); - }); - } + 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( + 'MAXIMUM TRUNCATION ATTEMPTS REACHED, GIVING UP', + ); + } + } + } + }); } }