]>
Commit | Line | Data |
---|---|---|
6fcd19ba C |
1 | import { isEmpty } from 'lodash' |
2 | import * as Promise from 'bluebird' | |
9e167724 | 3 | |
e02643f3 | 4 | import { database as db } from '../../initializers/database' |
65fcc311 | 5 | import { logger, makeSecureRequest } from '../../helpers' |
6fcd19ba | 6 | import { AbstractRequestClass, AbstractRequestToPodClass, PodInstance } from '../../models' |
65fcc311 C |
7 | import { |
8 | API_VERSION, | |
9 | REQUESTS_IN_PARALLEL, | |
10 | REQUESTS_INTERVAL | |
11 | } from '../../initializers' | |
12 | ||
6fcd19ba | 13 | abstract class AbstractRequestScheduler <T> { |
69818c93 C |
14 | requestInterval: number |
15 | limitPods: number | |
16 | limitPerPod: number | |
17 | ||
65fcc311 C |
18 | protected lastRequestTimestamp: number |
19 | protected timer: NodeJS.Timer | |
65fcc311 C |
20 | protected description: string |
21 | ||
22 | constructor () { | |
9e167724 C |
23 | this.lastRequestTimestamp = 0 |
24 | this.timer = null | |
65fcc311 | 25 | this.requestInterval = REQUESTS_INTERVAL |
9e167724 C |
26 | } |
27 | ||
6fcd19ba C |
28 | abstract getRequestModel (): AbstractRequestClass<T> |
29 | abstract getRequestToPodModel (): AbstractRequestToPodClass | |
30 | abstract buildRequestObjects (requestsGrouped: T): {} | |
65fcc311 | 31 | |
9e167724 C |
32 | activate () { |
33 | logger.info('Requests scheduler activated.') | |
34 | this.lastRequestTimestamp = Date.now() | |
35 | ||
36 | this.timer = setInterval(() => { | |
37 | this.lastRequestTimestamp = Date.now() | |
38 | this.makeRequests() | |
99fdec46 | 39 | }, this.requestInterval) |
9e167724 C |
40 | } |
41 | ||
42 | deactivate () { | |
43 | logger.info('Requests scheduler deactivated.') | |
44 | clearInterval(this.timer) | |
45 | this.timer = null | |
46 | } | |
47 | ||
48 | forceSend () { | |
49 | logger.info('Force requests scheduler sending.') | |
50 | this.makeRequests() | |
51 | } | |
52 | ||
53 | remainingMilliSeconds () { | |
54 | if (this.timer === null) return -1 | |
55 | ||
65fcc311 | 56 | return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp) |
9e167724 C |
57 | } |
58 | ||
6fcd19ba C |
59 | remainingRequestsCount () { |
60 | return this.getRequestModel().countTotalRequests() | |
99fdec46 C |
61 | } |
62 | ||
6fcd19ba C |
63 | flush () { |
64 | return this.getRequestModel().removeAll() | |
65fcc311 C |
65 | } |
66 | ||
9e167724 C |
67 | // --------------------------------------------------------------------------- |
68 | ||
69 | // Make a requests to friends of a certain type | |
6fcd19ba | 70 | protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: Object) { |
9e167724 C |
71 | const params = { |
72 | toPod: toPod, | |
69818c93 | 73 | method: 'POST' as 'POST', |
65fcc311 | 74 | path: '/api/' + API_VERSION + '/remote/' + requestEndpoint, |
9e167724 C |
75 | data: requestsToMake // Requests we need to make |
76 | } | |
77 | ||
78 | // Make multiple retry requests to all of pods | |
79 | // The function fire some useful callbacks | |
6fcd19ba C |
80 | return makeSecureRequest(params) |
81 | .then(({ response, body }) => { | |
82 | if (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204) { | |
83 | throw new Error('Status code not 20x : ' + response.statusCode) | |
84 | } | |
85 | }) | |
86 | .catch(err => { | |
ad0997ad | 87 | logger.error('Error sending secure request to %s pod.', toPod.host, err) |
9e167724 | 88 | |
6fcd19ba C |
89 | throw err |
90 | }) | |
9e167724 C |
91 | } |
92 | ||
93 | // Make all the requests of the scheduler | |
65fcc311 | 94 | protected makeRequests () { |
6fcd19ba C |
95 | return this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod) |
96 | .then((requestsGrouped: T) => { | |
97 | // We want to group requests by destinations pod and endpoint | |
98 | const requestsToMake = this.buildRequestObjects(requestsGrouped) | |
99 | ||
100 | // If there are no requests, abort | |
101 | if (isEmpty(requestsToMake) === true) { | |
102 | logger.info('No "%s" to make.', this.description) | |
103 | return { goodPods: [], badPods: [] } | |
104 | } | |
105 | ||
106 | logger.info('Making "%s" to friends.', this.description) | |
107 | ||
108 | const goodPods = [] | |
109 | const badPods = [] | |
110 | ||
111 | return Promise.map(Object.keys(requestsToMake), hashKey => { | |
112 | const requestToMake = requestsToMake[hashKey] | |
113 | const toPod: PodInstance = requestToMake.toPod | |
114 | ||
115 | return this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas) | |
116 | .then(() => { | |
117 | logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids }) | |
118 | goodPods.push(requestToMake.toPod.id) | |
119 | ||
120 | this.afterRequestHook() | |
121 | ||
122 | // Remove the pod id of these request ids | |
123 | return this.getRequestToPodModel().removeByRequestIdsAndPod(requestToMake.ids, requestToMake.toPod.id) | |
124 | }) | |
125 | .catch(err => { | |
126 | badPods.push(requestToMake.toPod.id) | |
ad0997ad | 127 | logger.info('Cannot make request to %s.', toPod.host, err) |
6fcd19ba C |
128 | }) |
129 | }, { concurrency: REQUESTS_IN_PARALLEL }).then(() => ({ goodPods, badPods })) | |
130 | }) | |
131 | .then(({ goodPods, badPods }) => { | |
132 | this.afterRequestsHook() | |
9e167724 | 133 | |
9e167724 | 134 | // All the requests were made, we update the pods score |
6fcd19ba | 135 | return db.Pod.updatePodsScore(goodPods, badPods) |
9e167724 | 136 | }) |
6fcd19ba | 137 | .catch(err => logger.error('Cannot get the list of "%s".', this.description, { error: err.stack })) |
9e167724 C |
138 | } |
139 | ||
65fcc311 | 140 | protected afterRequestHook () { |
9e167724 C |
141 | // Nothing to do, let children reimplement it |
142 | } | |
143 | ||
65fcc311 | 144 | protected afterRequestsHook () { |
9e167724 C |
145 | // Nothing to do, let children reimplement it |
146 | } | |
147 | } | |
65fcc311 C |
148 | |
149 | // --------------------------------------------------------------------------- | |
150 | ||
151 | export { | |
15a30294 | 152 | AbstractRequestScheduler |
65fcc311 | 153 | } |