2017-05-22 14:58:25 -04:00
|
|
|
import * as eachLimit from 'async/eachLimit'
|
2017-02-21 15:35:59 -05:00
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-05-15 16:22:03 -04:00
|
|
|
import { logger, makeSecureRequest } from '../../helpers'
|
2017-06-10 16:15:25 -04:00
|
|
|
import { PodInstance } from '../../models'
|
2017-05-15 16:22:03 -04:00
|
|
|
import {
|
|
|
|
API_VERSION,
|
|
|
|
REQUESTS_IN_PARALLEL,
|
|
|
|
REQUESTS_INTERVAL
|
|
|
|
} from '../../initializers'
|
|
|
|
|
|
|
|
abstract class BaseRequestScheduler {
|
2017-06-10 16:15:25 -04:00
|
|
|
requestInterval: number
|
|
|
|
limitPods: number
|
|
|
|
limitPerPod: number
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
protected lastRequestTimestamp: number
|
|
|
|
protected timer: NodeJS.Timer
|
|
|
|
protected description: string
|
|
|
|
|
|
|
|
constructor () {
|
2017-02-21 15:35:59 -05:00
|
|
|
this.lastRequestTimestamp = 0
|
|
|
|
this.timer = null
|
2017-05-15 16:22:03 -04:00
|
|
|
this.requestInterval = REQUESTS_INTERVAL
|
2017-02-21 15:35:59 -05:00
|
|
|
}
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
abstract getRequestModel ()
|
|
|
|
abstract getRequestToPodModel ()
|
|
|
|
abstract buildRequestObjects (requests: any)
|
|
|
|
|
2017-02-21 15:35:59 -05:00
|
|
|
activate () {
|
|
|
|
logger.info('Requests scheduler activated.')
|
|
|
|
this.lastRequestTimestamp = Date.now()
|
|
|
|
|
|
|
|
this.timer = setInterval(() => {
|
|
|
|
this.lastRequestTimestamp = Date.now()
|
|
|
|
this.makeRequests()
|
2017-02-27 15:56:55 -05:00
|
|
|
}, this.requestInterval)
|
2017-02-21 15:35:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
deactivate () {
|
|
|
|
logger.info('Requests scheduler deactivated.')
|
|
|
|
clearInterval(this.timer)
|
|
|
|
this.timer = null
|
|
|
|
}
|
|
|
|
|
|
|
|
forceSend () {
|
|
|
|
logger.info('Force requests scheduler sending.')
|
|
|
|
this.makeRequests()
|
|
|
|
}
|
|
|
|
|
|
|
|
remainingMilliSeconds () {
|
|
|
|
if (this.timer === null) return -1
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp)
|
2017-02-21 15:35:59 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
remainingRequestsCount (callback: (err: Error, total: number) => void) {
|
2017-02-27 15:56:55 -05:00
|
|
|
return this.getRequestModel().countTotalRequests(callback)
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
flush (callback: (err: Error) => void) {
|
2017-05-15 16:22:03 -04:00
|
|
|
this.getRequestModel().removeAll(callback)
|
|
|
|
}
|
|
|
|
|
2017-02-21 15:35:59 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Make a requests to friends of a certain type
|
2017-06-10 16:15:25 -04:00
|
|
|
protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: Object, callback) {
|
2017-05-15 16:22:03 -04:00
|
|
|
if (!callback) callback = function () { /* empty */ }
|
2017-02-21 15:35:59 -05:00
|
|
|
|
|
|
|
const params = {
|
|
|
|
toPod: toPod,
|
|
|
|
sign: true, // Prove our identity
|
2017-06-10 16:15:25 -04:00
|
|
|
method: 'POST' as 'POST',
|
2017-05-15 16:22:03 -04:00
|
|
|
path: '/api/' + API_VERSION + '/remote/' + requestEndpoint,
|
2017-02-21 15:35:59 -05:00
|
|
|
data: requestsToMake // Requests we need to make
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make multiple retry requests to all of pods
|
|
|
|
// The function fire some useful callbacks
|
2017-05-15 16:22:03 -04:00
|
|
|
makeSecureRequest(params, (err, res) => {
|
2017-02-21 15:35:59 -05:00
|
|
|
if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
|
|
|
|
err = err ? err.message : 'Status code not 20x : ' + res.statusCode
|
|
|
|
logger.error('Error sending secure request to %s pod.', toPod.host, { error: err })
|
|
|
|
|
2017-04-16 08:25:37 -04:00
|
|
|
return callback(err)
|
2017-02-21 15:35:59 -05:00
|
|
|
}
|
|
|
|
|
2017-04-16 08:25:37 -04:00
|
|
|
return callback(null)
|
2017-02-21 15:35:59 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make all the requests of the scheduler
|
2017-05-15 16:22:03 -04:00
|
|
|
protected makeRequests () {
|
2017-02-21 15:35:59 -05:00
|
|
|
this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod, (err, requests) => {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get the list of "%s".', this.description, { err: err })
|
|
|
|
return // Abort
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no requests, abort
|
|
|
|
if (requests.length === 0) {
|
|
|
|
logger.info('No "%s" to make.', this.description)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to group requests by destinations pod and endpoint
|
|
|
|
const requestsToMakeGrouped = this.buildRequestObjects(requests)
|
|
|
|
|
|
|
|
logger.info('Making "%s" to friends.', this.description)
|
|
|
|
|
|
|
|
const goodPods = []
|
|
|
|
const badPods = []
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
eachLimit(Object.keys(requestsToMakeGrouped), REQUESTS_IN_PARALLEL, (hashKey, callbackEach) => {
|
2017-02-21 15:35:59 -05:00
|
|
|
const requestToMake = requestsToMakeGrouped[hashKey]
|
|
|
|
const toPod = requestToMake.toPod
|
|
|
|
|
2017-04-16 08:25:37 -04:00
|
|
|
this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, (err) => {
|
|
|
|
if (err) {
|
2017-02-21 15:35:59 -05:00
|
|
|
badPods.push(requestToMake.toPod.id)
|
|
|
|
return callbackEach()
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids })
|
|
|
|
goodPods.push(requestToMake.toPod.id)
|
|
|
|
|
|
|
|
// Remove the pod id of these request ids
|
|
|
|
this.getRequestToPodModel().removeByRequestIdsAndPod(requestToMake.ids, requestToMake.toPod.id, callbackEach)
|
|
|
|
|
|
|
|
this.afterRequestHook()
|
|
|
|
})
|
|
|
|
}, () => {
|
|
|
|
// All the requests were made, we update the pods score
|
|
|
|
db.Pod.updatePodsScore(goodPods, badPods)
|
|
|
|
|
|
|
|
this.afterRequestsHook()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
protected afterRequestHook () {
|
2017-02-21 15:35:59 -05:00
|
|
|
// Nothing to do, let children reimplement it
|
|
|
|
}
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
protected afterRequestsHook () {
|
2017-02-21 15:35:59 -05:00
|
|
|
// Nothing to do, let children reimplement it
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 16:22:03 -04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
BaseRequestScheduler
|
|
|
|
}
|