]>
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) { |
00057e85 | 179 | removePodOf.call(self, requestToMake.ids, toPodId) |
528a9efa C |
180 | return callbackEach() |
181 | } | |
182 | ||
183 | makeRequest(toPod, requestToMake.datas, function (success) { | |
528a9efa C |
184 | if (success === true) { |
185 | logger.debug('Removing requests for %s pod.', toPodId, { requestsIds: requestToMake.ids }) | |
3c8ee69f | 186 | |
528a9efa | 187 | // Remove the pod id of these request ids |
00057e85 | 188 | removePodOf.call(self, requestToMake.ids, toPodId) |
528a9efa C |
189 | goodPods.push(toPodId) |
190 | } else { | |
191 | badPods.push(toPodId) | |
192 | } | |
193 | ||
194 | callbackEach() | |
3c8ee69f | 195 | }) |
528a9efa C |
196 | }) |
197 | }, function () { | |
198 | // All the requests were made, we update the pods score | |
199 | updatePodsScore(goodPods, badPods) | |
200 | // Flush requests with no pod | |
00057e85 | 201 | removeWithEmptyTo.call(self) |
528a9efa | 202 | }) |
9f10b292 C |
203 | }) |
204 | } | |
0b697522 | 205 | |
8c255eb5 | 206 | // Remove pods with a score of 0 (too many requests where they were unreachable) |
9f10b292 | 207 | function removeBadPods () { |
1a42c9e2 | 208 | waterfall([ |
e856e334 | 209 | function findBadPods (callback) { |
a3ee6fa2 | 210 | Pod.listBadPods(function (err, pods) { |
e856e334 C |
211 | if (err) { |
212 | logger.error('Cannot find bad pods.', { error: err }) | |
213 | return callback(err) | |
214 | } | |
8d6ae227 | 215 | |
e856e334 C |
216 | return callback(null, pods) |
217 | }) | |
218 | }, | |
8d6ae227 | 219 | |
e856e334 C |
220 | function listVideosOfTheseBadPods (pods, callback) { |
221 | if (pods.length === 0) return callback(null) | |
0b697522 | 222 | |
e856e334 | 223 | const urls = map(pods, 'url') |
0b697522 | 224 | |
aaf61f38 | 225 | Video.listByUrls(urls, function (err, videosList) { |
8425cb89 | 226 | if (err) { |
e856e334 | 227 | logger.error('Cannot list videos urls.', { error: err, urls: urls }) |
a3ee6fa2 | 228 | return callback(null, pods, []) |
8425cb89 | 229 | } |
e856e334 | 230 | |
a3ee6fa2 | 231 | return callback(null, pods, videosList) |
45239549 | 232 | }) |
e856e334 C |
233 | }, |
234 | ||
a3ee6fa2 | 235 | function removeVideosOfTheseBadPods (pods, videosList, callback) { |
e856e334 | 236 | // We don't have to remove pods, skip |
a3ee6fa2 C |
237 | if (typeof pods === 'function') { |
238 | callback = pods | |
239 | return callback(null) | |
240 | } | |
e856e334 | 241 | |
1a42c9e2 | 242 | each(videosList, function (video, callbackEach) { |
aaf61f38 C |
243 | video.remove(callbackEach) |
244 | }, function (err) { | |
245 | if (err) { | |
246 | // Don't stop the process | |
247 | logger.error('Error while removing videos of bad pods.', { error: err }) | |
248 | return | |
249 | } | |
e856e334 | 250 | |
a3ee6fa2 | 251 | return callback(null, pods) |
e856e334 C |
252 | }) |
253 | }, | |
254 | ||
a3ee6fa2 | 255 | function removeBadPodsFromDB (pods, callback) { |
e856e334 | 256 | // We don't have to remove pods, skip |
a3ee6fa2 C |
257 | if (typeof pods === 'function') { |
258 | callback = pods | |
259 | return callback(null) | |
260 | } | |
261 | ||
1a42c9e2 | 262 | each(pods, function (pod, callbackEach) { |
a3ee6fa2 C |
263 | pod.remove(callbackEach) |
264 | }, function (err) { | |
265 | if (err) return callback(err) | |
e856e334 | 266 | |
a3ee6fa2 C |
267 | return callback(null, pods.length) |
268 | }) | |
e856e334 | 269 | } |
a3ee6fa2 | 270 | ], function (err, numberOfPodsRemoved) { |
e856e334 C |
271 | if (err) { |
272 | logger.error('Cannot remove bad pods.', { error: err }) | |
a3ee6fa2 C |
273 | } else if (numberOfPodsRemoved) { |
274 | logger.info('Removed %d pods.', numberOfPodsRemoved) | |
e856e334 C |
275 | } else { |
276 | logger.info('No need to remove bad pods.') | |
277 | } | |
9f10b292 C |
278 | }) |
279 | } | |
0b697522 | 280 | |
bc503c2a C |
281 | function updatePodsScore (goodPods, badPods) { |
282 | logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) | |
0b697522 | 283 | |
a3ee6fa2 | 284 | Pod.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { |
9f10b292 C |
285 | if (err) logger.error('Cannot increment scores of good pods.') |
286 | }) | |
8425cb89 | 287 | |
a3ee6fa2 | 288 | Pod.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) { |
8c255eb5 | 289 | if (err) logger.error('Cannot decrement scores of bad pods.') |
9f10b292 C |
290 | removeBadPods() |
291 | }) | |
292 | } | |
00057e85 | 293 | |
b3595463 C |
294 | function listWithLimit (limit, callback) { |
295 | this.find({ }, { _id: 1, request: 1, to: 1 }).sort({ _id: 1 }).limit(limit).exec(callback) | |
00057e85 C |
296 | } |
297 | ||
298 | function removeAll (callback) { | |
299 | this.remove({ }, callback) | |
300 | } | |
301 | ||
302 | function removePodOf (requestsIds, podId, callback) { | |
303 | if (!callback) callback = function () {} | |
304 | ||
305 | this.update({ _id: { $in: requestsIds } }, { $pull: { to: podId } }, { multi: true }, callback) | |
306 | } | |
307 | ||
308 | function removeWithEmptyTo (callback) { | |
309 | if (!callback) callback = function () {} | |
310 | ||
311 | this.remove({ to: { $size: 0 } }, callback) | |
312 | } |