From 28dfb44b145c537aba07ae73cb1287f25532022a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 8 Jun 2021 17:29:45 +0200 Subject: Try to speed up AP update transaction --- server/helpers/database-utils.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index 7befa2c49..240b18033 100644 --- a/server/helpers/database-utils.ts +++ b/server/helpers/database-utils.ts @@ -58,7 +58,7 @@ function transactionRetryer (func: (err: any, data: T) => any) { errorFilter: err => { const willRetry = (err.name === 'SequelizeDatabaseError') - logger.debug('Maybe retrying the transaction function.', { willRetry, err }) + logger.debug('Maybe retrying the transaction function.', { willRetry, err, tags: [ 'sql', 'retry' ] }) return willRetry } }, @@ -68,6 +68,8 @@ function transactionRetryer (func: (err: any, data: T) => any) { }) } +// --------------------------------------------------------------------------- + function updateInstanceWithAnother > (instanceToUpdate: T, baseInstance: U) { const obj = baseInstance.toJSON() @@ -82,12 +84,6 @@ function resetSequelizeInstance (instance: Model, savedFields: object) { }) } -function afterCommitIfTransaction (t: Transaction, fn: Function) { - if (t) return t.afterCommit(() => fn()) - - return fn() -} - function deleteNonExistingModels > ( fromDatabase: T[], newModels: T[], @@ -111,6 +107,18 @@ function setAsUpdated (table: string, id: number, transaction?: Transaction) { // --------------------------------------------------------------------------- +function runInReadCommittedTransaction (fn: (t: Transaction) => Promise) { + return sequelizeTypescript.transaction(t => fn(t)) +} + +function afterCommitIfTransaction (t: Transaction, fn: Function) { + if (t) return t.afterCommit(() => fn()) + + return fn() +} + +// --------------------------------------------------------------------------- + export { resetSequelizeInstance, retryTransactionWrapper, @@ -118,5 +126,6 @@ export { updateInstanceWithAnother, afterCommitIfTransaction, deleteNonExistingModels, - setAsUpdated + setAsUpdated, + runInReadCommittedTransaction } -- cgit v1.2.3