2017-06-05 21:53:49 +02:00
|
|
|
import * as retry from 'async/retry'
|
2017-11-16 15:22:39 +01:00
|
|
|
import * as Bluebird from 'bluebird'
|
2021-05-07 17:14:39 +02:00
|
|
|
import { QueryTypes, Transaction } from 'sequelize'
|
2018-08-14 15:28:30 +02:00
|
|
|
import { Model } from 'sequelize-typescript'
|
2021-05-07 17:14:39 +02:00
|
|
|
import { sequelizeTypescript } from '@server/initializers/database'
|
2017-05-15 22:22:03 +02:00
|
|
|
import { logger } from './logger'
|
2017-01-17 20:38:45 +01:00
|
|
|
|
2021-01-21 16:57:21 +01:00
|
|
|
function retryTransactionWrapper <T, A, B, C, D> (
|
|
|
|
functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise<T> | Bluebird<T>,
|
|
|
|
arg1: A,
|
|
|
|
arg2: B,
|
|
|
|
arg3: C,
|
|
|
|
arg4: D,
|
|
|
|
): Promise<T>
|
|
|
|
|
2018-06-13 14:27:40 +02:00
|
|
|
function retryTransactionWrapper <T, A, B, C> (
|
|
|
|
functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise<T> | Bluebird<T>,
|
|
|
|
arg1: A,
|
|
|
|
arg2: B,
|
|
|
|
arg3: C
|
|
|
|
): Promise<T>
|
|
|
|
|
|
|
|
function retryTransactionWrapper <T, A, B> (
|
|
|
|
functionToRetry: (arg1: A, arg2: B) => Promise<T> | Bluebird<T>,
|
|
|
|
arg1: A,
|
|
|
|
arg2: B
|
|
|
|
): Promise<T>
|
|
|
|
|
|
|
|
function retryTransactionWrapper <T, A> (
|
|
|
|
functionToRetry: (arg1: A) => Promise<T> | Bluebird<T>,
|
|
|
|
arg1: A
|
|
|
|
): Promise<T>
|
|
|
|
|
2018-06-14 18:06:56 +02:00
|
|
|
function retryTransactionWrapper <T> (
|
|
|
|
functionToRetry: () => Promise<T> | Bluebird<T>
|
|
|
|
): Promise<T>
|
|
|
|
|
2017-11-21 13:43:29 +01:00
|
|
|
function retryTransactionWrapper <T> (
|
2018-06-13 14:27:40 +02:00
|
|
|
functionToRetry: (...args: any[]) => Promise<T> | Bluebird<T>,
|
|
|
|
...args: any[]
|
2017-11-21 13:43:29 +01:00
|
|
|
): Promise<T> {
|
|
|
|
return transactionRetryer<T>(callback => {
|
2018-06-14 18:06:56 +02:00
|
|
|
functionToRetry.apply(null, args)
|
2017-11-21 13:43:29 +01:00
|
|
|
.then((result: T) => callback(null, result))
|
2017-07-05 13:26:25 +02:00
|
|
|
.catch(err => callback(err))
|
2017-07-11 17:04:57 +02:00
|
|
|
})
|
2017-07-05 13:26:25 +02:00
|
|
|
.catch(err => {
|
2018-06-27 09:45:06 +02:00
|
|
|
logger.error(`Cannot execute ${functionToRetry.name} with many retries.`, { err })
|
2017-11-16 15:22:39 +01:00
|
|
|
throw err
|
2017-07-05 13:26:25 +02:00
|
|
|
})
|
2017-01-15 19:53:11 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:43:29 +01:00
|
|
|
function transactionRetryer <T> (func: (err: any, data: T) => any) {
|
|
|
|
return new Promise<T>((res, rej) => {
|
2018-06-13 14:27:40 +02:00
|
|
|
retry(
|
|
|
|
{
|
|
|
|
times: 5,
|
|
|
|
|
|
|
|
errorFilter: err => {
|
|
|
|
const willRetry = (err.name === 'SequelizeDatabaseError')
|
|
|
|
logger.debug('Maybe retrying the transaction function.', { willRetry, err })
|
|
|
|
return willRetry
|
|
|
|
}
|
|
|
|
},
|
|
|
|
func,
|
|
|
|
(err, data) => err ? rej(err) : res(data)
|
|
|
|
)
|
2017-01-15 19:53:11 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:09:04 +02:00
|
|
|
function updateInstanceWithAnother <M, T extends U, U extends Model<M>> (instanceToUpdate: T, baseInstance: U) {
|
2018-01-04 14:04:02 +01:00
|
|
|
const obj = baseInstance.toJSON()
|
|
|
|
|
|
|
|
for (const key of Object.keys(obj)) {
|
2019-04-18 11:28:17 +02:00
|
|
|
instanceToUpdate[key] = obj[key]
|
2018-01-04 14:04:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 15:28:30 +02:00
|
|
|
function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
|
|
|
|
Object.keys(savedFields).forEach(key => {
|
2019-04-18 11:28:17 +02:00
|
|
|
instance[key] = savedFields[key]
|
2018-08-14 15:28:30 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-29 11:59:29 +02:00
|
|
|
function afterCommitIfTransaction (t: Transaction, fn: Function) {
|
|
|
|
if (t) return t.afterCommit(() => fn())
|
|
|
|
|
|
|
|
return fn()
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:09:04 +02:00
|
|
|
function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Pick<Model, 'destroy'>> (
|
2019-11-15 15:06:03 +01:00
|
|
|
fromDatabase: T[],
|
|
|
|
newModels: T[],
|
|
|
|
t: Transaction
|
|
|
|
) {
|
|
|
|
return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f)))
|
|
|
|
.map(f => f.destroy({ transaction: t }))
|
|
|
|
}
|
|
|
|
|
2021-05-07 17:14:39 +02:00
|
|
|
// Sequelize always skip the update if we only update updatedAt field
|
|
|
|
function setAsUpdated (table: string, id: number, transaction?: Transaction) {
|
|
|
|
return sequelizeTypescript.query(
|
|
|
|
`UPDATE "${table}" SET "updatedAt" = :updatedAt WHERE id = :id`,
|
|
|
|
{
|
|
|
|
replacements: { table, id, updatedAt: new Date() },
|
|
|
|
type: QueryTypes.UPDATE,
|
|
|
|
transaction
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-01-15 19:53:11 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
export {
|
2018-08-14 15:28:30 +02:00
|
|
|
resetSequelizeInstance,
|
2017-05-15 22:22:03 +02:00
|
|
|
retryTransactionWrapper,
|
2018-01-04 14:04:02 +01:00
|
|
|
transactionRetryer,
|
2019-07-29 11:59:29 +02:00
|
|
|
updateInstanceWithAnother,
|
2019-11-15 15:06:03 +01:00
|
|
|
afterCommitIfTransaction,
|
2021-05-07 17:14:39 +02:00
|
|
|
deleteNonExistingModels,
|
|
|
|
setAsUpdated
|
2017-05-15 22:22:03 +02:00
|
|
|
}
|