diff options
Diffstat (limited to 'server/lib/request/base-request-scheduler.js')
-rw-r--r-- | server/lib/request/base-request-scheduler.js | 136 |
1 files changed, 0 insertions, 136 deletions
diff --git a/server/lib/request/base-request-scheduler.js b/server/lib/request/base-request-scheduler.js deleted file mode 100644 index 782448340..000000000 --- a/server/lib/request/base-request-scheduler.js +++ /dev/null | |||
@@ -1,136 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const eachLimit = require('async/eachLimit') | ||
4 | |||
5 | const constants = require('../../initializers/constants') | ||
6 | const db = require('../../initializers/database') | ||
7 | const logger = require('../../helpers/logger') | ||
8 | const requests = require('../../helpers/requests') | ||
9 | |||
10 | module.exports = class BaseRequestScheduler { | ||
11 | constructor (options) { | ||
12 | this.lastRequestTimestamp = 0 | ||
13 | this.timer = null | ||
14 | this.requestInterval = constants.REQUESTS_INTERVAL | ||
15 | } | ||
16 | |||
17 | activate () { | ||
18 | logger.info('Requests scheduler activated.') | ||
19 | this.lastRequestTimestamp = Date.now() | ||
20 | |||
21 | this.timer = setInterval(() => { | ||
22 | this.lastRequestTimestamp = Date.now() | ||
23 | this.makeRequests() | ||
24 | }, this.requestInterval) | ||
25 | } | ||
26 | |||
27 | deactivate () { | ||
28 | logger.info('Requests scheduler deactivated.') | ||
29 | clearInterval(this.timer) | ||
30 | this.timer = null | ||
31 | } | ||
32 | |||
33 | forceSend () { | ||
34 | logger.info('Force requests scheduler sending.') | ||
35 | this.makeRequests() | ||
36 | } | ||
37 | |||
38 | remainingMilliSeconds () { | ||
39 | if (this.timer === null) return -1 | ||
40 | |||
41 | return constants.REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp) | ||
42 | } | ||
43 | |||
44 | remainingRequestsCount (callback) { | ||
45 | return this.getRequestModel().countTotalRequests(callback) | ||
46 | } | ||
47 | |||
48 | // --------------------------------------------------------------------------- | ||
49 | |||
50 | // Make a requests to friends of a certain type | ||
51 | makeRequest (toPod, requestEndpoint, requestsToMake, callback) { | ||
52 | if (!callback) callback = function () {} | ||
53 | |||
54 | const params = { | ||
55 | toPod: toPod, | ||
56 | sign: true, // Prove our identity | ||
57 | method: 'POST', | ||
58 | path: '/api/' + constants.API_VERSION + '/remote/' + requestEndpoint, | ||
59 | data: requestsToMake // Requests we need to make | ||
60 | } | ||
61 | |||
62 | // Make multiple retry requests to all of pods | ||
63 | // The function fire some useful callbacks | ||
64 | requests.makeSecureRequest(params, (err, res) => { | ||
65 | if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) { | ||
66 | err = err ? err.message : 'Status code not 20x : ' + res.statusCode | ||
67 | logger.error('Error sending secure request to %s pod.', toPod.host, { error: err }) | ||
68 | |||
69 | return callback(err) | ||
70 | } | ||
71 | |||
72 | return callback(null) | ||
73 | }) | ||
74 | } | ||
75 | |||
76 | // Make all the requests of the scheduler | ||
77 | makeRequests () { | ||
78 | this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod, (err, requests) => { | ||
79 | if (err) { | ||
80 | logger.error('Cannot get the list of "%s".', this.description, { err: err }) | ||
81 | return // Abort | ||
82 | } | ||
83 | |||
84 | // If there are no requests, abort | ||
85 | if (requests.length === 0) { | ||
86 | logger.info('No "%s" to make.', this.description) | ||
87 | return | ||
88 | } | ||
89 | |||
90 | // We want to group requests by destinations pod and endpoint | ||
91 | const requestsToMakeGrouped = this.buildRequestObjects(requests) | ||
92 | |||
93 | logger.info('Making "%s" to friends.', this.description) | ||
94 | |||
95 | const goodPods = [] | ||
96 | const badPods = [] | ||
97 | |||
98 | eachLimit(Object.keys(requestsToMakeGrouped), constants.REQUESTS_IN_PARALLEL, (hashKey, callbackEach) => { | ||
99 | const requestToMake = requestsToMakeGrouped[hashKey] | ||
100 | const toPod = requestToMake.toPod | ||
101 | |||
102 | this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, (err) => { | ||
103 | if (err) { | ||
104 | badPods.push(requestToMake.toPod.id) | ||
105 | return callbackEach() | ||
106 | } | ||
107 | |||
108 | logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids }) | ||
109 | goodPods.push(requestToMake.toPod.id) | ||
110 | |||
111 | // Remove the pod id of these request ids | ||
112 | this.getRequestToPodModel().removeByRequestIdsAndPod(requestToMake.ids, requestToMake.toPod.id, callbackEach) | ||
113 | |||
114 | this.afterRequestHook() | ||
115 | }) | ||
116 | }, () => { | ||
117 | // All the requests were made, we update the pods score | ||
118 | db.Pod.updatePodsScore(goodPods, badPods) | ||
119 | |||
120 | this.afterRequestsHook() | ||
121 | }) | ||
122 | }) | ||
123 | } | ||
124 | |||
125 | flush (callback) { | ||
126 | this.getRequestModel().removeAll(callback) | ||
127 | } | ||
128 | |||
129 | afterRequestHook () { | ||
130 | // Nothing to do, let children reimplement it | ||
131 | } | ||
132 | |||
133 | afterRequestsHook () { | ||
134 | // Nothing to do, let children reimplement it | ||
135 | } | ||
136 | } | ||