]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
1a42c9e2 C |
3 | const each = require('async/each') |
4 | const eachLimit = require('async/eachLimit') | |
aaf61f38 | 5 | const mongoose = require('mongoose') |
1a42c9e2 | 6 | const waterfall = require('async/waterfall') |
9f10b292 | 7 | |
f0f5567b C |
8 | const constants = require('../initializers/constants') |
9 | const logger = require('../helpers/logger') | |
f0f5567b | 10 | const requests = require('../helpers/requests') |
aaf61f38 | 11 | |
a3ee6fa2 | 12 | const Pod = mongoose.model('Pod') |
9f10b292 | 13 | |
f0f5567b | 14 | let timer = null |
5abeec31 | 15 | let lastRequestTimestamp = 0 |
9f10b292 | 16 | |
00057e85 | 17 | // --------------------------------------------------------------------------- |
9f10b292 | 18 | |
00057e85 C |
19 | const RequestSchema = mongoose.Schema({ |
20 | request: mongoose.Schema.Types.Mixed, | |
d3cd34be | 21 | to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Pod' } ] |
00057e85 | 22 | }) |
9f10b292 | 23 | |
00057e85 C |
24 | RequestSchema.statics = { |
25 | activate, | |
26 | deactivate, | |
27 | flush, | |
d3cd34be | 28 | forceSend, |
5abeec31 C |
29 | list, |
30 | remainingMilliSeconds | |
00057e85 | 31 | } |
9f10b292 | 32 | |
00057e85 C |
33 | RequestSchema.pre('save', function (next) { |
34 | const self = this | |
528a9efa | 35 | |
00057e85 | 36 | if (self.to.length === 0) { |
a3ee6fa2 | 37 | Pod.listAllIds(function (err, podIds) { |
00057e85 | 38 | if (err) return next(err) |
9f10b292 | 39 | |
00057e85 C |
40 | // No friends |
41 | if (podIds.length === 0) return | |
528a9efa | 42 | |
00057e85 C |
43 | self.to = podIds |
44 | return next() | |
528a9efa | 45 | }) |
00057e85 C |
46 | } else { |
47 | return next() | |
528a9efa | 48 | } |
00057e85 | 49 | }) |
528a9efa | 50 | |
00057e85 C |
51 | mongoose.model('Request', RequestSchema) |
52 | ||
53 | // ------------------------------ STATICS ------------------------------ | |
54 | ||
55 | function activate () { | |
56 | logger.info('Requests scheduler activated.') | |
5abeec31 C |
57 | lastRequestTimestamp = Date.now() |
58 | ||
59 | const self = this | |
60 | timer = setInterval(function () { | |
61 | lastRequestTimestamp = Date.now() | |
62 | makeRequests.call(self) | |
63 | }, constants.REQUESTS_INTERVAL) | |
9f10b292 | 64 | } |
1fe5076f | 65 | |
9f10b292 | 66 | function deactivate () { |
e3647ae2 | 67 | logger.info('Requests scheduler deactivated.') |
9f10b292 | 68 | clearInterval(timer) |
5abeec31 | 69 | timer = null |
9f10b292 | 70 | } |
1fe5076f | 71 | |
528a9efa | 72 | function flush () { |
00057e85 C |
73 | removeAll.call(this, function (err) { |
74 | if (err) logger.error('Cannot flush the requests.', { error: err }) | |
528a9efa C |
75 | }) |
76 | } | |
77 | ||
9f10b292 | 78 | function forceSend () { |
e3647ae2 | 79 | logger.info('Force requests scheduler sending.') |
00057e85 | 80 | makeRequests.call(this) |
9f10b292 | 81 | } |
c45f7f84 | 82 | |
d3cd34be C |
83 | function list (callback) { |
84 | this.find({ }, callback) | |
85 | } | |
86 | ||
5abeec31 C |
87 | function remainingMilliSeconds () { |
88 | if (timer === null) return -1 | |
89 | ||
90 | return constants.REQUESTS_INTERVAL - (Date.now() - lastRequestTimestamp) | |
91 | } | |
92 | ||
9f10b292 | 93 | // --------------------------------------------------------------------------- |
c45f7f84 | 94 | |
8c255eb5 | 95 | // Make a requests to friends of a certain type |
528a9efa | 96 | function makeRequest (toPod, requestsToMake, callback) { |
9f10b292 | 97 | if (!callback) callback = function () {} |
c45f7f84 | 98 | |
528a9efa C |
99 | const params = { |
100 | toPod: toPod, | |
101 | encrypt: true, // Security | |
102 | sign: true, // To prove our identity | |
103 | method: 'POST', | |
104 | path: '/api/' + constants.API_VERSION + '/remote/videos', | |
105 | data: requestsToMake // Requests we need to make | |
106 | } | |
107 | ||
108 | // Make multiple retry requests to all of pods | |
109 | // The function fire some useful callbacks | |
110 | requests.makeSecureRequest(params, function (err, res) { | |
111 | if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) { | |
b9135905 C |
112 | logger.error( |
113 | 'Error sending secure request to %s pod.', | |
114 | toPod.url, | |
115 | { | |
7c34bc64 | 116 | error: err || new Error('Status code not 20x : ' + res.statusCode) |
b9135905 C |
117 | } |
118 | ) | |
528a9efa C |
119 | |
120 | return callback(false) | |
9f10b292 | 121 | } |
c45f7f84 | 122 | |
528a9efa | 123 | return callback(true) |
9f10b292 C |
124 | }) |
125 | } | |
126 | ||
8c255eb5 | 127 | // Make all the requests of the scheduler |
e3647ae2 | 128 | function makeRequests () { |
00057e85 C |
129 | const self = this |
130 | ||
43666d61 C |
131 | // We limit the size of the requests (REQUESTS_LIMIT) |
132 | // We don't want to stuck with the same failing requests so we get a random list | |
133 | listWithLimitAndRandom.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 | goodPods.push(toPodId) |
c2ee5ce8 C |
189 | |
190 | // Remove the pod id of these request ids | |
191 | removePodOf.call(self, requestToMake.ids, toPodId, callbackEach) | |
528a9efa C |
192 | } else { |
193 | badPods.push(toPodId) | |
c2ee5ce8 | 194 | callbackEach() |
528a9efa | 195 | } |
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 | |
80a6c9e7 C |
221 | function removeTheseBadPods (pods, callback) { |
222 | if (pods.length === 0) return callback(null, 0) | |
a3ee6fa2 | 223 | |
1a42c9e2 | 224 | each(pods, function (pod, callbackEach) { |
a3ee6fa2 C |
225 | pod.remove(callbackEach) |
226 | }, function (err) { | |
80a6c9e7 | 227 | return callback(err, pods.length) |
a3ee6fa2 | 228 | }) |
e856e334 | 229 | } |
a3ee6fa2 | 230 | ], function (err, numberOfPodsRemoved) { |
e856e334 C |
231 | if (err) { |
232 | logger.error('Cannot remove bad pods.', { error: err }) | |
a3ee6fa2 C |
233 | } else if (numberOfPodsRemoved) { |
234 | logger.info('Removed %d pods.', numberOfPodsRemoved) | |
e856e334 C |
235 | } else { |
236 | logger.info('No need to remove bad pods.') | |
237 | } | |
9f10b292 C |
238 | }) |
239 | } | |
0b697522 | 240 | |
bc503c2a C |
241 | function updatePodsScore (goodPods, badPods) { |
242 | logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) | |
0b697522 | 243 | |
a3ee6fa2 | 244 | Pod.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { |
9f10b292 C |
245 | if (err) logger.error('Cannot increment scores of good pods.') |
246 | }) | |
8425cb89 | 247 | |
a3ee6fa2 | 248 | Pod.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 | } | |
00057e85 | 253 | |
43666d61 C |
254 | function listWithLimitAndRandom (limit, callback) { |
255 | const self = this | |
256 | ||
257 | self.count(function (err, count) { | |
258 | if (err) return callback(err) | |
259 | ||
260 | let start = Math.floor(Math.random() * count) - limit | |
261 | if (start < 0) start = 0 | |
262 | ||
263 | self.find({ }, { _id: 1, request: 1, to: 1 }).sort({ _id: 1 }).skip(start).limit(limit).exec(callback) | |
264 | }) | |
00057e85 C |
265 | } |
266 | ||
267 | function removeAll (callback) { | |
268 | this.remove({ }, callback) | |
269 | } | |
270 | ||
271 | function removePodOf (requestsIds, podId, callback) { | |
272 | if (!callback) callback = function () {} | |
273 | ||
274 | this.update({ _id: { $in: requestsIds } }, { $pull: { to: podId } }, { multi: true }, callback) | |
275 | } | |
276 | ||
277 | function removeWithEmptyTo (callback) { | |
278 | if (!callback) callback = function () {} | |
279 | ||
280 | this.remove({ to: { $size: 0 } }, callback) | |
281 | } |