mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 02:39:33 +02:00
Server: create transaction refractoring
This commit is contained in:
parent
9bce75925e
commit
4df023f2d4
5 changed files with 75 additions and 73 deletions
|
@ -1,7 +1,6 @@
|
|||
'use strict'
|
||||
|
||||
const crypto = require('crypto')
|
||||
const retry = require('async/retry')
|
||||
|
||||
const logger = require('./logger')
|
||||
|
||||
|
@ -10,9 +9,7 @@ const utils = {
|
|||
cleanForExit,
|
||||
generateRandomString,
|
||||
isTestInstance,
|
||||
getFormatedObjects,
|
||||
retryWrapper,
|
||||
transactionRetryer
|
||||
getFormatedObjects
|
||||
}
|
||||
|
||||
function badRequest (req, res, next) {
|
||||
|
@ -49,37 +46,6 @@ function getFormatedObjects (objects, objectsTotal) {
|
|||
}
|
||||
}
|
||||
|
||||
// { arguments, errorMessage }
|
||||
function retryWrapper (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)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
module.exports = utils
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue