aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/database-utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/database-utils.js')
-rw-r--r--server/helpers/database-utils.js55
1 files changed, 55 insertions, 0 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
3const retry = require('async/retry')
4
5const db = require('../initializers/database')
6const logger = require('./logger')
7
8const utils = {
9 retryTransactionWrapper,
10 transactionRetryer,
11 startSerializableTransaction
12}
13
14// { arguments, errorMessage }
15function 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
33function 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
45function 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
55module.exports = utils