]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/requestsScheduler.js
Remove useless use of async.each
[github/Chocobozzz/PeerTube.git] / server / lib / requestsScheduler.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b 3const async = require('async')
35f1c54e 4const map = require('lodash/map')
9f10b292 5
f0f5567b
C
6const constants = require('../initializers/constants')
7const logger = require('../helpers/logger')
8const Pods = require('../models/pods')
e3647ae2 9const Requests = require('../models/requests')
f0f5567b 10const requests = require('../helpers/requests')
cbe2f7c3 11const videos = require('../lib/videos')
f0f5567b 12const Videos = require('../models/videos')
9f10b292 13
8c255eb5 14const REQUEST_SCHEDULER_TYPE = constants.REQUEST_SCHEDULER_TYPE
f0f5567b 15let timer = null
9f10b292 16
e3647ae2 17const requestsScheduler = {
9f10b292
C
18 activate: activate,
19 addRequest: addRequest,
20 deactivate: deactivate,
21 forceSend: forceSend
22}
23
24function activate () {
e3647ae2
C
25 logger.info('Requests scheduler activated.')
26 timer = setInterval(makeRequests, constants.INTERVAL)
9f10b292
C
27}
28
8c255eb5 29// Add request to the scheduler
9f10b292 30function addRequest (id, type, request) {
e3647ae2 31 logger.debug('Add request to the requests scheduler.', { id: id, type: type, request: request })
9f10b292 32
e3647ae2 33 Requests.findById(id, function (err, entity) {
9f10b292 34 if (err) {
8c255eb5 35 logger.error('Error when trying to find a request.', { error: err })
9f10b292
C
36 return // Abort
37 }
38
8c255eb5 39 // If there were already a request with this id in the scheduler...
9f10b292
C
40 if (entity) {
41 if (entity.type === type) {
42 logger.error('Cannot insert two same requests.')
1fe5076f
C
43 return // Abort
44 }
45
9f10b292 46 // Remove the request of the other type
e3647ae2 47 Requests.removeRequestById(id, function (err) {
9f10b292 48 if (err) {
e3647ae2 49 logger.error('Cannot remove a request.', { error: err })
1fe5076f
C
50 return // Abort
51 }
9f10b292
C
52 })
53 } else {
e3647ae2
C
54 Requests.create(id, type, request, function (err) {
55 if (err) logger.error('Cannot create a request.', { error: err })
9f10b292
C
56 return // Abort
57 })
58 }
59 })
60}
1fe5076f 61
9f10b292 62function deactivate () {
e3647ae2 63 logger.info('Requests scheduler deactivated.')
9f10b292
C
64 clearInterval(timer)
65}
1fe5076f 66
9f10b292 67function forceSend () {
e3647ae2
C
68 logger.info('Force requests scheduler sending.')
69 makeRequests()
9f10b292 70}
c45f7f84 71
9f10b292 72// ---------------------------------------------------------------------------
c45f7f84 73
e3647ae2 74module.exports = requestsScheduler
c45f7f84 75
9f10b292 76// ---------------------------------------------------------------------------
c45f7f84 77
8c255eb5 78// Make a requests to friends of a certain type
bc503c2a 79function makeRequest (type, requestsToMake, callback) {
9f10b292 80 if (!callback) callback = function () {}
c45f7f84 81
9f10b292
C
82 Pods.list(function (err, pods) {
83 if (err) return callback(err)
c45f7f84 84
f0f5567b 85 const params = {
8c255eb5
C
86 encrypt: true, // Security
87 sign: true, // To prove our identity
9f10b292 88 method: 'POST',
8c255eb5
C
89 path: null, // We build the path later
90 data: requestsToMake // Requests we need to make
9f10b292 91 }
c45f7f84 92
8c255eb5
C
93 // If this is a valid type, we build the path
94 if (REQUEST_SCHEDULER_TYPE.indexOf(type) > -1) {
95 params.path = '/api/' + constants.API_VERSION + '/remotevideos/' + type
9f10b292
C
96 } else {
97 return callback(new Error('Unkown pool request type.'))
98 }
99
bc503c2a
C
100 const badPods = []
101 const goodPods = []
c45f7f84 102
8c255eb5
C
103 // Make multiple retry requests to all of pods
104 // The function fire some useful callbacks
9f10b292
C
105 requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
106
bc503c2a 107 function callbackEachPodFinished (err, response, body, url, pod, callbackEachPodFinished) {
8c255eb5 108 // We failed the request, add the pod unreachable to the bad pods list
cbe2f7c3 109 if (err || (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204)) {
bc503c2a 110 badPods.push(pod._id)
9f10b292 111 logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') })
c45f7f84 112 } else {
8c255eb5 113 // Request success
bc503c2a 114 goodPods.push(pod._id)
c45f7f84
C
115 }
116
bc503c2a 117 return callbackEachPodFinished()
9f10b292 118 }
c45f7f84 119
9f10b292
C
120 function callbackAllPodsFinished (err) {
121 if (err) return callback(err)
c45f7f84 122
8c255eb5 123 // All the requests were made, we update the pods score
bc503c2a 124 updatePodsScore(goodPods, badPods)
9f10b292
C
125 callback(null)
126 }
127 })
128}
129
8c255eb5 130// Make all the requests of the scheduler
e3647ae2 131function makeRequests () {
e3647ae2 132 Requests.list(function (err, requests) {
9f10b292 133 if (err) {
e3647ae2 134 logger.error('Cannot get the list of requests.', { err: err })
9f10b292
C
135 return // Abort
136 }
137
8c255eb5
C
138 // If there are no requests, abort
139 if (requests.length === 0) {
140 logger.info('No requests to make.')
141 return
142 }
9f10b292 143
8c255eb5
C
144 logger.info('Making requests to friends.')
145
146 const requestsToMake = {}
147 for (const type of REQUEST_SCHEDULER_TYPE) {
148 requestsToMake[type] = {
9f10b292
C
149 ids: [],
150 requests: []
c45f7f84 151 }
9f10b292
C
152 }
153
8c255eb5 154 // For each requests to make, we add it to the correct request type
3c8ee69f 155 requests.forEach(function (poolRequest) {
8c255eb5
C
156 if (REQUEST_SCHEDULER_TYPE.indexOf(poolRequest.type) > -1) {
157 const requestTypeToMake = requestsToMake[poolRequest.type]
158 requestTypeToMake.requests.push(poolRequest.request)
159 requestTypeToMake.ids.push(poolRequest._id)
9f10b292 160 } else {
bc503c2a 161 logger.error('Unkown request type.', { request_type: poolRequest.type })
9f10b292 162 return // abort
c45f7f84 163 }
3c8ee69f 164 })
8d6ae227 165
3c8ee69f
C
166 for (let type of Object.keys(requestsToMake)) {
167 const requestTypeToMake = requestsToMake[type]
168 // If there are requests for this type
169 if (requestTypeToMake.requests.length !== 0) {
170 makeRequest(type, requestTypeToMake.requests, function (err) {
171 if (err) logger.error('Errors when sent ' + type + ' requests.', { error: err })
172
173 // We made the requests, so we can remove them from the scheduler
174 Requests.removeRequests(requestTypeToMake.ids)
175 })
0b697522 176 }
3c8ee69f 177 }
9f10b292
C
178 })
179}
0b697522 180
8c255eb5 181// Remove pods with a score of 0 (too many requests where they were unreachable)
9f10b292 182function removeBadPods () {
e856e334
C
183 async.waterfall([
184 function findBadPods (callback) {
185 Pods.findBadPods(function (err, pods) {
186 if (err) {
187 logger.error('Cannot find bad pods.', { error: err })
188 return callback(err)
189 }
8d6ae227 190
e856e334
C
191 return callback(null, pods)
192 })
193 },
8d6ae227 194
e856e334
C
195 function listVideosOfTheseBadPods (pods, callback) {
196 if (pods.length === 0) return callback(null)
0b697522 197
e856e334
C
198 const urls = map(pods, 'url')
199 const ids = map(pods, '_id')
0b697522 200
e856e334 201 Videos.listFromUrls(urls, function (err, videosList) {
8425cb89 202 if (err) {
e856e334
C
203 logger.error('Cannot list videos urls.', { error: err, urls: urls })
204 return callback(null, ids, [])
8425cb89 205 }
e856e334
C
206
207 return callback(null, ids, videosList)
45239549 208 })
e856e334
C
209 },
210
211 function removeVideosOfTheseBadPods (podIds, videosList, callback) {
212 // We don't have to remove pods, skip
213 if (typeof podIds === 'function') return podIds(null)
214
215 // Remove the remote videos
216 videos.removeRemoteVideos(videosList, function (err) {
217 if (err) logger.error('Cannot remove remote videos.', { error: err })
218
219 return callback(null, podIds)
220 })
221 },
222
223 function removeBadPodsFromDB (podIds, callback) {
224 // We don't have to remove pods, skip
225 if (typeof podIds === 'function') return podIds(null)
226
227 Pods.removeAllByIds(podIds, callback)
228 }
229 ], function (err, removeResult) {
230 if (err) {
231 logger.error('Cannot remove bad pods.', { error: err })
232 } else if (removeResult) {
233 const podsRemoved = removeResult.result.n
234 logger.info('Removed %d pods.', podsRemoved)
235 } else {
236 logger.info('No need to remove bad pods.')
237 }
9f10b292
C
238 })
239}
0b697522 240
bc503c2a
C
241function updatePodsScore (goodPods, badPods) {
242 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
0b697522 243
bc503c2a 244 Pods.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) {
9f10b292
C
245 if (err) logger.error('Cannot increment scores of good pods.')
246 })
8425cb89 247
bc503c2a 248 Pods.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) {
8c255eb5 249 if (err) logger.error('Cannot decrement scores of bad pods.')
9f10b292
C
250 removeBadPods()
251 })
252}