Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Server: retryer transaction wrapper refractoring

Chocobozzzd6a5b018b89f
diff --git a/.codeclimate.yml b/.codeclimate.yml
index ddff519bf3..2318cfa4b5 100644
--- a/.codeclimate.yml
+++ b/.codeclimate.yml
@@ -19,5 +19,6 @@ exclude_paths:
- config/
- node_modules/
- client
+- scripts/
- server/tests/
- .tmp/
diff --git a/server/controllers/api/remote/videos.js b/server/controllers/api/remote/videos.js
index c45a86dbbf..9d007246fd 100644
--- a/server/controllers/api/remote/videos.js
+++ b/server/controllers/api/remote/videos.js
@@ -66,19 +66,12 @@ function remoteVideos (req, res, next) {
// Handle retries on fail
function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) {
- utils.transactionRetryer(
- function (callback) {
- return addRemoteVideo(videoToCreateData, fromPod, callback)
- },
- function (err) {
- if (err) {
- logger.error('Cannot insert the remote video with many retries.', { error: err })
- }
+ const options = {
+ arguments: [ videoToCreateData, fromPod ],
+ errorMessage: 'Cannot insert the remote video with many retries.'
+ }
- // Do not return the error, continue the process
- return finalCallback(null)
- }
- )
+ utils.retryWrapper(addRemoteVideo, options, finalCallback)
}
function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
@@ -182,19 +175,12 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
// Handle retries on fail
function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalCallback) {
- utils.transactionRetryer(
- function (callback) {
- return updateRemoteVideo(videoAttributesToUpdate, fromPod, callback)
- },
- function (err) {
- if (err) {
- logger.error('Cannot update the remote video with many retries.', { error: err })
- }
+ const options = {
+ arguments: [ fromPod, videoAttributesToUpdate ],
+ errorMessage: 'Cannot update the remote video with many retries'
+ }
- // Do not return the error, continue the process
- return finalCallback(null)
- }
- )
+ utils.retryWrapper(updateRemoteVideo, options, finalCallback)
}
function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) {
diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js
index 2c4af520e6..9a50a29bef 100644
--- a/server/controllers/api/videos.js
+++ b/server/controllers/api/videos.js
@@ -106,20 +106,17 @@ module.exports = router
// Wrapper to video add that retry the function if there is a database error
// We need this because we run the transaction in SERIALIZABLE isolation that can fail
function addVideoRetryWrapper (req, res, next) {
- utils.transactionRetryer(
- function (callback) {
- return addVideo(req, res, req.files.videofile[0], callback)
- },
- function (err) {
- if (err) {
- logger.error('Cannot insert the video with many retries.', { error: err })
- return next(err)
- }
+ const options = {
+ arguments: [ req, res, req.files.videofile[0] ],
+ errorMessage: 'Cannot insert the video with many retries.'
+ }
- // TODO : include Location of the new video -> 201
- return res.type('json').status(204).end()
- }
- )
+ utils.retryWrapper(addVideo, options, function (err) {
+ if (err) return next(err)
+
+ // TODO : include Location of the new video -> 201
+ return res.type('json').status(204).end()
+ })
}
function addVideo (req, res, videoFile, callback) {
@@ -241,20 +238,17 @@ function addVideo (req, res, videoFile, callback) {
}
function updateVideoRetryWrapper (req, res, next) {
- utils.transactionRetryer(
- function (callback) {
- return updateVideo(req, res, callback)
- },
- function (err) {
- if (err) {
- logger.error('Cannot update the video with many retries.', { error: err })
- return next(err)
- }
+ const options = {
+ arguments: [ req, res ],
+ errorMessage: 'Cannot update the video with many retries.'
+ }
- // TODO : include Location of the new video -> 201
- return res.type('json').status(204).end()
- }
- )
+ utils.retryWrapper(updateVideo, options, function (err) {
+ if (err) return next(err)
+
+ // TODO : include Location of the new video -> 201
+ return res.type('json').status(204).end()
+ })
}
function updateVideo (req, res, finalCallback) {
diff --git a/server/helpers/utils.js b/server/helpers/utils.js
index a902850cdd..fb4dd08cc6 100644
--- a/server/helpers/utils.js
+++ b/server/helpers/utils.js
@@ -11,6 +11,7 @@ const utils = {
generateRandomString,
isTestInstance,
getFormatedObjects,
+ retryWrapper,
transactionRetryer
}
@@ -48,6 +49,25 @@ 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,