Skip to content

Commit

Permalink
fix: truncate staging only once
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed May 10, 2024
1 parent 49ae20f commit 2ff211d
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions src/tasks/sync-registries.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ const tryParseJSON = (jsonString, defaultValue) => {
}
};

const truncateStaging = 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');
}
}
}
};

const syncOrganizationAudit = async (organization) => {
try {
let afterCommitCallbacks = [];
Expand Down Expand Up @@ -385,37 +412,6 @@ const syncOrganizationAudit = async (organization) => {
}
}

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(
'MAXIMUM TRUNCATION ATTEMPTS REACHED, GIVING UP',
);
}
}
}
});
}

// Create the Audit record
await Audit.create(auditData, { transaction, mirrorTransaction });
await Organization.update(
Expand All @@ -430,6 +426,10 @@ const syncOrganizationAudit = async (organization) => {
}
};

if (organization.orgUid === homeOrg?.orgUid) {
afterCommitCallbacks.push(truncateStaging);
}

await createTransaction(updateTransaction, afterCommitCallbacks);
} catch (error) {
logger.error('Error syncing org audit', error);
Expand Down

0 comments on commit 2ff211d

Please sign in to comment.