]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
1a42c9e2 C |
3 | const each = require('async/each') |
4 | const eachLimit = require('async/eachLimit') | |
35f1c54e | 5 | const map = require('lodash/map') |
aaf61f38 | 6 | const mongoose = require('mongoose') |
1a42c9e2 | 7 | const waterfall = require('async/waterfall') |
9f10b292 | 8 | |
f0f5567b C |
9 | const constants = require('../initializers/constants') |
10 | const logger = require('../helpers/logger') | |
f0f5567b | 11 | const requests = require('../helpers/requests') |
aaf61f38 | 12 | |
a3ee6fa2 | 13 | const Pod = mongoose.model('Pod') |
aaf61f38 | 14 | const Video = mongoose.model('Video') |
9f10b292 | 15 | |
f0f5567b | 16 | let timer = null |
5abeec31 | 17 | let lastRequestTimestamp = 0 |
9f10b292 | 18 | |
00057e85 | 19 | // --------------------------------------------------------------------------- |
9f10b292 | 20 | |
00057e85 C |
21 | const RequestSchema = mongoose.Schema({ |
22 | request: mongoose.Schema.Types.Mixed, | |
d3cd34be | 23 | to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Pod' } ] |
00057e85 | 24 | }) |
9f10b292 | 25 | |
00057e85 C |
26 | RequestSchema.statics = { |
27 | activate, | |
28 | deactivate, | |
29 | flush, | |
d3cd34be | 30 | forceSend, |
5abeec31 C |
31 | list, |
32 | remainingMilliSeconds | |
00057e85 | 33 | } |
9f10b292 | 34 | |
00057e85 C |
35 | RequestSchema.pre('save', function (next) { |
36 | const self = this | |
528a9efa | 37 | |
00057e85 | 38 | if (self.to.length === 0) { |
a3ee6fa2 | 39 | Pod.listAllIds(function (err, podIds) { |
00057e85 | 40 | if (err) return next(err) |
9f10b292 | 41 | |
00057e85 C |
42 | // No friends |
43 | if (podIds.length === 0) return | |
528a9efa | 44 | |
00057e85 C |
45 | self.to = podIds |
46 | return next() | |
528a9efa | 47 | }) |
00057e85 C |
48 | } else { |
49 | return next() | |
528a9efa | 50 | } |
00057e85 | 51 | }) |
528a9efa | 52 | |
00057e85 C |
53 | mongoose.model('Request', RequestSchema) |
54 | ||
55 | // ------------------------------ STATICS ------------------------------ | |
56 | ||
57 | function activate () { | |
58 | logger.info('Requests scheduler activated.') | |
5abeec31 C |
59 | lastRequestTimestamp = Date.now() |
60 | ||
61 | const self = this | |
62 | timer = setInterval(function () { | |
63 | lastRequestTimestamp = Date.now() | |
64 | makeRequests.call(self) | |
65 | }, constants.REQUESTS_INTERVAL) | |
9f10b292 | 66 | } |
1fe5076f | 67 | |
9f10b292 | 68 | function deactivate () { |
e3647ae2 | 69 | logger.info('Requests scheduler deactivated.') |
9f10b292 | 70 | clearInterval(timer) |
5abeec31 | 71 | timer = null |
9f10b292 | 72 | } |
1fe5076f | 73 | |
528a9efa | 74 | function flush () { |
00057e85 C |
75 | removeAll.call(this, function (err) { |
76 | if (err) logger.error('Cannot flush the requests.', { error: err }) | |
528a9efa C |
77 | }) |
78 | } | |
79 | ||
9f10b292 | 80 | function forceSend () { |
e3647ae2 | 81 | logger.info('Force requests scheduler sending.') |
00057e85 | 82 | makeRequests.call(this) |
9f10b292 | 83 | } |
c45f7f84 | 84 | |
d3cd34be C |
85 | function list (callback) { |
86 | this.find({ }, callback) | |
87 | } | |
88 | ||
5abeec31 C |
89 | function remainingMilliSeconds () { |
90 | if (timer === null) return -1 | |
91 | ||
92 | return constants.REQUESTS_INTERVAL - (Date.now() - lastRequestTimestamp) | |
93 | } | |
94 | ||
9f10b292 | 95 | // --------------------------------------------------------------------------- |
c45f7f84 | 96 | |
8c255eb5 | 97 | // Make a requests to friends of a certain type |
528a9efa | 98 | function makeRequest (toPod, requestsToMake, callback) { |
9f10b292 | 99 | if (!callback) callback = function () {} |
c45f7f84 | 100 | |
528a9efa C |
101 | const params = { |
102 | toPod: toPod, | |
103 | encrypt: true, // Security | |
104 | sign: true, // To prove our identity | |
105 | method: 'POST', | |
106 | path: '/api/' + constants.API_VERSION + '/remote/videos', | |
107 | data: requestsToMake // Requests we need to make | |
108 | } | |
109 | ||
110 | // Make multiple retry requests to all of pods | |
111 | // The function fire some useful callbacks | |
112 | requests.makeSecureRequest(params, function (err, res) { | |
113 | if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) { | |
b9135905 C |
114 | logger.error( |
115 | 'Error sending secure request to %s pod.', | |
116 | toPod.url, | |
117 | { | |
7c34bc64 | 118 | error: err || new Error('Status code not 20x : ' + res.statusCode) |
b9135905 C |
119 | } |
120 | ) | |
528a9efa C |
121 | |
122 | return callback(false) | |
9f10b292 | 123 | } |
c45f7f84 | 124 | |
528a9efa | 125 | return callback(true) |
9f10b292 C |
126 | }) |
127 | } | |
128 | ||
8c255eb5 | 129 | // Make all the requests of the scheduler |
e3647ae2 | 130 | function makeRequests () { |
00057e85 C |
131 | const self = this |
132 | ||
b3595463 | 133 | listWithLimit.call(self, constants.REQUESTS_LIMIT, function (err, requests) { |
9f10b292 | 134 | if (err) { |
e3647ae2 | 135 | logger.error('Cannot get the list of requests.', { err: err }) |
9f10b292 C |
136 | return // Abort |
137 | } | |
138 | ||
8c255eb5 C |
139 | // If there are no requests, abort |
140 | if (requests.length === 0) { | |
141 | logger.info('No requests to make.') | |
142 | return | |
143 | } | |
9f10b292 | 144 | |
8c255eb5 C |
145 | logger.info('Making requests to friends.') |
146 | ||
528a9efa | 147 | // Requests by pods id |
8c255eb5 | 148 | const requestsToMake = {} |
9f10b292 | 149 | |
3c8ee69f | 150 | requests.forEach(function (poolRequest) { |
528a9efa C |
151 | poolRequest.to.forEach(function (toPodId) { |
152 | if (!requestsToMake[toPodId]) { | |
153 | requestsToMake[toPodId] = { | |
154 | ids: [], | |
155 | datas: [] | |
156 | } | |
157 | } | |
158 | ||
159 | requestsToMake[toPodId].ids.push(poolRequest._id) | |
160 | requestsToMake[toPodId].datas.push(poolRequest.request) | |
161 | }) | |
3c8ee69f | 162 | }) |
8d6ae227 | 163 | |
528a9efa C |
164 | const goodPods = [] |
165 | const badPods = [] | |
166 | ||
1a42c9e2 | 167 | eachLimit(Object.keys(requestsToMake), constants.REQUESTS_IN_PARALLEL, function (toPodId, callbackEach) { |
528a9efa C |
168 | const requestToMake = requestsToMake[toPodId] |
169 | ||
170 | // FIXME: mongodb request inside a loop :/ | |
a3ee6fa2 | 171 | Pod.load(toPodId, function (err, toPod) { |
00057e85 C |
172 | if (err) { |
173 | logger.error('Error finding pod by id.', { err: err }) | |
174 | return callbackEach() | |
175 | } | |
528a9efa | 176 | |
5abeec31 | 177 | // Maybe the pod is not our friend anymore so simply remove it |
528a9efa | 178 | if (!toPod) { |
d6cf31b7 | 179 | logger.info('Removing %d requests of unexisting pod %s.', requestToMake.ids.length, toPodId) |
00057e85 | 180 | removePodOf.call(self, requestToMake.ids, toPodId) |
528a9efa C |
181 | return callbackEach() |
182 | } | |
183 | ||
184 | makeRequest(toPod, requestToMake.datas, function (success) { | |
528a9efa C |
185 | if (success === true) { |
186 | logger.debug('Removing requests for %s pod.', toPodId, { requestsIds: requestToMake.ids }) | |
3c8ee69f | 187 | |
528a9efa | 188 | // Remove the pod id of these request ids |
00057e85 | 189 | removePodOf.call(self, requestToMake.ids, toPodId) |
528a9efa C |
190 | goodPods.push(toPodId) |
191 | } else { | |
192 | badPods.push(toPodId) | |
193 | } | |
194 | ||
195 | callbackEach() | |
3c8ee69f | 196 | }) |
528a9efa C |
197 | }) |
198 | }, function () { | |
199 | // All the requests were made, we update the pods score | |
200 | updatePodsScore(goodPods, badPods) | |
201 | // Flush requests with no pod | |
00057e85 | 202 | removeWithEmptyTo.call(self) |
528a9efa | 203 | }) |
9f10b292 C |
204 | }) |
205 | } | |
0b697522 | 206 | |
8c255eb5 | 207 | // Remove pods with a score of 0 (too many requests where they were unreachable) |
9f10b292 | 208 | function removeBadPods () { |
1a42c9e2 | 209 | waterfall([ |
e856e334 | 210 | function findBadPods (callback) { |
a3ee6fa2 | 211 | Pod.listBadPods(function (err, pods) { |
e856e334 C |
212 | if (err) { |
213 | logger.error('Cannot find bad pods.', { error: err }) | |
214 | return callback(err) | |
215 | } | |
8d6ae227 | 216 | |
e856e334 C |
217 | return callback(null, pods) |
218 | }) | |
219 | }, | |
8d6ae227 | 220 | |
e856e334 C |
221 | function listVideosOfTheseBadPods (pods, callback) { |
222 | if (pods.length === 0) return callback(null) | |
0b697522 | 223 | |
e856e334 | 224 | const urls = map(pods, 'url') |
0b697522 | 225 | |
aaf61f38 | 226 | Video.listByUrls(urls, function (err, videosList) { |
8425cb89 | 227 | if (err) { |
e856e334 | 228 | logger.error('Cannot list videos urls.', { error: err, urls: urls }) |
a3ee6fa2 | 229 | return callback(null, pods, []) |
8425cb89 | 230 | } |
e856e334 | 231 | |
a3ee6fa2 | 232 | return callback(null, pods, videosList) |
45239549 | 233 | }) |
e856e334 C |
234 | }, |
235 | ||
a3ee6fa2 | 236 | function removeVideosOfTheseBadPods (pods, videosList, callback) { |
e856e334 | 237 | // We don't have to remove pods, skip |
a3ee6fa2 C |
238 | if (typeof pods === 'function') { |
239 | callback = pods | |
240 | return callback(null) | |
241 | } | |
e856e334 | 242 | |
1a42c9e2 | 243 | each(videosList, function (video, callbackEach) { |
aaf61f38 C |
244 | video.remove(callbackEach) |
245 | }, function (err) { | |
246 | if (err) { | |
247 | // Don't stop the process | |
248 | logger.error('Error while removing videos of bad pods.', { error: err }) | |
249 | return | |
250 | } | |
e856e334 | 251 | |
a3ee6fa2 | 252 | return callback(null, pods) |
e856e334 C |
253 | }) |
254 | }, | |
255 | ||
a3ee6fa2 | 256 | function removeBadPodsFromDB (pods, callback) { |
e856e334 | 257 | // We don't have to remove pods, skip |
a3ee6fa2 C |
258 | if (typeof pods === 'function') { |
259 | callback = pods | |
260 | return callback(null) | |
261 | } | |
262 | ||
1a42c9e2 | 263 | each(pods, function (pod, callbackEach) { |
a3ee6fa2 C |
264 | pod.remove(callbackEach) |
265 | }, function (err) { | |
266 | if (err) return callback(err) | |
e856e334 | 267 | |
a3ee6fa2 C |
268 | return callback(null, pods.length) |
269 | }) | |
e856e334 | 270 | } |
a3ee6fa2 | 271 | ], function (err, numberOfPodsRemoved) { |
e856e334 C |
272 | if (err) { |
273 | logger.error('Cannot remove bad pods.', { error: err }) | |
a3ee6fa2 C |
274 | } else if (numberOfPodsRemoved) { |
275 | logger.info('Removed %d pods.', numberOfPodsRemoved) | |
e856e334 C |
276 | } else { |
277 | logger.info('No need to remove bad pods.') | |
278 | } | |
9f10b292 C |
279 | }) |
280 | } | |
0b697522 | 281 | |
bc503c2a C |
282 | function updatePodsScore (goodPods, badPods) { |
283 | logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) | |
0b697522 | 284 | |
a3ee6fa2 | 285 | Pod.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { |
9f10b292 C |
286 | if (err) logger.error('Cannot increment scores of good pods.') |
287 | }) | |
8425cb89 | 288 | |
a3ee6fa2 | 289 | Pod.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) { |
8c255eb5 | 290 | if (err) logger.error('Cannot decrement scores of bad pods.') |
9f10b292 C |
291 | removeBadPods() |
292 | }) | |
293 | } | |
00057e85 | 294 | |
b3595463 C |
295 | function listWithLimit (limit, callback) { |
296 | this.find({ }, { _id: 1, request: 1, to: 1 }).sort({ _id: 1 }).limit(limit).exec(callback) | |
00057e85 C |
297 | } |
298 | ||
299 | function removeAll (callback) { | |
300 | this.remove({ }, callback) | |
301 | } | |
302 | ||
303 | function removePodOf (requestsIds, podId, callback) { | |
304 | if (!callback) callback = function () {} | |
305 | ||
306 | this.update({ _id: { $in: requestsIds } }, { $pull: { to: podId } }, { multi: true }, callback) | |
307 | } | |
308 | ||
309 | function removeWithEmptyTo (callback) { | |
310 | if (!callback) callback = function () {} | |
311 | ||
312 | this.remove({ to: { $size: 0 } }, callback) | |
313 | } |