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 {
12 constructor (options) {
13 this.lastRequestTimestamp = 0
18 logger.info('Requests scheduler activated.')
19 this.lastRequestTimestamp = Date.now()
21 this.timer = setInterval(() => {
22 this.lastRequestTimestamp = Date.now()
24 }, constants.REQUESTS_INTERVAL)
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 // ---------------------------------------------------------------------------
46 // Make a requests to friends of a certain type
47 makeRequest (toPod, requestEndpoint, requestsToMake, callback) {
48 if (!callback) callback = function () {}
52 sign: true, // Prove our identity
54 path: '/api/' + constants.API_VERSION + '/remote/' + requestEndpoint,
55 data: requestsToMake // Requests we need to make
58 // Make multiple retry requests to all of pods
59 // The function fire some useful callbacks
60 requests.makeSecureRequest(params, (err, res) => {
61 if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
62 err = err ? err.message : 'Status code not 20x : ' + res.statusCode
63 logger.error('Error sending secure request to %s pod.', toPod.host, { error: err })
65 return callback(false)
72 // Make all the requests of the scheduler
74 this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod, (err, requests) => {
76 logger.error('Cannot get the list of "%s".', this.description, { err: err })
80 // If there are no requests, abort
81 if (requests.length === 0) {
82 logger.info('No "%s" to make.', this.description)
86 // We want to group requests by destinations pod and endpoint
87 const requestsToMakeGrouped = this.buildRequestObjects(requests)
89 logger.info('Making "%s" to friends.', this.description)
94 eachLimit(Object.keys(requestsToMakeGrouped), constants.REQUESTS_IN_PARALLEL, (hashKey, callbackEach) => {
95 const requestToMake = requestsToMakeGrouped[hashKey]
96 const toPod = requestToMake.toPod
98 this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, (success) => {
99 if (success === false) {
100 badPods.push(requestToMake.toPod.id)
101 return callbackEach()
104 logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids })
105 goodPods.push(requestToMake.toPod.id)
107 // Remove the pod id of these request ids
108 this.getRequestToPodModel().removeByRequestIdsAndPod(requestToMake.ids, requestToMake.toPod.id, callbackEach)
110 this.afterRequestHook()
113 // All the requests were made, we update the pods score
114 db.Pod.updatePodsScore(goodPods, badPods)
116 this.afterRequestsHook()
122 this.getRequestModel().removeAll(callback)
125 afterRequestHook () {
126 // Nothing to do, let children reimplement it
129 afterRequestsHook () {
130 // Nothing to do, let children reimplement it