2017-01-15 12:53:11 -06:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const retry = require('async/retry')
|
|
|
|
|
|
|
|
const db = require('../initializers/database')
|
|
|
|
const logger = require('./logger')
|
|
|
|
|
|
|
|
const utils = {
|
2017-01-17 13:38:45 -06:00
|
|
|
commitTransaction,
|
2017-01-15 12:53:11 -06:00
|
|
|
retryTransactionWrapper,
|
2017-01-17 13:38:45 -06:00
|
|
|
rollbackTransaction,
|
|
|
|
startSerializableTransaction,
|
|
|
|
transactionRetryer
|
|
|
|
}
|
|
|
|
|
|
|
|
function commitTransaction (t, callback) {
|
|
|
|
return t.commit().asCallback(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function rollbackTransaction (err, t, callback) {
|
|
|
|
// Try to rollback transaction
|
|
|
|
if (t) {
|
|
|
|
// Do not catch err, report the original one
|
|
|
|
t.rollback().asCallback(function () {
|
|
|
|
return callback(err)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2017-01-15 12:53:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// { arguments, errorMessage }
|
|
|
|
function retryTransactionWrapper (functionToRetry, options, finalCallback) {
|
|
|
|
const args = options.arguments ? options.arguments : []
|
|
|
|
|
|
|
|
utils.transactionRetryer(
|
|
|
|
function (callback) {
|
|
|
|
return functionToRetry.apply(this, args.concat([ callback ]))
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error(options.errorMessage, { error: err })
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not return the error, continue the process
|
|
|
|
return finalCallback(null)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function transactionRetryer (func, callback) {
|
|
|
|
retry({
|
|
|
|
times: 5,
|
|
|
|
|
|
|
|
errorFilter: function (err) {
|
|
|
|
const willRetry = (err.name === 'SequelizeDatabaseError')
|
|
|
|
logger.debug('Maybe retrying the transaction function.', { willRetry })
|
|
|
|
return willRetry
|
|
|
|
}
|
|
|
|
}, func, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function startSerializableTransaction (callback) {
|
|
|
|
db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) {
|
|
|
|
// We force to return only two parameters
|
|
|
|
return callback(err, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = utils
|