1 import { isEmpty } from 'lodash'
2 import * as Promise from 'bluebird'
4 import { database as db } from '../../initializers/database'
5 import { logger, makeSecureRequest } from '../../helpers'
6 import { AbstractRequestClass, AbstractRequestToPodClass, PodInstance } from '../../models'
11 } from '../../initializers'
13 abstract class AbstractRequestScheduler <T> {
14 requestInterval: number
18 protected lastRequestTimestamp: number
19 protected timer: NodeJS.Timer
20 protected description: string
23 this.lastRequestTimestamp = 0
25 this.requestInterval = REQUESTS_INTERVAL
28 abstract getRequestModel (): AbstractRequestClass<T>
29 abstract getRequestToPodModel (): AbstractRequestToPodClass
30 abstract buildRequestObjects (requestsGrouped: T): {}
33 logger.info('Requests scheduler activated.')
34 this.lastRequestTimestamp = Date.now()
36 this.timer = setInterval(() => {
37 this.lastRequestTimestamp = Date.now()
39 }, this.requestInterval)
43 logger.info('Requests scheduler deactivated.')
44 clearInterval(this.timer)
49 logger.info('Force requests scheduler sending.')
53 remainingMilliSeconds () {
54 if (this.timer === null) return -1
56 return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp)
59 remainingRequestsCount () {
60 return this.getRequestModel().countTotalRequests()
64 return this.getRequestModel().removeAll()
67 // ---------------------------------------------------------------------------
69 // Make a requests to friends of a certain type
70 protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: Object) {
73 sign: true, // Prove our identity
74 method: 'POST' as 'POST',
75 path: '/api/' + API_VERSION + '/remote/' + requestEndpoint,
76 data: requestsToMake // Requests we need to make
79 // Make multiple retry requests to all of pods
80 // The function fire some useful callbacks
81 return makeSecureRequest(params)
82 .then(({ response, body }) => {
83 if (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204) {
84 throw new Error('Status code not 20x : ' + response.statusCode)
88 logger.error('Error sending secure request to %s pod.', toPod.host, { error: err })
94 // Make all the requests of the scheduler
95 protected makeRequests () {
96 return this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod)
97 .then((requestsGrouped: T) => {
98 // We want to group requests by destinations pod and endpoint
99 const requestsToMake = this.buildRequestObjects(requestsGrouped)
101 // If there are no requests, abort
102 if (isEmpty(requestsToMake) === true) {
103 logger.info('No "%s" to make.', this.description)
104 return { goodPods: [], badPods: [] }
107 logger.info('Making "%s" to friends.', this.description)
112 return Promise.map(Object.keys(requestsToMake), hashKey => {
113 const requestToMake = requestsToMake[hashKey]
114 const toPod: PodInstance = requestToMake.toPod
116 return this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas)
118 logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids })
119 goodPods.push(requestToMake.toPod.id)
121 this.afterRequestHook()
123 // Remove the pod id of these request ids
124 return this.getRequestToPodModel().removeByRequestIdsAndPod(requestToMake.ids, requestToMake.toPod.id)
127 badPods.push(requestToMake.toPod.id)
128 logger.info('Cannot make request to %s.', toPod.host, { error: err })
130 }, { concurrency: REQUESTS_IN_PARALLEL }).then(() => ({ goodPods, badPods }))
132 .then(({ goodPods, badPods }) => {
133 this.afterRequestsHook()
135 // All the requests were made, we update the pods score
136 return db.Pod.updatePodsScore(goodPods, badPods)
138 .catch(err => logger.error('Cannot get the list of "%s".', this.description, { error: err.stack }))
141 protected afterRequestHook () {
142 // Nothing to do, let children reimplement it
145 protected afterRequestsHook () {
146 // Nothing to do, let children reimplement it
150 // ---------------------------------------------------------------------------
153 AbstractRequestScheduler