2023-07-31 08:34:36 -04:00
|
|
|
import retry from 'async/retry.js'
|
2021-08-27 08:32:44 -04:00
|
|
|
import Bluebird from 'bluebird'
|
2021-07-28 04:32:40 -04:00
|
|
|
import { Transaction } from 'sequelize'
|
2018-08-14 09:28:30 -04:00
|
|
|
import { Model } from 'sequelize-typescript'
|
2023-07-31 08:34:36 -04:00
|
|
|
import { sequelizeTypescript } from '@server/initializers/database.js'
|
|
|
|
import { logger } from './logger.js'
|
2017-01-17 14:38:45 -05:00
|
|
|
|
2021-01-21 10:57:21 -05:00
|
|
|
function retryTransactionWrapper <T, A, B, C, D> (
|
2021-07-06 10:02:11 -04:00
|
|
|
functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise<T>,
|
2021-01-21 10:57:21 -05:00
|
|
|
arg1: A,
|
|
|
|
arg2: B,
|
|
|
|
arg3: C,
|
|
|
|
arg4: D,
|
|
|
|
): Promise<T>
|
|
|
|
|
2018-06-13 08:27:40 -04:00
|
|
|
function retryTransactionWrapper <T, A, B, C> (
|
2021-07-06 10:02:11 -04:00
|
|
|
functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise<T>,
|
2018-06-13 08:27:40 -04:00
|
|
|
arg1: A,
|
|
|
|
arg2: B,
|
|
|
|
arg3: C
|
|
|
|
): Promise<T>
|
|
|
|
|
|
|
|
function retryTransactionWrapper <T, A, B> (
|
2021-07-06 10:02:11 -04:00
|
|
|
functionToRetry: (arg1: A, arg2: B) => Promise<T>,
|
2018-06-13 08:27:40 -04:00
|
|
|
arg1: A,
|
|
|
|
arg2: B
|
|
|
|
): Promise<T>
|
|
|
|
|
|
|
|
function retryTransactionWrapper <T, A> (
|
2021-07-06 10:02:11 -04:00
|
|
|
functionToRetry: (arg1: A) => Promise<T>,
|
2018-06-13 08:27:40 -04:00
|
|
|
arg1: A
|
|
|
|
): Promise<T>
|
|
|
|
|
2018-06-14 12:06:56 -04:00
|
|
|
function retryTransactionWrapper <T> (
|
|
|
|
functionToRetry: () => Promise<T> | Bluebird<T>
|
|
|
|
): Promise<T>
|
|
|
|
|
2017-11-21 07:43:29 -05:00
|
|
|
function retryTransactionWrapper <T> (
|
2021-07-06 10:02:11 -04:00
|
|
|
functionToRetry: (...args: any[]) => Promise<T>,
|
2018-06-13 08:27:40 -04:00
|
|
|
...args: any[]
|
2017-11-21 07:43:29 -05:00
|
|
|
): Promise<T> {
|
|
|
|
return transactionRetryer<T>(callback => {
|
2018-06-14 12:06:56 -04:00
|
|
|
functionToRetry.apply(null, args)
|
2017-11-21 07:43:29 -05:00
|
|
|
.then((result: T) => callback(null, result))
|
2017-07-05 07:26:25 -04:00
|
|
|
.catch(err => callback(err))
|
2017-07-11 11:04:57 -04:00
|
|
|
})
|
2017-07-05 07:26:25 -04:00
|
|
|
.catch(err => {
|
2022-07-13 04:59:55 -04:00
|
|
|
logger.warn(`Cannot execute ${functionToRetry.name} with many retries.`, { err })
|
2017-11-16 09:22:39 -05:00
|
|
|
throw err
|
2017-07-05 07:26:25 -04:00
|
|
|
})
|
2017-01-15 13:53:11 -05:00
|
|
|
}
|
|
|
|
|
2017-11-21 07:43:29 -05:00
|
|
|
function transactionRetryer <T> (func: (err: any, data: T) => any) {
|
|
|
|
return new Promise<T>((res, rej) => {
|
2018-06-13 08:27:40 -04:00
|
|
|
retry(
|
|
|
|
{
|
|
|
|
times: 5,
|
|
|
|
|
|
|
|
errorFilter: err => {
|
|
|
|
const willRetry = (err.name === 'SequelizeDatabaseError')
|
2021-06-08 11:29:45 -04:00
|
|
|
logger.debug('Maybe retrying the transaction function.', { willRetry, err, tags: [ 'sql', 'retry' ] })
|
2018-06-13 08:27:40 -04:00
|
|
|
return willRetry
|
|
|
|
}
|
|
|
|
},
|
|
|
|
func,
|
|
|
|
(err, data) => err ? rej(err) : res(data)
|
|
|
|
)
|
2017-01-15 13:53:11 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-23 10:38:28 -04:00
|
|
|
function saveInTransactionWithRetries <T extends Pick<Model, 'save' | 'changed'>> (model: T) {
|
2024-08-19 04:13:33 -04:00
|
|
|
const changedKeys = model.changed() || []
|
2024-07-23 10:38:28 -04:00
|
|
|
|
2023-05-19 04:10:41 -04:00
|
|
|
return retryTransactionWrapper(() => {
|
|
|
|
return sequelizeTypescript.transaction(async transaction => {
|
2024-07-23 10:38:28 -04:00
|
|
|
try {
|
|
|
|
await model.save({ transaction })
|
2024-07-24 09:20:03 -04:00
|
|
|
} catch (err) {
|
2024-07-23 10:38:28 -04:00
|
|
|
// Reinit changed keys
|
|
|
|
for (const key of changedKeys) {
|
|
|
|
model.changed(key as keyof Model, true)
|
|
|
|
}
|
2024-07-24 09:20:03 -04:00
|
|
|
|
|
|
|
throw err
|
2024-07-23 10:38:28 -04:00
|
|
|
}
|
2023-05-19 04:10:41 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:29:45 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-02-14 02:59:27 -05:00
|
|
|
function resetSequelizeInstance <T> (instance: Model<T>) {
|
2023-05-11 09:02:53 -04:00
|
|
|
return instance.reload()
|
2018-08-14 09:28:30 -04:00
|
|
|
}
|
|
|
|
|
2021-07-23 05:20:00 -04:00
|
|
|
function filterNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean }> (
|
2019-11-15 09:06:03 -05:00
|
|
|
fromDatabase: T[],
|
2021-07-23 05:20:00 -04:00
|
|
|
newModels: T[]
|
2019-11-15 09:06:03 -05:00
|
|
|
) {
|
|
|
|
return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f)))
|
2021-07-23 05:20:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function deleteAllModels <T extends Pick<Model, 'destroy'>> (models: T[], transaction: Transaction) {
|
|
|
|
return Promise.all(models.map(f => f.destroy({ transaction })))
|
2019-11-15 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
2017-01-15 13:53:11 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-06-08 11:29:45 -04:00
|
|
|
function runInReadCommittedTransaction <T> (fn: (t: Transaction) => Promise<T>) {
|
2021-06-09 07:31:22 -04:00
|
|
|
const options = { isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED }
|
|
|
|
|
|
|
|
return sequelizeTypescript.transaction(options, t => fn(t))
|
2021-06-08 11:29:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function afterCommitIfTransaction (t: Transaction, fn: Function) {
|
|
|
|
if (t) return t.afterCommit(() => fn())
|
|
|
|
|
|
|
|
return fn()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
2018-08-14 09:28:30 -04:00
|
|
|
resetSequelizeInstance,
|
2017-05-15 16:22:03 -04:00
|
|
|
retryTransactionWrapper,
|
2018-01-04 08:04:02 -05:00
|
|
|
transactionRetryer,
|
2023-05-19 04:10:41 -04:00
|
|
|
saveInTransactionWithRetries,
|
2019-11-15 09:06:03 -05:00
|
|
|
afterCommitIfTransaction,
|
2021-07-23 05:20:00 -04:00
|
|
|
filterNonExistingModels,
|
|
|
|
deleteAllModels,
|
2021-07-28 04:32:40 -04:00
|
|
|
runInReadCommittedTransaction
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|