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