]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as retry from 'async/retry' |
20494f12 | 2 | import * as Bluebird from 'bluebird' |
06215f15 | 3 | import { Model } from 'sequelize-typescript' |
65fcc311 | 4 | import { logger } from './logger' |
4145c1c6 | 5 | |
90d4bb81 C |
6 | function retryTransactionWrapper <T, A, B, C> ( |
7 | functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise<T> | Bluebird<T>, | |
8 | arg1: A, | |
9 | arg2: B, | |
10 | arg3: C | |
11 | ): Promise<T> | |
12 | ||
13 | function retryTransactionWrapper <T, A, B> ( | |
14 | functionToRetry: (arg1: A, arg2: B) => Promise<T> | Bluebird<T>, | |
15 | arg1: A, | |
16 | arg2: B | |
17 | ): Promise<T> | |
18 | ||
19 | function retryTransactionWrapper <T, A> ( | |
20 | functionToRetry: (arg1: A) => Promise<T> | Bluebird<T>, | |
21 | arg1: A | |
22 | ): Promise<T> | |
23 | ||
2baea0c7 C |
24 | function retryTransactionWrapper <T> ( |
25 | functionToRetry: () => Promise<T> | Bluebird<T> | |
26 | ): Promise<T> | |
27 | ||
0f91ae62 | 28 | function retryTransactionWrapper <T> ( |
90d4bb81 C |
29 | functionToRetry: (...args: any[]) => Promise<T> | Bluebird<T>, |
30 | ...args: any[] | |
0f91ae62 | 31 | ): Promise<T> { |
0f91ae62 | 32 | return transactionRetryer<T>(callback => { |
2baea0c7 | 33 | functionToRetry.apply(null, args) |
0f91ae62 | 34 | .then((result: T) => callback(null, result)) |
6fcd19ba | 35 | .catch(err => callback(err)) |
075f16ca | 36 | }) |
6fcd19ba | 37 | .catch(err => { |
1e11f67b | 38 | logger.error(`Cannot execute ${functionToRetry.name} with many retries.`, { err }) |
20494f12 | 39 | throw err |
6fcd19ba | 40 | }) |
4df023f2 C |
41 | } |
42 | ||
0f91ae62 C |
43 | function transactionRetryer <T> (func: (err: any, data: T) => any) { |
44 | return new Promise<T>((res, rej) => { | |
90d4bb81 C |
45 | retry( |
46 | { | |
47 | times: 5, | |
48 | ||
49 | errorFilter: err => { | |
50 | const willRetry = (err.name === 'SequelizeDatabaseError') | |
51 | logger.debug('Maybe retrying the transaction function.', { willRetry, err }) | |
52 | return willRetry | |
53 | } | |
54 | }, | |
55 | func, | |
56 | (err, data) => err ? rej(err) : res(data) | |
57 | ) | |
4df023f2 C |
58 | }) |
59 | } | |
60 | ||
c39ea24b | 61 | function updateInstanceWithAnother <T extends Model<T>> (instanceToUpdate: Model<T>, baseInstance: Model<T>) { |
a5625b41 C |
62 | const obj = baseInstance.toJSON() |
63 | ||
64 | for (const key of Object.keys(obj)) { | |
65 | instanceToUpdate.set(key, obj[key]) | |
66 | } | |
67 | } | |
68 | ||
06215f15 C |
69 | function resetSequelizeInstance (instance: Model<any>, savedFields: object) { |
70 | Object.keys(savedFields).forEach(key => { | |
71 | const value = savedFields[key] | |
72 | instance.set(key, value) | |
73 | }) | |
74 | } | |
75 | ||
4df023f2 C |
76 | // --------------------------------------------------------------------------- |
77 | ||
65fcc311 | 78 | export { |
06215f15 | 79 | resetSequelizeInstance, |
65fcc311 | 80 | retryTransactionWrapper, |
a5625b41 C |
81 | transactionRetryer, |
82 | updateInstanceWithAnother | |
65fcc311 | 83 | } |