]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/requestsScheduler.js
3 const async
= require('async')
4 const map
= require('lodash/map')
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')
16 const requestsScheduler
= {
18 addRequest: addRequest
,
19 deactivate: deactivate
,
23 function activate () {
24 logger
.info('Requests scheduler activated.')
25 timer
= setInterval(makeRequests
, constants
.INTERVAL
)
28 function addRequest (id
, type
, request
) {
29 logger
.debug('Add request to the requests scheduler.', { id: id
, type: type
, request: request
})
31 Requests
.findById(id
, function (err
, entity
) {
33 logger
.error('Cannot find one request.', { error: err
})
38 if (entity
.type
=== type
) {
39 logger
.error('Cannot insert two same requests.')
43 // Remove the request of the other type
44 Requests
.removeRequestById(id
, function (err
) {
46 logger
.error('Cannot remove a request.', { error: err
})
51 Requests
.create(id
, type
, request
, function (err
) {
52 if (err
) logger
.error('Cannot create a request.', { error: err
})
59 function deactivate () {
60 logger
.info('Requests scheduler deactivated.')
64 function forceSend () {
65 logger
.info('Force requests scheduler sending.')
69 // ---------------------------------------------------------------------------
71 module
.exports
= requestsScheduler
73 // ---------------------------------------------------------------------------
75 function makeRequest (type
, requestsToMake
, callback
) {
76 if (!callback
) callback = function () {}
78 Pods
.list(function (err
, pods
) {
79 if (err
) return callback(err
)
90 params
.path
= '/api/' + constants
.API_VERSION
+ '/remotevideos/add'
91 } else if (type
=== 'remove') {
92 params
.path
= '/api/' + constants
.API_VERSION
+ '/remotevideos/remove'
94 return callback(new Error('Unkown pool request type.'))
100 requests
.makeMultipleRetryRequest(params
, pods
, callbackEachPodFinished
, callbackAllPodsFinished
)
102 function callbackEachPodFinished (err
, response
, body
, url
, pod
, callbackEachPodFinished
) {
103 if (err
|| (response
.statusCode
!== 200 && response
.statusCode
!== 201 && response
.statusCode
!== 204)) {
104 badPods
.push(pod
._id
)
105 logger
.error('Error sending secure request to %s pod.', url
, { error: err
|| new Error('Status code not 20x') })
107 goodPods
.push(pod
._id
)
110 return callbackEachPodFinished()
113 function callbackAllPodsFinished (err
) {
114 if (err
) return callback(err
)
116 updatePodsScore(goodPods
, badPods
)
122 function makeRequests () {
123 logger
.info('Making requests to friends.')
125 Requests
.list(function (err
, requests
) {
127 logger
.error('Cannot get the list of requests.', { err: err
})
131 if (requests
.length
=== 0) return
133 const requestsToMake
= {
144 async
.each(requests
, function (poolRequest
, callbackEach
) {
145 if (poolRequest
.type
=== 'add') {
146 requestsToMake
.add
.requests
.push(poolRequest
.request
)
147 requestsToMake
.add
.ids
.push(poolRequest
._id
)
148 } else if (poolRequest
.type
=== 'remove') {
149 requestsToMake
.remove
.requests
.push(poolRequest
.request
)
150 requestsToMake
.remove
.ids
.push(poolRequest
._id
)
152 logger
.error('Unkown request type.', { request_type: poolRequest
.type
})
158 // Send the add requests
159 if (requestsToMake
.add
.requests
.length
!== 0) {
160 makeRequest('add', requestsToMake
.add
.requests
, function (err
) {
161 if (err
) logger
.error('Errors when sent add requests.', { error: err
})
163 Requests
.removeRequests(requestsToMake
.add
.ids
)
167 // Send the remove requests
168 if (requestsToMake
.remove
.requests
.length
!== 0) {
169 makeRequest('remove', requestsToMake
.remove
.requests
, function (err
) {
170 if (err
) logger
.error('Errors when sent remove pool requests.', { error: err
})
172 Requests
.removeRequests(requestsToMake
.remove
.ids
)
179 function removeBadPods () {
181 function findBadPods (callback
) {
182 Pods
.findBadPods(function (err
, pods
) {
184 logger
.error('Cannot find bad pods.', { error: err
})
188 return callback(null, pods
)
192 function listVideosOfTheseBadPods (pods
, callback
) {
193 if (pods
.length
=== 0) return callback(null)
195 const urls
= map(pods
, 'url')
196 const ids
= map(pods
, '_id')
198 Videos
.listFromUrls(urls
, function (err
, videosList
) {
200 logger
.error('Cannot list videos urls.', { error: err
, urls: urls
})
201 return callback(null, ids
, [])
204 return callback(null, ids
, videosList
)
208 function removeVideosOfTheseBadPods (podIds
, videosList
, callback
) {
209 // We don't have to remove pods, skip
210 if (typeof podIds
=== 'function') return podIds(null)
212 // Remove the remote videos
213 videos
.removeRemoteVideos(videosList
, function (err
) {
214 if (err
) logger
.error('Cannot remove remote videos.', { error: err
})
216 return callback(null, podIds
)
220 function removeBadPodsFromDB (podIds
, callback
) {
221 // We don't have to remove pods, skip
222 if (typeof podIds
=== 'function') return podIds(null)
224 Pods
.removeAllByIds(podIds
, callback
)
226 ], function (err
, removeResult
) {
228 logger
.error('Cannot remove bad pods.', { error: err
})
229 } else if (removeResult
) {
230 const podsRemoved
= removeResult
.result
.n
231 logger
.info('Removed %d pods.', podsRemoved
)
233 logger
.info('No need to remove bad pods.')
238 function updatePodsScore (goodPods
, badPods
) {
239 logger
.info('Updating %d good pods and %d bad pods scores.', goodPods
.length
, badPods
.length
)
241 Pods
.incrementScores(goodPods
, constants
.PODS_SCORE
.BONUS
, function (err
) {
242 if (err
) logger
.error('Cannot increment scores of good pods.')
245 Pods
.incrementScores(badPods
, constants
.PODS_SCORE
.MALUS
, function (err
) {
246 if (err
) logger
.error('Cannot increment scores of bad pods.')