diff options
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/database-utils.ts | 49 |
1 files changed, 34 insertions, 15 deletions
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index b3ff42a37..9b861a88c 100644 --- a/server/helpers/database-utils.ts +++ b/server/helpers/database-utils.ts | |||
@@ -3,35 +3,54 @@ import * as Bluebird from 'bluebird' | |||
3 | import { Model } from 'sequelize-typescript' | 3 | import { Model } from 'sequelize-typescript' |
4 | import { logger } from './logger' | 4 | import { logger } from './logger' |
5 | 5 | ||
6 | type RetryTransactionWrapperOptions = { errorMessage: string, arguments?: any[] } | 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 | |||
7 | function retryTransactionWrapper <T> ( | 24 | function retryTransactionWrapper <T> ( |
8 | functionToRetry: (...args) => Promise<T> | Bluebird<T>, | 25 | functionToRetry: (...args: any[]) => Promise<T> | Bluebird<T>, |
9 | options: RetryTransactionWrapperOptions | 26 | ...args: any[] |
10 | ): Promise<T> { | 27 | ): Promise<T> { |
11 | const args = options.arguments ? options.arguments : [] | ||
12 | |||
13 | return transactionRetryer<T>(callback => { | 28 | return transactionRetryer<T>(callback => { |
14 | functionToRetry.apply(this, args) | 29 | functionToRetry.apply(this, args) |
15 | .then((result: T) => callback(null, result)) | 30 | .then((result: T) => callback(null, result)) |
16 | .catch(err => callback(err)) | 31 | .catch(err => callback(err)) |
17 | }) | 32 | }) |
18 | .catch(err => { | 33 | .catch(err => { |
19 | logger.error(options.errorMessage, { err }) | 34 | logger.error('Cannot execute %s with many retries.', functionToRetry.toString(), { err }) |
20 | throw err | 35 | throw err |
21 | }) | 36 | }) |
22 | } | 37 | } |
23 | 38 | ||
24 | function transactionRetryer <T> (func: (err: any, data: T) => any) { | 39 | function transactionRetryer <T> (func: (err: any, data: T) => any) { |
25 | return new Promise<T>((res, rej) => { | 40 | return new Promise<T>((res, rej) => { |
26 | retry({ | 41 | retry( |
27 | times: 5, | 42 | { |
28 | 43 | times: 5, | |
29 | errorFilter: err => { | 44 | |
30 | const willRetry = (err.name === 'SequelizeDatabaseError') | 45 | errorFilter: err => { |
31 | logger.debug('Maybe retrying the transaction function.', { willRetry, err }) | 46 | const willRetry = (err.name === 'SequelizeDatabaseError') |
32 | return willRetry | 47 | logger.debug('Maybe retrying the transaction function.', { willRetry, err }) |
33 | } | 48 | return willRetry |
34 | }, func, (err, data) => err ? rej(err) : res(data)) | 49 | } |
50 | }, | ||
51 | func, | ||
52 | (err, data) => err ? rej(err) : res(data) | ||
53 | ) | ||
35 | }) | 54 | }) |
36 | } | 55 | } |
37 | 56 | ||