const program = require('commander')
-const constants = require('../server/initializers/constants')
const db = require('../server/initializers/database')
program
const videosValidators = middlewares.validators.remote.videos
const signatureValidators = middlewares.validators.remote.signature
const logger = require('../../../helpers/logger')
-const utils = require('../../../helpers/utils')
+const databaseUtils = require('../../../helpers/database-utils')
const router = express.Router()
errorMessage: 'Cannot insert the remote video with many retries.'
}
- utils.retryWrapper(addRemoteVideo, options, finalCallback)
+ databaseUtils.retryTransactionWrapper(addRemoteVideo, options, finalCallback)
}
function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
waterfall([
- function startTransaction (callback) {
- db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) {
- return callback(err, t)
- })
- },
+ databaseUtils.startSerializableTransaction,
function findOrCreateAuthor (t, callback) {
const name = videoToCreateData.author
errorMessage: 'Cannot update the remote video with many retries'
}
- utils.retryWrapper(updateRemoteVideo, options, finalCallback)
+ databaseUtils.retryWrapper(updateRemoteVideo, options, finalCallback)
}
function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) {
waterfall([
- function startTransaction (callback) {
- db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) {
- return callback(err, t)
- })
- },
+ databaseUtils.startSerializableTransaction,
function findVideo (t, callback) {
fetchVideo(fromPod.host, videoAttributesToUpdate.remoteId, function (err, videoInstance) {
const validatorsVideos = validators.videos
const search = middlewares.search
const sort = middlewares.sort
+const databaseUtils = require('../../helpers/database-utils')
const utils = require('../../helpers/utils')
const router = express.Router()
errorMessage: 'Cannot insert the video with many retries.'
}
- utils.retryWrapper(addVideo, options, function (err) {
+ databaseUtils.retryTransactionWrapper(addVideo, options, function (err) {
if (err) return next(err)
// TODO : include Location of the new video -> 201
waterfall([
- function startTransaction (callbackWaterfall) {
- db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) {
- return callbackWaterfall(err, t)
- })
- },
+ databaseUtils.startSerializableTransaction,
function findOrCreateAuthor (t, callbackWaterfall) {
const user = res.locals.oauth.token.User
errorMessage: 'Cannot update the video with many retries.'
}
- utils.retryWrapper(updateVideo, options, function (err) {
+ databaseUtils.retryTransactionWrapper(updateVideo, options, function (err) {
if (err) return next(err)
// TODO : include Location of the new video -> 201
waterfall([
- function startTransaction (callback) {
- db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) {
- return callback(err, t)
- })
- },
+ databaseUtils.startSerializableTransaction,
function findOrCreateTags (t, callback) {
if (videoInfosToUpdate.tags) {
}
function reportVideoAbuseRetryWrapper (req, res, next) {
- utils.transactionRetryer(
- function (callback) {
- return reportVideoAbuse(req, res, callback)
- },
- function (err) {
- if (err) {
- logger.error('Cannot report abuse to the video with many retries.', { error: err })
- return next(err)
- }
+ const options = {
+ arguments: [ req, res ],
+ errorMessage: 'Cannot report abuse to the video with many retries.'
+ }
- return res.type('json').status(204).end()
- }
- )
+ databaseUtils.retryTransactionWrapper(reportVideoAbuse, options, function (err) {
+ if (err) return next(err)
+
+ return res.type('json').status(204).end()
+ })
}
function reportVideoAbuse (req, res, finalCallback) {
--- /dev/null
+'use strict'
+
+const retry = require('async/retry')
+
+const db = require('../initializers/database')
+const logger = require('./logger')
+
+const utils = {
+ retryTransactionWrapper,
+ transactionRetryer,
+ startSerializableTransaction
+}
+
+// { 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) {
+ console.log(db)
+ db.sequelize.transaction({ isolationLevel: 'SERIALIZABLE' }).asCallback(function (err, t) {
+ // We force to return only two parameters
+ return callback(err, t)
+ })
+}
+
+// ---------------------------------------------------------------------------
+
+module.exports = utils
'use strict'
const crypto = require('crypto')
-const retry = require('async/retry')
const logger = require('./logger')
cleanForExit,
generateRandomString,
isTestInstance,
- getFormatedObjects,
- retryWrapper,
- transactionRetryer
+ getFormatedObjects
}
function badRequest (req, res, next) {
}
}
-// { 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