]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/request-scheduler.js
3 const eachLimit
= require('async/eachLimit')
5 const constants
= require('../initializers/constants')
6 const db
= require('../initializers/database')
7 const logger
= require('../helpers/logger')
8 const requests
= require('../helpers/requests')
10 module
.exports
= class RequestScheduler
{
15 this.lastRequestTimestamp
= 0
20 logger
.info('Requests scheduler activated.')
21 this.lastRequestTimestamp
= Date
.now()
23 this.timer
= setInterval(() => {
24 this.lastRequestTimestamp
= Date
.now()
26 }, constants
.REQUESTS_INTERVAL
)
30 logger
.info('Requests scheduler deactivated.')
31 clearInterval(this.timer
)
36 logger
.info('Force requests scheduler sending.')
40 remainingMilliSeconds () {
41 if (this.timer
=== null) return -1
43 return constants
.REQUESTS_INTERVAL
- (Date
.now() - this.lastRequestTimestamp
)
46 // { type, endpoint, data, toIds, transaction }
47 createRequest (options
, callback
) {
48 const type
= options
.type
49 const endpoint
= options
.endpoint
50 const data
= options
.data
51 const toIds
= options
.toIds
52 const transaction
= options
.transaction
56 // If there are no destination pods abort
57 if (toIds
.length
=== 0) return callback(null)
59 toIds
.forEach(toPod
=> {
60 pods
.push(db
.Pod
.build({ id: toPod
}))
71 const dbRequestOptions
= {
75 return db
.Request
.create(createQuery
, dbRequestOptions
).asCallback((err
, request
) => {
76 if (err
) return callback(err
)
78 return request
.setPods(pods
, dbRequestOptions
).asCallback(callback
)
82 // ---------------------------------------------------------------------------
84 // Make all the requests of the scheduler
86 // We limit the size of the requests
87 // We don't want to stuck with the same failing requests so we get a random list
88 db
.Request
.listWithLimitAndRandom(constants
.REQUESTS_LIMIT_PODS
, constants
.REQUESTS_LIMIT_PER_POD
, (err
, requests
) => {
90 logger
.error('Cannot get the list of requests.', { err: err
})
94 // If there are no requests, abort
95 if (requests
.length
=== 0) {
96 logger
.info('No requests to make.')
100 // We want to group requests by destinations pod and endpoint
101 const requestsToMakeGrouped
= this.buildRequestObjects(requests
)
103 logger
.info('Making requests to friends.')
108 eachLimit(Object
.keys(requestsToMakeGrouped
), constants
.REQUESTS_IN_PARALLEL
, (hashKey
, callbackEach
) => {
109 const requestToMake
= requestsToMakeGrouped
[hashKey
]
110 const toPod
= requestToMake
.toPod
112 // Maybe the pod is not our friend anymore so simply remove it
114 const requestIdsToDelete
= requestToMake
.ids
116 logger
.info('Removing %d requests of unexisting pod %s.', requestIdsToDelete
.length
, requestToMake
.toPod
.id
)
117 return db
.RequestToPod
.removePodOf(requestIdsToDelete
, requestToMake
.toPod
.id
, callbackEach
)
120 this.makeRequest(toPod
, requestToMake
.endpoint
, requestToMake
.datas
, (success
) => {
121 if (success
=== false) {
122 badPods
.push(requestToMake
.toPod
.id
)
123 return callbackEach()
126 logger
.debug('Removing requests for pod %s.', requestToMake
.toPod
.id
, { requestsIds: requestToMake
.ids
})
127 goodPods
.push(requestToMake
.toPod
.id
)
129 // Remove the pod id of these request ids
130 db
.RequestToPod
.removePodOf(requestToMake
.ids
, requestToMake
.toPod
.id
, callbackEach
)
133 // All the requests were made, we update the pods score
134 db
.Request
.updatePodsScore(goodPods
, badPods
)
135 // Flush requests with no pod
136 db
.Request
.removeWithEmptyTo(err
=> {
137 if (err
) logger
.error('Error when removing requests with no pods.', { error: err
})
143 // Make a requests to friends of a certain type
144 makeRequest (toPod
, requestEndpoint
, requestsToMake
, callback
) {
145 if (!callback
) callback = function () {}
149 sign: true, // Prove our identity
151 path: '/api/' + constants
.API_VERSION
+ '/remote/' + requestEndpoint
,
152 data: requestsToMake
// Requests we need to make
155 // Make multiple retry requests to all of pods
156 // The function fire some useful callbacks
157 requests
.makeSecureRequest(params
, (err
, res
) => {
158 if (err
|| (res
.statusCode
!== 200 && res
.statusCode
!== 201 && res
.statusCode
!== 204)) {
159 err
= err
? err
.message : 'Status code not 20x : ' + res
.statusCode
160 logger
.error('Error sending secure request to %s pod.', toPod
.host
, { error: err
})
162 return callback(false)
165 return callback(true)
169 buildRequestObjects (requests
) {
170 const requestsToMakeGrouped
= {}
172 Object
.keys(requests
).forEach(toPodId
=> {
173 requests
[toPodId
].forEach(data
=> {
174 const request
= data
.request
176 const hashKey
= toPodId
+ request
.endpoint
178 if (!requestsToMakeGrouped
[hashKey
]) {
179 requestsToMakeGrouped
[hashKey
] = {
181 endpoint: request
.endpoint
,
182 ids: [], // request ids, to delete them from the DB in the future
183 datas: [] // requests data,
187 requestsToMakeGrouped
[hashKey
].ids
.push(request
.id
)
188 requestsToMakeGrouped
[hashKey
].datas
.push(request
.request
)
192 return requestsToMakeGrouped
196 db
.Request
.removeAll(err
=> {
197 if (err
) logger
.error('Cannot flush the requests.', { error: err
})