diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-05-15 10:42:17 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-05-15 10:42:17 +0200 |
commit | e7ea2817c0830e88090424ca3ed0038f257f0d34 (patch) | |
tree | 4e0902eb2d883637f7c4a75043f0e2c4633cdc88 /server/lib/friends.js | |
parent | cefc718dd6b6c0adafbb1e7de122217cffa25cd0 (diff) | |
download | PeerTube-e7ea2817c0830e88090424ca3ed0038f257f0d34.tar.gz PeerTube-e7ea2817c0830e88090424ca3ed0038f257f0d34.tar.zst PeerTube-e7ea2817c0830e88090424ca3ed0038f257f0d34.zip |
Use async waterfall for better readability in friends lib
Diffstat (limited to 'server/lib/friends.js')
-rw-r--r-- | server/lib/friends.js | 79 |
1 files changed, 47 insertions, 32 deletions
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) { | |||
80 | // Flush pool requests | 80 | // Flush pool requests |
81 | requestsScheduler.forceSend() | 81 | requestsScheduler.forceSend() |
82 | 82 | ||
83 | Pods.list(function (err, pods) { | 83 | async.waterfall([ |
84 | if (err) return callback(err) | 84 | function getPodsList (callbackAsync) { |
85 | 85 | return Pods.list(callbackAsync) | |
86 | const request = { | 86 | }, |
87 | method: 'POST', | 87 | |
88 | path: '/api/' + constants.API_VERSION + '/pods/remove', | 88 | function announceIQuitMyFriends (pods, callbackAsync) { |
89 | sign: true, | 89 | const request = { |
90 | encrypt: true, | 90 | method: 'POST', |
91 | data: { | 91 | path: '/api/' + constants.API_VERSION + '/pods/remove', |
92 | url: 'me' // Fake data | 92 | sign: true, |
93 | encrypt: true, | ||
94 | data: { | ||
95 | url: 'me' // Fake data | ||
96 | } | ||
93 | } | 97 | } |
94 | } | ||
95 | 98 | ||
96 | // Announce we quit them | 99 | // Announce we quit them |
97 | requests.makeMultipleRetryRequest(request, pods, function () { | 100 | requests.makeMultipleRetryRequest(request, pods, function (err) { |
98 | Pods.removeAll(function (err) { | 101 | return callbackAsync(err) |
99 | requestsScheduler.activate() | 102 | }) |
103 | }, | ||
100 | 104 | ||
101 | if (err) return callback(err) | 105 | function removePodsFromDB (callbackAsync) { |
106 | Pods.removeAll(function (err) { | ||
107 | return callbackAsync(err) | ||
108 | }) | ||
109 | }, | ||
102 | 110 | ||
103 | logger.info('Broke friends, so sad :(') | 111 | function listRemoteVideos (callbackAsync) { |
112 | logger.info('Broke friends, so sad :(') | ||
104 | 113 | ||
105 | Videos.listFromRemotes(function (err, videosList) { | 114 | Videos.listFromRemotes(callbackAsync) |
106 | if (err) return callback(err) | 115 | }, |
107 | 116 | ||
108 | videos.removeRemoteVideos(videosList, function (err) { | 117 | function removeTheRemoteVideos (videosList, callbackAsync) { |
109 | if (err) { | 118 | videos.removeRemoteVideos(videosList, function (err) { |
110 | logger.error('Cannot remove remote videos.', { error: err }) | 119 | if (err) { |
111 | return callback(err) | 120 | logger.error('Cannot remove remote videos.', { error: err }) |
112 | } | 121 | return callbackAsync(err) |
122 | } | ||
113 | 123 | ||
114 | logger.info('Removed all remote videos.') | 124 | return callbackAsync(null) |
115 | callback(null) | ||
116 | }) | ||
117 | }) | ||
118 | }) | 125 | }) |
119 | }) | 126 | } |
127 | ], function (err) { | ||
128 | // Don't forget to re activate the scheduler, even if there was an error | ||
129 | requestsScheduler.activate() | ||
130 | |||
131 | if (err) return callback(err) | ||
132 | |||
133 | logger.info('Removed all remote videos.') | ||
134 | return callback(null) | ||
120 | }) | 135 | }) |
121 | } | 136 | } |
122 | 137 | ||
@@ -159,9 +174,7 @@ function computeWinningPods (urls, podsScore) { | |||
159 | Object.keys(podsScore).forEach(function (pod) { | 174 | Object.keys(podsScore).forEach(function (pod) { |
160 | if (podsScore[pod] > baseScore) podsList.push({ url: pod }) | 175 | if (podsScore[pod] > baseScore) podsList.push({ url: pod }) |
161 | }) | 176 | }) |
162 | console.log(urls) | 177 | |
163 | console.log(podsScore) | ||
164 | console.log(podsList) | ||
165 | return podsList | 178 | return podsList |
166 | } | 179 | } |
167 | 180 | ||
@@ -199,6 +212,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) { | |||
199 | 212 | ||
200 | podsList, | 213 | podsList, |
201 | 214 | ||
215 | // Callback called after each request | ||
202 | function eachRequest (err, response, body, url, pod, callbackEachRequest) { | 216 | function eachRequest (err, response, body, url, pod, callbackEachRequest) { |
203 | // We add the pod if it responded correctly with its public certificate | 217 | // We add the pod if it responded correctly with its public certificate |
204 | if (!err && response.statusCode === 200) { | 218 | if (!err && response.statusCode === 200) { |
@@ -224,6 +238,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) { | |||
224 | } | 238 | } |
225 | }, | 239 | }, |
226 | 240 | ||
241 | // Final callback, we've ended all the requests | ||
227 | function endRequests (err) { | 242 | function endRequests (err) { |
228 | // Now we made new friends, we can re activate the pool of requests | 243 | // Now we made new friends, we can re activate the pool of requests |
229 | requestsScheduler.activate() | 244 | requestsScheduler.activate() |