diff options
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/database-utils.js | 55 | ||||
-rw-r--r-- | server/helpers/utils.js | 36 |
2 files changed, 56 insertions, 35 deletions
diff --git a/server/helpers/database-utils.js b/server/helpers/database-utils.js new file mode 100644 index 000000000..046717517 --- /dev/null +++ b/server/helpers/database-utils.js | |||
@@ -0,0 +1,55 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const retry = require('async/retry') | ||
4 | |||
5 | const db = require('../initializers/database') | ||
6 | const logger = require('./logger') | ||
7 | |||
8 | const utils = { | ||
9 | retryTransactionWrapper, | ||
10 | transactionRetryer, | ||
11 | startSerializableTransaction | ||
12 | } | ||
13 | |||
14 | // { arguments, errorMessage } | ||
15 | function retryTransactionWrapper (functionToRetry, options, finalCallback) { | ||
16 | const args = options.arguments ? options.arguments : [] | ||
17 | |||
18 | utils.transactionRetryer( | ||
19 | function (callback) { | ||
20 | return functionToRetry.apply(this, args.concat([ callback ])) | ||
21 | }, | ||
22 | function (err) { | ||
23 | if (err) { | ||
24 | logger.error(options.errorMessage, { error: err }) | ||
25 | } | ||
26 | |||
27 | // Do not return the error, continue the process | ||
28 | return finalCallback(null) | ||
29 | } | ||
30 | ) | ||
31 | } | ||
32 | |||
33 | function transactionRetryer (func, callback) { | ||
34 | retry({ | ||
35 | times: 5, | ||
36 | |||
37 | errorFilter: function (err) { | ||
38 | const willRetry = (err.name === 'SequelizeDatabaseError') | ||
39 | logger.debug('Maybe retrying the transaction function.', { willRetry }) | ||
40 | return willRetry | ||
41 | } | ||
42 | }, func, callback) | ||
43 | } | ||
44 | |||
45 | function startSerializableTransaction (callback) { | ||
46 | console.log(db) | ||
47 | db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) { | ||
48 | // We force to return only two parameters | ||
49 | return callback(err, t) | ||
50 | }) | ||
51 | } | ||
52 | |||
53 | // --------------------------------------------------------------------------- | ||
54 | |||
55 | module.exports = utils | ||
diff --git a/server/helpers/utils.js b/server/helpers/utils.js index fb4dd08cc..9f4b14582 100644 --- a/server/helpers/utils.js +++ b/server/helpers/utils.js | |||
@@ -1,7 +1,6 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const crypto = require('crypto') | 3 | const crypto = require('crypto') |
4 | const retry = require('async/retry') | ||
5 | 4 | ||
6 | const logger = require('./logger') | 5 | const logger = require('./logger') |
7 | 6 | ||
@@ -10,9 +9,7 @@ const utils = { | |||
10 | cleanForExit, | 9 | cleanForExit, |
11 | generateRandomString, | 10 | generateRandomString, |
12 | isTestInstance, | 11 | isTestInstance, |
13 | getFormatedObjects, | 12 | getFormatedObjects |
14 | retryWrapper, | ||
15 | transactionRetryer | ||
16 | } | 13 | } |
17 | 14 | ||
18 | function badRequest (req, res, next) { | 15 | function badRequest (req, res, next) { |
@@ -49,37 +46,6 @@ function getFormatedObjects (objects, objectsTotal) { | |||
49 | } | 46 | } |
50 | } | 47 | } |
51 | 48 | ||
52 | // { arguments, errorMessage } | ||
53 | function retryWrapper (functionToRetry, options, finalCallback) { | ||
54 | const args = options.arguments ? options.arguments : [] | ||
55 | |||
56 | utils.transactionRetryer( | ||
57 | function (callback) { | ||
58 | return functionToRetry.apply(this, args.concat([ callback ])) | ||
59 | }, | ||
60 | function (err) { | ||
61 | if (err) { | ||
62 | logger.error(options.errorMessage, { error: err }) | ||
63 | } | ||
64 | |||
65 | // Do not return the error, continue the process | ||
66 | return finalCallback(null) | ||
67 | } | ||
68 | ) | ||
69 | } | ||
70 | |||
71 | function transactionRetryer (func, callback) { | ||
72 | retry({ | ||
73 | times: 5, | ||
74 | |||
75 | errorFilter: function (err) { | ||
76 | const willRetry = (err.name === 'SequelizeDatabaseError') | ||
77 | logger.debug('Maybe retrying the transaction function.', { willRetry }) | ||
78 | return willRetry | ||
79 | } | ||
80 | }, func, callback) | ||
81 | } | ||
82 | |||
83 | // --------------------------------------------------------------------------- | 49 | // --------------------------------------------------------------------------- |
84 | 50 | ||
85 | module.exports = utils | 51 | module.exports = utils |