2017-06-05 15:53:49 -04:00
|
|
|
import * as retry from 'async/retry'
|
2017-11-16 09:22:39 -05:00
|
|
|
import * as Bluebird from 'bluebird'
|
2018-01-04 08:04:02 -05:00
|
|
|
import { Model } from 'sequelize-typescript'
|
2017-05-15 16:22:03 -04:00
|
|
|
import { logger } from './logger'
|
2017-01-17 14:38:45 -05:00
|
|
|
|
2018-06-13 08:27:40 -04: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 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> (
|
2018-06-13 08:27:40 -04:00
|
|
|
functionToRetry: (...args: any[]) => Promise<T> | Bluebird<T>,
|
|
|
|
...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 => {
|
2018-06-13 08:27:40 -04:00
|
|
|
logger.error('Cannot execute %s with many retries.', functionToRetry.toString(), { 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')
|
|
|
|
logger.debug('Maybe retrying the transaction function.', { willRetry, err })
|
|
|
|
return willRetry
|
|
|
|
}
|
|
|
|
},
|
|
|
|
func,
|
|
|
|
(err, data) => err ? rej(err) : res(data)
|
|
|
|
)
|
2017-01-15 13:53:11 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-13 11:00:39 -04:00
|
|
|
function updateInstanceWithAnother <T extends Model<T>> (instanceToUpdate: Model<T>, baseInstance: Model<T>) {
|
2018-01-04 08:04:02 -05:00
|
|
|
const obj = baseInstance.toJSON()
|
|
|
|
|
|
|
|
for (const key of Object.keys(obj)) {
|
|
|
|
instanceToUpdate.set(key, obj[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 13:53:11 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
|
|
|
retryTransactionWrapper,
|
2018-01-04 08:04:02 -05:00
|
|
|
transactionRetryer,
|
|
|
|
updateInstanceWithAnother
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|