2017-07-05 06:26:25 -05:00
|
|
|
import { isEmpty } from 'lodash'
|
|
|
|
import * as Promise from 'bluebird'
|
2017-02-21 14:35:59 -06:00
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-05-15 15:22:03 -05:00
|
|
|
import { logger, makeSecureRequest } from '../../helpers'
|
2017-07-05 06:26:25 -05:00
|
|
|
import { AbstractRequestClass, AbstractRequestToPodClass, PodInstance } from '../../models'
|
2017-05-15 15:22:03 -05:00
|
|
|
import {
|
|
|
|
API_VERSION,
|
|
|
|
REQUESTS_IN_PARALLEL,
|
|
|
|
REQUESTS_INTERVAL
|
|
|
|
} from '../../initializers'
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
abstract class AbstractRequestScheduler <T> {
|
2017-06-10 15:15:25 -05:00
|
|
|
requestInterval: number
|
|
|
|
limitPods: number
|
|
|
|
limitPerPod: number
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
protected lastRequestTimestamp: number
|
|
|
|
protected timer: NodeJS.Timer
|
|
|
|
protected description: string
|
|
|
|
|
|
|
|
constructor () {
|
2017-02-21 14:35:59 -06:00
|
|
|
this.lastRequestTimestamp = 0
|
|
|
|
this.timer = null
|
2017-05-15 15:22:03 -05:00
|
|
|
this.requestInterval = REQUESTS_INTERVAL
|
2017-02-21 14:35:59 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
abstract getRequestModel (): AbstractRequestClass<T>
|
|
|
|
abstract getRequestToPodModel (): AbstractRequestToPodClass
|
|
|
|
abstract buildRequestObjects (requestsGrouped: T): {}
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-02-21 14:35:59 -06:00
|
|
|
activate () {
|
|
|
|
logger.info('Requests scheduler activated.')
|
|
|
|
this.lastRequestTimestamp = Date.now()
|
|
|
|
|
|
|
|
this.timer = setInterval(() => {
|
|
|
|
this.lastRequestTimestamp = Date.now()
|
|
|
|
this.makeRequests()
|
2017-02-27 14:56:55 -06:00
|
|
|
}, this.requestInterval)
|
2017-02-21 14:35:59 -06: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 15:22:03 -05:00
|
|
|
return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp)
|
2017-02-21 14:35:59 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
remainingRequestsCount () {
|
|
|
|
return this.getRequestModel().countTotalRequests()
|
2017-02-27 14:56:55 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
flush () {
|
|
|
|
return this.getRequestModel().removeAll()
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2017-02-21 14:35:59 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Make a requests to friends of a certain type
|
2017-07-05 06:26:25 -05:00
|
|
|
protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: Object) {
|
2017-02-21 14:35:59 -06:00
|
|
|
const params = {
|
|
|
|
toPod: toPod,
|
2017-06-10 15:15:25 -05:00
|
|
|
method: 'POST' as 'POST',
|
2017-05-15 15:22:03 -05:00
|
|
|
path: '/api/' + API_VERSION + '/remote/' + requestEndpoint,
|
2017-02-21 14:35:59 -06:00
|
|
|
data: requestsToMake // Requests we need to make
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make multiple retry requests to all of pods
|
|
|
|
// The function fire some useful callbacks
|
2017-07-05 06:26:25 -05:00
|
|
|
return makeSecureRequest(params)
|
|
|
|
.then(({ response, body }) => {
|
|
|
|
if (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204) {
|
|
|
|
throw new Error('Status code not 20x : ' + response.statusCode)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2017-02-21 14:35:59 -06:00
|
|
|
logger.error('Error sending secure request to %s pod.', toPod.host, { error: err })
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
throw err
|
|
|
|
})
|
2017-02-21 14:35:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make all the requests of the scheduler
|
2017-05-15 15:22:03 -05:00
|
|
|
protected makeRequests () {
|
2017-07-05 06:26:25 -05:00
|
|
|
return this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod)
|
|
|
|
.then((requestsGrouped: T) => {
|
|
|
|
// We want to group requests by destinations pod and endpoint
|
|
|
|
const requestsToMake = this.buildRequestObjects(requestsGrouped)
|
|
|
|
|
|
|
|
// If there are no requests, abort
|
|
|
|
if (isEmpty(requestsToMake) === true) {
|
|
|
|
logger.info('No "%s" to make.', this.description)
|
|
|
|
return { goodPods: [], badPods: [] }
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Making "%s" to friends.', this.description)
|
|
|
|
|
|
|
|
const goodPods = []
|
|
|
|
const badPods = []
|
|
|
|
|
|
|
|
return Promise.map(Object.keys(requestsToMake), hashKey => {
|
|
|
|
const requestToMake = requestsToMake[hashKey]
|
|
|
|
const toPod: PodInstance = requestToMake.toPod
|
|
|
|
|
|
|
|
return this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas)
|
|
|
|
.then(() => {
|
|
|
|
logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids })
|
|
|
|
goodPods.push(requestToMake.toPod.id)
|
|
|
|
|
|
|
|
this.afterRequestHook()
|
|
|
|
|
|
|
|
// Remove the pod id of these request ids
|
|
|
|
return this.getRequestToPodModel().removeByRequestIdsAndPod(requestToMake.ids, requestToMake.toPod.id)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
badPods.push(requestToMake.toPod.id)
|
|
|
|
logger.info('Cannot make request to %s.', toPod.host, { error: err })
|
|
|
|
})
|
|
|
|
}, { concurrency: REQUESTS_IN_PARALLEL }).then(() => ({ goodPods, badPods }))
|
|
|
|
})
|
|
|
|
.then(({ goodPods, badPods }) => {
|
|
|
|
this.afterRequestsHook()
|
2017-02-21 14:35:59 -06:00
|
|
|
|
|
|
|
// All the requests were made, we update the pods score
|
2017-07-05 06:26:25 -05:00
|
|
|
return db.Pod.updatePodsScore(goodPods, badPods)
|
2017-02-21 14:35:59 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.catch(err => logger.error('Cannot get the list of "%s".', this.description, { error: err.stack }))
|
2017-02-21 14:35:59 -06:00
|
|
|
}
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
protected afterRequestHook () {
|
2017-02-21 14:35:59 -06:00
|
|
|
// Nothing to do, let children reimplement it
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
protected afterRequestsHook () {
|
2017-02-21 14:35:59 -06:00
|
|
|
// Nothing to do, let children reimplement it
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2017-06-16 02:14:41 -05:00
|
|
|
AbstractRequestScheduler
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|