]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Use async waterfall in request scheduler for better readability
authorChocobozzz <florian.bigard@gmail.com>
Sun, 15 May 2016 16:03:43 +0000 (18:03 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Sun, 15 May 2016 16:03:43 +0000 (18:03 +0200)
server/lib/requestsScheduler.js

index f10de62769c5a56ae86d2697b6fd34ceebf99f89..3d04b8cc839b847414dce3c2f9a9cd8c484a267a 100644 (file)
@@ -177,35 +177,61 @@ function makeRequests () {
 }
 
 function removeBadPods () {
-  Pods.findBadPods(function (err, pods) {
-    if (err) {
-      logger.error('Cannot find bad pods.', { error: err })
-      return // abort
-    }
+  async.waterfall([
+    function findBadPods (callback) {
+      Pods.findBadPods(function (err, pods) {
+        if (err) {
+          logger.error('Cannot find bad pods.', { error: err })
+          return callback(err)
+        }
 
-    if (pods.length === 0) return
+        return callback(null, pods)
+      })
+    },
 
-    const urls = map(pods, 'url')
-    const ids = map(pods, '_id')
+    function listVideosOfTheseBadPods (pods, callback) {
+      if (pods.length === 0) return callback(null)
 
-    Videos.listFromUrls(urls, function (err, videosList) {
-      if (err) {
-        logger.error('Cannot list videos urls.', { error: err, urls: urls })
-      } else {
-        videos.removeRemoteVideos(videosList, function (err) {
-          if (err) logger.error('Cannot remove remote videos.', { error: err })
-        })
-      }
+      const urls = map(pods, 'url')
+      const ids = map(pods, '_id')
 
-      Pods.removeAllByIds(ids, function (err, r) {
+      Videos.listFromUrls(urls, function (err, videosList) {
         if (err) {
-          logger.error('Cannot remove bad pods.', { error: err })
-        } else {
-          const podsRemoved = r.result.n
-          logger.info('Removed %d pods.', podsRemoved)
+          logger.error('Cannot list videos urls.', { error: err, urls: urls })
+          return callback(null, ids, [])
         }
+
+        return callback(null, ids, videosList)
       })
-    })
+    },
+
+    function removeVideosOfTheseBadPods (podIds, videosList, callback) {
+      // We don't have to remove pods, skip
+      if (typeof podIds === 'function') return podIds(null)
+
+      // Remove the remote videos
+      videos.removeRemoteVideos(videosList, function (err) {
+        if (err) logger.error('Cannot remove remote videos.', { error: err })
+
+        return callback(null, podIds)
+      })
+    },
+
+    function removeBadPodsFromDB (podIds, callback) {
+      // We don't have to remove pods, skip
+      if (typeof podIds === 'function') return podIds(null)
+
+      Pods.removeAllByIds(podIds, callback)
+    }
+  ], function (err, removeResult) {
+    if (err) {
+      logger.error('Cannot remove bad pods.', { error: err })
+    } else if (removeResult) {
+      const podsRemoved = removeResult.result.n
+      logger.info('Removed %d pods.', podsRemoved)
+    } else {
+      logger.info('No need to remove bad pods.')
+    }
   })
 }