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