1 import * as eachLimit from 'async/eachLimit'
3 import { database as db } from '../../initializers/database'
4 import { logger, makeSecureRequest } from '../../helpers'
5 import { PodInstance } from '../../models'
10 } from '../../initializers'
12 abstract class AbstractRequestScheduler {
13 requestInterval: number
17 protected lastRequestTimestamp: number
18 protected timer: NodeJS.Timer
19 protected description: string
22 this.lastRequestTimestamp = 0
24 this.requestInterval = REQUESTS_INTERVAL
27 abstract getRequestModel ()
28 abstract getRequestToPodModel ()
29 abstract buildRequestObjects (requests: any)
32 logger.info('Requests scheduler activated.')
33 this.lastRequestTimestamp = Date.now()
35 this.timer = setInterval(() => {
36 this.lastRequestTimestamp = Date.now()
38 }, this.requestInterval)
42 logger.info('Requests scheduler deactivated.')
43 clearInterval(this.timer)
48 logger.info('Force requests scheduler sending.')
52 remainingMilliSeconds () {
53 if (this.timer === null) return -1
55 return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp)
58 remainingRequestsCount (callback: (err: Error, total: number) => void) {
59 return this.getRequestModel().countTotalRequests(callback)
62 flush (callback: (err: Error) => void) {
63 this.getRequestModel().removeAll(callback)
66 // ---------------------------------------------------------------------------
68 // Make a requests to friends of a certain type
69 protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: Object, callback) {
70 if (!callback) callback = function () { /* empty */ }
74 sign: true, // Prove our identity
75 method: 'POST' as 'POST',
76 path: '/api/' + API_VERSION + '/remote/' + requestEndpoint,
77 data: requestsToMake // Requests we need to make
80 // Make multiple retry requests to all of pods
81 // The function fire some useful callbacks
82 makeSecureRequest(params, (err, res) => {
83 if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
84 err = err ? err.message : 'Status code not 20x : ' + res.statusCode
85 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 this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod, (err, requests) => {
98 logger.error('Cannot get the list of "%s".', this.description, { err: err })
102 // If there are no requests, abort
103 if (requests.length === 0) {
104 logger.info('No "%s" to make.', this.description)
108 // We want to group requests by destinations pod and endpoint
109 const requestsToMakeGrouped = this.buildRequestObjects(requests)
111 logger.info('Making "%s" to friends.', this.description)
116 eachLimit(Object.keys(requestsToMakeGrouped), REQUESTS_IN_PARALLEL, (hashKey, callbackEach) => {
117 const requestToMake = requestsToMakeGrouped[hashKey]
118 const toPod = requestToMake.toPod
120 this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, (err) => {
122 badPods.push(requestToMake.toPod.id)
123 return callbackEach()
126 logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids })
127 goodPods.push(requestToMake.toPod.id)
129 // Remove the pod id of these request ids
130 this.getRequestToPodModel().removeByRequestIdsAndPod(requestToMake.ids, requestToMake.toPod.id, callbackEach)
132 this.afterRequestHook()
135 // All the requests were made, we update the pods score
136 db.Pod.updatePodsScore(goodPods, badPods)
138 this.afterRequestsHook()
143 protected afterRequestHook () {
144 // Nothing to do, let children reimplement it
147 protected afterRequestsHook () {
148 // Nothing to do, let children reimplement it
152 // ---------------------------------------------------------------------------
155 AbstractRequestScheduler