1
0
Fork 0

Server: retryer transaction wrapper refractoring

This commit is contained in:
Chocobozzz 2017-01-15 19:13:16 +01:00
parent a7721e62c0
commit d6a5b018b8
4 changed files with 51 additions and 50 deletions

View File

@ -19,5 +19,6 @@ exclude_paths:
- config/ - config/
- node_modules/ - node_modules/
- client - client
- scripts/
- server/tests/ - server/tests/
- .tmp/ - .tmp/

View File

@ -66,19 +66,12 @@ function remoteVideos (req, res, next) {
// Handle retries on fail // Handle retries on fail
function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) { function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) {
utils.transactionRetryer( const options = {
function (callback) { arguments: [ videoToCreateData, fromPod ],
return addRemoteVideo(videoToCreateData, fromPod, callback) errorMessage: 'Cannot insert the remote video with many retries.'
}, }
function (err) {
if (err) {
logger.error('Cannot insert the remote video with many retries.', { error: err })
}
// Do not return the error, continue the process utils.retryWrapper(addRemoteVideo, options, finalCallback)
return finalCallback(null)
}
)
} }
function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
@ -182,19 +175,12 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
// Handle retries on fail // Handle retries on fail
function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalCallback) { function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalCallback) {
utils.transactionRetryer( const options = {
function (callback) { arguments: [ fromPod, videoAttributesToUpdate ],
return updateRemoteVideo(videoAttributesToUpdate, fromPod, callback) errorMessage: 'Cannot update the remote video with many retries'
}, }
function (err) {
if (err) {
logger.error('Cannot update the remote video with many retries.', { error: err })
}
// Do not return the error, continue the process utils.retryWrapper(updateRemoteVideo, options, finalCallback)
return finalCallback(null)
}
)
} }
function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) {

View File

@ -106,20 +106,17 @@ module.exports = router
// Wrapper to video add that retry the function if there is a database error // 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 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
function addVideoRetryWrapper (req, res, next) { function addVideoRetryWrapper (req, res, next) {
utils.transactionRetryer( const options = {
function (callback) { arguments: [ req, res, req.files.videofile[0] ],
return addVideo(req, res, req.files.videofile[0], callback) errorMessage: 'Cannot insert the video with many retries.'
}, }
function (err) {
if (err) {
logger.error('Cannot insert the video with many retries.', { error: err })
return next(err)
}
// TODO : include Location of the new video -> 201 utils.retryWrapper(addVideo, options, function (err) {
return res.type('json').status(204).end() 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) { function addVideo (req, res, videoFile, callback) {
@ -241,20 +238,17 @@ function addVideo (req, res, videoFile, callback) {
} }
function updateVideoRetryWrapper (req, res, next) { function updateVideoRetryWrapper (req, res, next) {
utils.transactionRetryer( const options = {
function (callback) { arguments: [ req, res ],
return updateVideo(req, res, callback) errorMessage: 'Cannot update the video with many retries.'
}, }
function (err) {
if (err) {
logger.error('Cannot update the video with many retries.', { error: err })
return next(err)
}
// TODO : include Location of the new video -> 201 utils.retryWrapper(updateVideo, options, function (err) {
return res.type('json').status(204).end() 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) { function updateVideo (req, res, finalCallback) {

View File

@ -11,6 +11,7 @@ const utils = {
generateRandomString, generateRandomString,
isTestInstance, isTestInstance,
getFormatedObjects, getFormatedObjects,
retryWrapper,
transactionRetryer 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) { function transactionRetryer (func, callback) {
retry({ retry({
times: 5, times: 5,