]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos.js
Server: retryer transaction wrapper refractoring
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos.js
index 55d671f5b1a16fa932faf682fa0a5410ee368918..9a50a29bef5065e04fc39b12cc7aa20ca48b04c7 100644 (file)
@@ -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,24 +238,22 @@ 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) {
   const videoInstance = res.locals.video
+  const videoFieldsSave = videoInstance.toJSON()
   const videoInfosToUpdate = req.body
 
   waterfall([
@@ -280,12 +275,13 @@ function updateVideo (req, res, finalCallback) {
     },
 
     function updateVideoIntoDB (t, tagInstances, callback) {
-      const options = { transaction: t }
+      const options = {
+        transaction: t
+      }
 
       if (videoInfosToUpdate.name) videoInstance.set('name', videoInfosToUpdate.name)
       if (videoInfosToUpdate.description) videoInstance.set('description', videoInfosToUpdate.description)
 
-      // Add tags association
       videoInstance.save(options).asCallback(function (err) {
         return callback(err, t, tagInstances)
       })
@@ -321,6 +317,14 @@ function updateVideo (req, res, finalCallback) {
       // Abort transaction?
       if (t) t.rollback()
 
+      // Force fields we want to update
+      // If the transaction is retried, sequelize will think the object has not changed
+      // So it will skip the SQL request, even if the last one was ROLLBACKed!
+      Object.keys(videoFieldsSave).forEach(function (key) {
+        const value = videoFieldsSave[key]
+        videoInstance.set(key, value)
+      })
+
       return finalCallback(err)
     }