]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/base-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 BaseRequestScheduler
{
11 constructor (options
) {
12 this.lastRequestTimestamp
= 0
14 this.requestInterval
= constants
.REQUESTS_INTERVAL
18 logger
.info('Requests scheduler activated.')
19 this.lastRequestTimestamp
= Date
.now()
21 this.timer
= setInterval(() => {
22 this.lastRequestTimestamp
= Date
.now()
24 }, this.requestInterval
)
28 logger
.info('Requests scheduler deactivated.')
29 clearInterval(this.timer
)
34 logger
.info('Force requests scheduler sending.')
38 remainingMilliSeconds () {
39 if (this.timer
=== null) return -1
41 return constants
.REQUESTS_INTERVAL
- (Date
.now() - this.lastRequestTimestamp
)
44 remainingRequestsCount (callback
) {
45 return this.getRequestModel().countTotalRequests(callback
)
48 // ---------------------------------------------------------------------------
50 // Make a requests to friends of a certain type
51 makeRequest (toPod
, requestEndpoint
, requestsToMake
, callback
) {
52 if (!callback
) callback = function () {}
56 sign: true, // Prove our identity
58 path: '/api/' + constants
.API_VERSION
+ '/remote/' + requestEndpoint
,
59 data: requestsToMake
// Requests we need to make
62 // Make multiple retry requests to all of pods
63 // The function fire some useful callbacks
64 requests
.makeSecureRequest(params
, (err
, res
) => {
65 if (err
|| (res
.statusCode
!== 200 && res
.statusCode
!== 201 && res
.statusCode
!== 204)) {
66 err
= err
? err
.message : 'Status code not 20x : ' + res
.statusCode
67 logger
.error('Error sending secure request to %s pod.', toPod
.host
, { error: err
})
69 return callback(false)
76 // Make all the requests of the scheduler
78 this.getRequestModel().listWithLimitAndRandom(this.limitPods
, this.limitPerPod
, (err
, requests
) => {
80 logger
.error('Cannot get the list of "%s".', this.description
, { err: err
})
84 // If there are no requests, abort
85 if (requests
.length
=== 0) {
86 logger
.info('No "%s" to make.', this.description
)
90 // We want to group requests by destinations pod and endpoint
91 const requestsToMakeGrouped
= this.buildRequestObjects(requests
)
93 logger
.info('Making "%s" to friends.', this.description
)
98 eachLimit(Object
.keys(requestsToMakeGrouped
), constants
.REQUESTS_IN_PARALLEL
, (hashKey
, callbackEach
) => {
99 const requestToMake
= requestsToMakeGrouped
[hashKey
]
100 const toPod
= requestToMake
.toPod
102 this.makeRequest(toPod
, requestToMake
.endpoint
, requestToMake
.datas
, (success
) => {
103 if (success
=== false) {
104 badPods
.push(requestToMake
.toPod
.id
)
105 return callbackEach()
108 logger
.debug('Removing requests for pod %s.', requestToMake
.toPod
.id
, { requestsIds: requestToMake
.ids
})
109 goodPods
.push(requestToMake
.toPod
.id
)
111 // Remove the pod id of these request ids
112 this.getRequestToPodModel().removeByRequestIdsAndPod(requestToMake
.ids
, requestToMake
.toPod
.id
, callbackEach
)
114 this.afterRequestHook()
117 // All the requests were made, we update the pods score
118 db
.Pod
.updatePodsScore(goodPods
, badPods
)
120 this.afterRequestsHook()
126 this.getRequestModel().removeAll(callback
)
129 afterRequestHook () {
130 // Nothing to do, let children reimplement it
133 afterRequestsHook () {
134 // Nothing to do, let children reimplement it