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