From 4df023f2d4e4ea93d3fbc6010f0460511ba45140 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 15 Jan 2017 19:53:11 +0100 Subject: Server: create transaction refractoring --- server/controllers/api/remote/videos.js | 18 +++++----------- server/controllers/api/videos.js | 38 ++++++++++++--------------------- 2 files changed, 19 insertions(+), 37 deletions(-) (limited to 'server/controllers/api') diff --git a/server/controllers/api/remote/videos.js b/server/controllers/api/remote/videos.js index 9d007246f..33b521c5f 100644 --- a/server/controllers/api/remote/videos.js +++ b/server/controllers/api/remote/videos.js @@ -10,7 +10,7 @@ const secureMiddleware = middlewares.secure 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() @@ -71,7 +71,7 @@ function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) errorMessage: 'Cannot insert the remote video with many retries.' } - utils.retryWrapper(addRemoteVideo, options, finalCallback) + databaseUtils.retryTransactionWrapper(addRemoteVideo, options, finalCallback) } function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { @@ -79,11 +79,7 @@ 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 @@ -180,7 +176,7 @@ function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalC errorMessage: 'Cannot update the remote video with many retries' } - utils.retryWrapper(updateRemoteVideo, options, finalCallback) + databaseUtils.retryWrapper(updateRemoteVideo, options, finalCallback) } function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { @@ -188,11 +184,7 @@ 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) { diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js index 9a50a29be..ebfdb32f9 100644 --- a/server/controllers/api/videos.js +++ b/server/controllers/api/videos.js @@ -20,6 +20,7 @@ const validatorsSort = validators.sort 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() @@ -111,7 +112,7 @@ function addVideoRetryWrapper (req, res, next) { 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 @@ -124,11 +125,7 @@ function addVideo (req, res, videoFile, callback) { 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 @@ -243,7 +240,7 @@ function updateVideoRetryWrapper (req, res, next) { 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 @@ -258,11 +255,7 @@ function updateVideo (req, res, finalCallback) { 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) { @@ -384,19 +377,16 @@ function listVideoAbuses (req, res, next) { } 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) { -- cgit v1.2.3