diff options
Diffstat (limited to 'server/lib/request/base-request-scheduler.ts')
-rw-r--r-- | server/lib/request/base-request-scheduler.ts | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/server/lib/request/base-request-scheduler.ts b/server/lib/request/base-request-scheduler.ts new file mode 100644 index 000000000..7fc88b5f1 --- /dev/null +++ b/server/lib/request/base-request-scheduler.ts | |||
@@ -0,0 +1,154 @@ | |||
1 | import { eachLimit } from 'async/eachLimit' | ||
2 | |||
3 | const db = require('../../initializers/database') | ||
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 () { | ||
20 | this.lastRequestTimestamp = 0 | ||
21 | this.timer = null | ||
22 | this.requestInterval = REQUESTS_INTERVAL | ||
23 | } | ||
24 | |||
25 | abstract getRequestModel () | ||
26 | abstract getRequestToPodModel () | ||
27 | abstract buildRequestObjects (requests: any) | ||
28 | |||
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() | ||
36 | }, this.requestInterval) | ||
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 | |||
53 | return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp) | ||
54 | } | ||
55 | |||
56 | remainingRequestsCount (callback) { | ||
57 | return this.getRequestModel().countTotalRequests(callback) | ||
58 | } | ||
59 | |||
60 | flush (callback) { | ||
61 | this.getRequestModel().removeAll(callback) | ||
62 | } | ||
63 | |||
64 | // --------------------------------------------------------------------------- | ||
65 | |||
66 | // Make a requests to friends of a certain type | ||
67 | protected makeRequest (toPod, requestEndpoint, requestsToMake, callback) { | ||
68 | if (!callback) callback = function () { /* empty */ } | ||
69 | |||
70 | const params = { | ||
71 | toPod: toPod, | ||
72 | sign: true, // Prove our identity | ||
73 | method: 'POST', | ||
74 | path: '/api/' + API_VERSION + '/remote/' + requestEndpoint, | ||
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 | ||
80 | makeSecureRequest(params, (err, res) => { | ||
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 | |||
85 | return callback(err) | ||
86 | } | ||
87 | |||
88 | return callback(null) | ||
89 | }) | ||
90 | } | ||
91 | |||
92 | // Make all the requests of the scheduler | ||
93 | protected makeRequests () { | ||
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 | |||
114 | eachLimit(Object.keys(requestsToMakeGrouped), REQUESTS_IN_PARALLEL, (hashKey, callbackEach) => { | ||
115 | const requestToMake = requestsToMakeGrouped[hashKey] | ||
116 | const toPod = requestToMake.toPod | ||
117 | |||
118 | this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, (err) => { | ||
119 | if (err) { | ||
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 | |||
141 | protected afterRequestHook () { | ||
142 | // Nothing to do, let children reimplement it | ||
143 | } | ||
144 | |||
145 | protected afterRequestsHook () { | ||
146 | // Nothing to do, let children reimplement it | ||
147 | } | ||
148 | } | ||
149 | |||
150 | // --------------------------------------------------------------------------- | ||
151 | |||
152 | export { | ||
153 | BaseRequestScheduler | ||
154 | } | ||