1 import * as retry from 'async/retry'
2 import * as Bluebird from 'bluebird'
3 import { logger } from './logger'
5 type RetryTransactionWrapperOptions = { errorMessage: string, arguments?: any[] }
6 function retryTransactionWrapper <T> (
7 functionToRetry: (...args) => Promise<T> | Bluebird<T>,
8 options: RetryTransactionWrapperOptions
10 const args = options.arguments ? options.arguments : []
12 return transactionRetryer<T>(callback => {
13 functionToRetry.apply(this, args)
14 .then((result: T) => callback(null, result))
15 .catch(err => callback(err))
18 logger.error(options.errorMessage, err)
23 function transactionRetryer <T> (func: (err: any, data: T) => any) {
24 return new Promise<T>((res, rej) => {
29 const willRetry = (err.name === 'SequelizeDatabaseError')
30 logger.debug('Maybe retrying the transaction function.', { willRetry })
33 }, func, (err, data) => err ? rej(err) : res(data))
37 // ---------------------------------------------------------------------------
40 retryTransactionWrapper,