Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TTAHUB-3097] Implement transactions for worker processes #2233

Merged
merged 25 commits into from
Jul 15, 2024

Conversation

GarrettEHill
Copy link
Collaborator

@GarrettEHill GarrettEHill commented Jun 27, 2024

Description of change

This update introduces transaction handling for worker processes in the Head Start TTA Data Platform. The main changes include:

  1. Implementing transaction management in worker scripts to ensure data consistency.
  2. Enhancing error logging for detailed information about errors and database connection states.
  3. Updating tests to align with the new transaction management and error handling.

How to test

  1. Run Unit Tests: Execute all unit tests to ensure they pass with the new transaction handling logic.
  2. Simulate Worker Processes: Trigger worker processes and monitor logs for detailed error messages.
  3. Check Database Consistency: Verify that data remains consistent and correctly rolled back in case of errors during worker processes.

Issue(s)

Checklists

Every PR

  • Meets issue criteria
  • JIRA ticket status updated
  • Code is meaningfully tested
  • Meets accessibility standards (WCAG 2.1 Levels A, AA)
  • API Documentation updated
  • Boundary diagram updated
  • Logical Data Model updated
  • Architectural Decision Records written for major infrastructure decisions
  • UI review complete

Before merge to main

  • OHS demo complete
  • Ready to create production PR

Production Deploy

  • Staging smoke test completed

After merge/deploy

  • Update JIRA ticket status

@GarrettEHill
Copy link
Collaborator Author

@thewatermethod @nvms @AdamAdHocTeam @kcirtapfromspace @kryswisnaskas @hardwarehuman
Its all passing now, ready for review... again

Copy link
Collaborator

@nvms nvms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's simplify transactionWrapper a little bit:

export default function transactionWrapper(originalFunction, context = '') {
  return async function wrapper(req, res, next) {
    const startTime = Date.now();
    try {
      // eslint-disable-next-line @typescript-eslint/return-await
      return await sequelize.transaction(async () => {
        try {
          await addAuditTransactionSettings(sequelize, null, null, 'transaction', originalFunction.name);
          const result = await originalFunction(req, res, next);
          const duration = Date.now() - startTime;
          auditLogger.info(`${originalFunction.name} ${context} execution time: ${duration}ms`);
          removeFromAuditedTransactions();
          return result;
        } catch (err) {
          auditLogger.error(`Error executing ${originalFunction.name} ${context}: ${err.message}`);
          throw err;
        }
      });
    } catch (err) {
      return handleErrors(req, res, err, logContext);
    }
  };
}

So we don't have to do that error = err thing, which isn't needed. Also the result can be returned inside the try, so it doesn't need to be in the upper scope. Just a bit easier to read IMO.

Copy link
Collaborator

@thewatermethod thewatermethod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All functionality looks to still be there/work 👍

@thewatermethod
Copy link
Collaborator

I'd grab Jon's changes to the sequelizeConnectionError stuff, or wait until that's merged into main, so we don't merge anything that throws errors in the error handler


try {
const responseBody = typeof error === 'object' && error !== null
? { ...error, errorStack: error?.stack }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't ...error also contain error.stack. Why do we need to put it in the property errorStack?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just matching what the original code had

let operation;
let label;

if (error instanceof Sequelize.Error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we check instanceof Sequelize.Error here but above we do operation !== 'SequelizeError'.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just matching original logic

* @param {Object} logContext - The context for logging.
*/
export const handleUnexpectedWorkerError = (job, error, logContext) => {
logger.error(`${logContext.namespace} - Unexpected error in catch block - ${error}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will call stack be visible here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think so

@@ -35,7 +40,8 @@ const processScanQueue = () => {
scanQueue.on('failed', onFailedScanQueue);
scanQueue.on('completed', onCompletedScanQueue);
increaseListeners(scanQueue);
scanQueue.process((job) => processFile(job.data.key));
const process = (job) => processFile(job.data.key);
scanQueue.process(transactionQueueWrapper(process));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to pass a second param here to the transactionQueueWrapper like in S3 and Resource queue's?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the scan queue never passed a key, so there is no associated value to use as a context

@GarrettEHill GarrettEHill merged commit be2c339 into main Jul 15, 2024
10 checks passed
@GarrettEHill GarrettEHill deleted the TTAHUB-3097/worker-transactions branch July 15, 2024 14:29
thewatermethod added a commit that referenced this pull request Jul 19, 2024
…ions"

This reverts commit be2c339, reversing
changes made to 98ac36a.
thewatermethod added a commit that referenced this pull request Jul 19, 2024
Revert "Merge pull request #2233 from HHS/TTAHUB-3097/worker-transact…
GarrettEHill added a commit that referenced this pull request Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants