diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-05-15 18:03:43 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-05-15 18:03:43 +0200 |
commit | e856e334a14ac8449b31db24bd5c6fb18f9a23e7 (patch) | |
tree | 1a9e321cdce6711afe07be30b800a9f87ecd6036 /server/lib | |
parent | e7ea2817c0830e88090424ca3ed0038f257f0d34 (diff) | |
download | PeerTube-e856e334a14ac8449b31db24bd5c6fb18f9a23e7.tar.gz PeerTube-e856e334a14ac8449b31db24bd5c6fb18f9a23e7.tar.zst PeerTube-e856e334a14ac8449b31db24bd5c6fb18f9a23e7.zip |
Use async waterfall in request scheduler for better readability
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/requestsScheduler.js | 70 |
1 files changed, 48 insertions, 22 deletions
diff --git a/server/lib/requestsScheduler.js b/server/lib/requestsScheduler.js index f10de6276..3d04b8cc8 100644 --- a/server/lib/requestsScheduler.js +++ b/server/lib/requestsScheduler.js | |||
@@ -177,35 +177,61 @@ function makeRequests () { | |||
177 | } | 177 | } |
178 | 178 | ||
179 | function removeBadPods () { | 179 | function removeBadPods () { |
180 | Pods.findBadPods(function (err, pods) { | 180 | async.waterfall([ |
181 | if (err) { | 181 | function findBadPods (callback) { |
182 | logger.error('Cannot find bad pods.', { error: err }) | 182 | Pods.findBadPods(function (err, pods) { |
183 | return // abort | 183 | if (err) { |
184 | } | 184 | logger.error('Cannot find bad pods.', { error: err }) |
185 | return callback(err) | ||
186 | } | ||
185 | 187 | ||
186 | if (pods.length === 0) return | 188 | return callback(null, pods) |
189 | }) | ||
190 | }, | ||
187 | 191 | ||
188 | const urls = map(pods, 'url') | 192 | function listVideosOfTheseBadPods (pods, callback) { |
189 | const ids = map(pods, '_id') | 193 | if (pods.length === 0) return callback(null) |
190 | 194 | ||
191 | Videos.listFromUrls(urls, function (err, videosList) { | 195 | const urls = map(pods, 'url') |
192 | if (err) { | 196 | const ids = map(pods, '_id') |
193 | logger.error('Cannot list videos urls.', { error: err, urls: urls }) | ||
194 | } else { | ||
195 | videos.removeRemoteVideos(videosList, function (err) { | ||
196 | if (err) logger.error('Cannot remove remote videos.', { error: err }) | ||
197 | }) | ||
198 | } | ||
199 | 197 | ||
200 | Pods.removeAllByIds(ids, function (err, r) { | 198 | Videos.listFromUrls(urls, function (err, videosList) { |
201 | if (err) { | 199 | if (err) { |
202 | logger.error('Cannot remove bad pods.', { error: err }) | 200 | logger.error('Cannot list videos urls.', { error: err, urls: urls }) |
203 | } else { | 201 | return callback(null, ids, []) |
204 | const podsRemoved = r.result.n | ||
205 | logger.info('Removed %d pods.', podsRemoved) | ||
206 | } | 202 | } |
203 | |||
204 | return callback(null, ids, videosList) | ||
207 | }) | 205 | }) |
208 | }) | 206 | }, |
207 | |||
208 | function removeVideosOfTheseBadPods (podIds, videosList, callback) { | ||
209 | // We don't have to remove pods, skip | ||
210 | if (typeof podIds === 'function') return podIds(null) | ||
211 | |||
212 | // Remove the remote videos | ||
213 | videos.removeRemoteVideos(videosList, function (err) { | ||
214 | if (err) logger.error('Cannot remove remote videos.', { error: err }) | ||
215 | |||
216 | return callback(null, podIds) | ||
217 | }) | ||
218 | }, | ||
219 | |||
220 | function removeBadPodsFromDB (podIds, callback) { | ||
221 | // We don't have to remove pods, skip | ||
222 | if (typeof podIds === 'function') return podIds(null) | ||
223 | |||
224 | Pods.removeAllByIds(podIds, callback) | ||
225 | } | ||
226 | ], function (err, removeResult) { | ||
227 | if (err) { | ||
228 | logger.error('Cannot remove bad pods.', { error: err }) | ||
229 | } else if (removeResult) { | ||
230 | const podsRemoved = removeResult.result.n | ||
231 | logger.info('Removed %d pods.', podsRemoved) | ||
232 | } else { | ||
233 | logger.info('No need to remove bad pods.') | ||
234 | } | ||
209 | }) | 235 | }) |
210 | } | 236 | } |
211 | 237 | ||