Use async waterfall in request scheduler for better readability

pull/10/head
Chocobozzz 2016-05-15 18:03:43 +02:00
parent e7ea2817c0
commit e856e334a1
1 changed files with 52 additions and 26 deletions

View File

@ -177,13 +177,20 @@ function makeRequests () {
} }
function removeBadPods () { function removeBadPods () {
async.waterfall([
function findBadPods (callback) {
Pods.findBadPods(function (err, pods) { Pods.findBadPods(function (err, pods) {
if (err) { if (err) {
logger.error('Cannot find bad pods.', { error: err }) logger.error('Cannot find bad pods.', { error: err })
return // abort return callback(err)
} }
if (pods.length === 0) return return callback(null, pods)
})
},
function listVideosOfTheseBadPods (pods, callback) {
if (pods.length === 0) return callback(null)
const urls = map(pods, 'url') const urls = map(pods, 'url')
const ids = map(pods, '_id') const ids = map(pods, '_id')
@ -191,22 +198,41 @@ function removeBadPods () {
Videos.listFromUrls(urls, function (err, videosList) { Videos.listFromUrls(urls, function (err, videosList) {
if (err) { if (err) {
logger.error('Cannot list videos urls.', { error: err, urls: urls }) logger.error('Cannot list videos urls.', { error: err, urls: urls })
} else { return callback(null, ids, [])
videos.removeRemoteVideos(videosList, function (err) {
if (err) logger.error('Cannot remove remote videos.', { error: err })
})
} }
Pods.removeAllByIds(ids, function (err, r) { 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) { if (err) {
logger.error('Cannot remove bad pods.', { error: err }) logger.error('Cannot remove bad pods.', { error: err })
} else { } else if (removeResult) {
const podsRemoved = r.result.n const podsRemoved = removeResult.result.n
logger.info('Removed %d pods.', podsRemoved) logger.info('Removed %d pods.', podsRemoved)
} else {
logger.info('No need to remove bad pods.')
} }
}) })
})
})
} }
function updatePodsScore (goodPods, badPods) { function updatePodsScore (goodPods, badPods) {