3 const each = require('async/each')
4 const eachLimit = require('async/eachLimit')
5 const mongoose = require('mongoose')
6 const waterfall = require('async/waterfall')
8 const constants = require('../initializers/constants')
9 const logger = require('../helpers/logger')
10 const requests = require('../helpers/requests')
12 const Pod = mongoose.model('Pod')
15 let lastRequestTimestamp = 0
17 // ---------------------------------------------------------------------------
19 const RequestSchema = mongoose.Schema({
20 request: mongoose.Schema.Types.Mixed,
21 to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Pod' } ]
24 RequestSchema.statics = {
33 RequestSchema.pre('save', function (next) {
36 if (self.to.length === 0) {
37 Pod.listAllIds(function (err, podIds) {
38 if (err) return next(err)
41 if (podIds.length === 0) return
51 mongoose.model('Request', RequestSchema)
53 // ------------------------------ STATICS ------------------------------
55 function activate () {
56 logger.info('Requests scheduler activated.')
57 lastRequestTimestamp = Date.now()
60 timer = setInterval(function () {
61 lastRequestTimestamp = Date.now()
62 makeRequests.call(self)
63 }, constants.REQUESTS_INTERVAL)
66 function deactivate () {
67 logger.info('Requests scheduler deactivated.')
73 removeAll.call(this, function (err) {
74 if (err) logger.error('Cannot flush the requests.', { error: err })
78 function forceSend () {
79 logger.info('Force requests scheduler sending.')
80 makeRequests.call(this)
83 function list (callback) {
84 this.find({ }, callback)
87 function remainingMilliSeconds () {
88 if (timer === null) return -1
90 return constants.REQUESTS_INTERVAL - (Date.now() - lastRequestTimestamp)
93 // ---------------------------------------------------------------------------
95 // Make a requests to friends of a certain type
96 function makeRequest (toPod, requestsToMake, callback) {
97 if (!callback) callback = function () {}
101 encrypt: true, // Security
102 sign: true, // To prove our identity
104 path: '/api/' + constants.API_VERSION + '/remote/videos',
105 data: requestsToMake // Requests we need to make
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)) {
113 'Error sending secure request to %s pod.',
116 error: err || new Error('Status code not 20x : ' + res.statusCode)
120 return callback(false)
123 return callback(true)
127 // Make all the requests of the scheduler
128 function makeRequests () {
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) {
135 logger.error('Cannot get the list of requests.', { err: err })
139 // If there are no requests, abort
140 if (requests.length === 0) {
141 logger.info('No requests to make.')
145 logger.info('Making requests to friends.')
147 // Requests by pods id
148 const requestsToMake = {}
150 requests.forEach(function (poolRequest) {
151 poolRequest.to.forEach(function (toPodId) {
152 if (!requestsToMake[toPodId]) {
153 requestsToMake[toPodId] = {
159 requestsToMake[toPodId].ids.push(poolRequest._id)
160 requestsToMake[toPodId].datas.push(poolRequest.request)
167 eachLimit(Object.keys(requestsToMake), constants.REQUESTS_IN_PARALLEL, function (toPodId, callbackEach) {
168 const requestToMake = requestsToMake[toPodId]
170 // FIXME: mongodb request inside a loop :/
171 Pod.load(toPodId, function (err, toPod) {
173 logger.error('Error finding pod by id.', { err: err })
174 return callbackEach()
177 // Maybe the pod is not our friend anymore so simply remove it
179 logger.info('Removing %d requests of unexisting pod %s.', requestToMake.ids.length, toPodId)
180 removePodOf.call(self, requestToMake.ids, toPodId)
181 return callbackEach()
184 makeRequest(toPod, requestToMake.datas, function (success) {
185 if (success === true) {
186 logger.debug('Removing requests for %s pod.', toPodId, { requestsIds: requestToMake.ids })
188 goodPods.push(toPodId)
190 // Remove the pod id of these request ids
191 removePodOf.call(self, requestToMake.ids, toPodId, callbackEach)
193 badPods.push(toPodId)
199 // All the requests were made, we update the pods score
200 updatePodsScore(goodPods, badPods)
201 // Flush requests with no pod
202 removeWithEmptyTo.call(self)
207 // Remove pods with a score of 0 (too many requests where they were unreachable)
208 function removeBadPods () {
210 function findBadPods (callback) {
211 Pod.listBadPods(function (err, pods) {
213 logger.error('Cannot find bad pods.', { error: err })
217 return callback(null, pods)
221 function removeTheseBadPods (pods, callback) {
222 if (pods.length === 0) return callback(null, 0)
224 each(pods, function (pod, callbackEach) {
225 pod.remove(callbackEach)
227 return callback(err, pods.length)
230 ], function (err, numberOfPodsRemoved) {
232 logger.error('Cannot remove bad pods.', { error: err })
233 } else if (numberOfPodsRemoved) {
234 logger.info('Removed %d pods.', numberOfPodsRemoved)
236 logger.info('No need to remove bad pods.')
241 function updatePodsScore (goodPods, badPods) {
242 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
244 Pod.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) {
245 if (err) logger.error('Cannot increment scores of good pods.')
248 Pod.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) {
249 if (err) logger.error('Cannot decrement scores of bad pods.')
254 function listWithLimitAndRandom (limit, callback) {
257 self.count(function (err, count) {
258 if (err) return callback(err)
260 let start = Math.floor(Math.random() * count) - limit
261 if (start < 0) start = 0
263 self.find({ }, { _id: 1, request: 1, to: 1 }).sort({ _id: 1 }).skip(start).limit(limit).exec(callback)
267 function removeAll (callback) {
268 this.remove({ }, callback)
271 function removePodOf (requestsIds, podId, callback) {
272 if (!callback) callback = function () {}
274 this.update({ _id: { $in: requestsIds } }, { $pull: { to: podId } }, { multi: true }, callback)
277 function removeWithEmptyTo (callback) {
278 if (!callback) callback = function () {}
280 this.remove({ to: { $size: 0 } }, callback)