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