Skip to content

#51 Commit Submission in worker thread#195

Merged
leoraba merged 14 commits intomainfrom
commit_submission_worker
Mar 20, 2026
Merged

#51 Commit Submission in worker thread#195
leoraba merged 14 commits intomainfrom
commit_submission_worker

Conversation

@leoraba
Copy link
Copy Markdown
Contributor

@leoraba leoraba commented Mar 3, 2026

Summary

This PR improves the performance of the submission commit process.

Issues

Description of Changes

Database changes

  • Added indices to tables submission, submitted_data and audit_submitted_data to improve data lookup
  • Added new Submission status COMMITTING, to indicate a submission is being processed from status VALID to COMMITTED

Worker threads

  • Added new dependency workerpool to manage a pool of workers.
  • Implemented an execution of worker thread to commit a submission. (CPU intensive process running out of the main thread) to keep the main thread available.

@leoraba leoraba marked this pull request as draft March 5, 2026 16:19
Comment on lines +345 to +346
const invalidRecordErrors = findInvalidRecordErrorsBySchemaName(resultValidation, entityName);
const hasErrorByIndex = groupErrorsByIndex(invalidRecordErrors);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Move calling these 2 functions out of the for loop to improve the performance.
During local testing, this change improve 15x the performance during commit operation.

Comment on lines +454 to +459
return {
submissionId: submission.id,
organization: submission.organization,
categoryId: submission.dictionaryCategory.id,
data: resultCommit,
};
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the execution of params.onFinishCommit from here, since this now runs on a worker thread and it doesn't have access to this function, instead this function just returns the data object

categoryId,
submission?.organization,
);
await submissionRepository.update(submissionId, { status: SUBMISSION_STATUS.COMMITTING, updatedBy: username });
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

submission is updated to COMMITTING status when the commit operation is called

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

refactor: updated this function to handle large arrays of submitted data more efficiently by replacing the spread operator, which previously created new arrays during each iteration and was using memory inefficiently.

Comment on lines +13 to +15
export interface WorkerFunctions {
commitSubmission(input: CommitWorkerInput): Promise<void>;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

functions exposed are ran on a worker thread pool, more functions can be added later.

@leoraba leoraba marked this pull request as ready for review March 10, 2026 20:51
@leoraba leoraba changed the title #51 Submission Validation in worker thread #51 Commit Submission in worker thread Mar 11, 2026
Copy link
Copy Markdown
Contributor

@joneubank joneubank left a comment

Choose a reason for hiding this comment

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

Really great foundation for our worker setup. Love that this runs successfully. Most of my comments are for dealing with configuration and setup issue edge cases. There's a few changes to look at for server stability and completeness.

Comment on lines +104 to +106
// Reset the submission status back to VALID so it can be retried
await submissionRepo.update(submissionId, { status: SUBMISSION_STATUS.VALID, updatedBy: username });
throw error;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When an error occurs during committing a submission, it is back to VALID status.

* @param configData The application configuration
* @returns The worker functions to execute tasks in the worker pool
*/
export const createWorkerPool = (configData: AppConfig): WorkerFunctions => {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the main function used to initialize the worker pool, it is used when the Lyric provider is created, returns the functions available in the baseDependency in the provider.

If any error occurs during worker pool initialization an error is thrown immediately that stops the application

@leoraba leoraba requested a review from joneubank March 18, 2026 13:11
Copy link
Copy Markdown
Contributor

@joneubank joneubank left a comment

Choose a reason for hiding this comment

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

Amazing. Almost there. We added the shutdown mechanism, but now we need to use it in our sever and our docs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

great documentation updates, will be very helpful. much appreciated.

Comment on lines +74 to +77
terminate: async (): Promise<void> => {
await pool.terminate();
logger.info(LOG_MODULE, 'Worker pool terminated');
},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should make sure this is used in our lyric server code. We should also update the documentation in the lyric provider README so that we show how to perform clean up properly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added in the Server a call to this function to gracefully terminate the workers.

Docs have been updated to explain the usage.

Copy link
Copy Markdown
Contributor

@joneubank joneubank left a comment

Choose a reason for hiding this comment

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

Couple more comments on error handling. No requests for change, use your best judgement of whats necessary.

.then(() => logger.info(LOG_MODULE, `Worker pool execution succeeded for submission ${submissionId}`))
.catch((error) => {
logger.error(LOG_MODULE, `Worker pool execution failed for submission ${submissionId}: ${error}`);
throw new InternalServerError(`Commit submission failed: ${error}`);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How are errors thrown from the worker pool functions handled? Is there a built in handler for errors thrown from workers? At a glance it seems like this won't be caught or handled by anything (may even crash the server).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This throw indeed is crashing the server, will remove the then and catch from here as it's already handled by the worker when if fails or succeds.

Adittionally, The worker should not trhow any error.

message,
);
logger.error(error);
throw new Error(`${message}`, { cause: error });
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Any reason to throw a generic Error instead of just throwing the original error?

Suggested change
throw new Error(`${message}`, { cause: error });
throw error;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

changed to re throw the original error!

@leoraba
Copy link
Copy Markdown
Contributor Author

leoraba commented Mar 19, 2026

PR updated. ready for code review :)

@leoraba leoraba requested a review from joneubank March 19, 2026 21:14
Copy link
Copy Markdown
Contributor

@joneubank joneubank left a comment

Choose a reason for hiding this comment

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

YESSSS LETS GO!

@leoraba leoraba merged commit 14ddd0e into main Mar 20, 2026
2 checks passed
@leoraba leoraba deleted the commit_submission_worker branch March 20, 2026 12:45
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.

Prod Issue: Loading the submission details page takes several minutes

2 participants