From e7ea2817c0830e88090424ca3ed0038f257f0d34 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 15 May 2016 10:42:17 +0200 Subject: Use async waterfall for better readability in friends lib --- server/lib/friends.js | 79 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 32 deletions(-) (limited to 'server/lib') diff --git a/server/lib/friends.js b/server/lib/friends.js index e052574a5..e986fa006 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js @@ -80,43 +80,58 @@ function quitFriends (callback) { // Flush pool requests requestsScheduler.forceSend() - Pods.list(function (err, pods) { - if (err) return callback(err) - - const request = { - method: 'POST', - path: '/api/' + constants.API_VERSION + '/pods/remove', - sign: true, - encrypt: true, - data: { - url: 'me' // Fake data + async.waterfall([ + function getPodsList (callbackAsync) { + return Pods.list(callbackAsync) + }, + + function announceIQuitMyFriends (pods, callbackAsync) { + const request = { + method: 'POST', + path: '/api/' + constants.API_VERSION + '/pods/remove', + sign: true, + encrypt: true, + data: { + url: 'me' // Fake data + } } - } - // Announce we quit them - requests.makeMultipleRetryRequest(request, pods, function () { - Pods.removeAll(function (err) { - requestsScheduler.activate() + // Announce we quit them + requests.makeMultipleRetryRequest(request, pods, function (err) { + return callbackAsync(err) + }) + }, - if (err) return callback(err) + function removePodsFromDB (callbackAsync) { + Pods.removeAll(function (err) { + return callbackAsync(err) + }) + }, - logger.info('Broke friends, so sad :(') + function listRemoteVideos (callbackAsync) { + logger.info('Broke friends, so sad :(') - Videos.listFromRemotes(function (err, videosList) { - if (err) return callback(err) + Videos.listFromRemotes(callbackAsync) + }, - videos.removeRemoteVideos(videosList, function (err) { - if (err) { - logger.error('Cannot remove remote videos.', { error: err }) - return callback(err) - } + function removeTheRemoteVideos (videosList, callbackAsync) { + videos.removeRemoteVideos(videosList, function (err) { + if (err) { + logger.error('Cannot remove remote videos.', { error: err }) + return callbackAsync(err) + } - logger.info('Removed all remote videos.') - callback(null) - }) - }) + return callbackAsync(null) }) - }) + } + ], function (err) { + // Don't forget to re activate the scheduler, even if there was an error + requestsScheduler.activate() + + if (err) return callback(err) + + logger.info('Removed all remote videos.') + return callback(null) }) } @@ -159,9 +174,7 @@ function computeWinningPods (urls, podsScore) { Object.keys(podsScore).forEach(function (pod) { if (podsScore[pod] > baseScore) podsList.push({ url: pod }) }) - console.log(urls) - console.log(podsScore) - console.log(podsList) + return podsList } @@ -199,6 +212,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) { podsList, + // Callback called after each request function eachRequest (err, response, body, url, pod, callbackEachRequest) { // We add the pod if it responded correctly with its public certificate if (!err && response.statusCode === 200) { @@ -224,6 +238,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) { } }, + // Final callback, we've ended all the requests function endRequests (err) { // Now we made new friends, we can re activate the pool of requests requestsScheduler.activate() -- cgit v1.2.3