diff options
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/request.js | 59 |
1 files changed, 37 insertions, 22 deletions
diff --git a/server/models/request.js b/server/models/request.js index 34a4287ea..f5eec2134 100644 --- a/server/models/request.js +++ b/server/models/request.js | |||
@@ -2,6 +2,7 @@ | |||
2 | 2 | ||
3 | const each = require('async/each') | 3 | const each = require('async/each') |
4 | const eachLimit = require('async/eachLimit') | 4 | const eachLimit = require('async/eachLimit') |
5 | const values = require('lodash/values') | ||
5 | const mongoose = require('mongoose') | 6 | const mongoose = require('mongoose') |
6 | const waterfall = require('async/waterfall') | 7 | const waterfall = require('async/waterfall') |
7 | 8 | ||
@@ -18,7 +19,16 @@ let lastRequestTimestamp = 0 | |||
18 | 19 | ||
19 | const RequestSchema = mongoose.Schema({ | 20 | const RequestSchema = mongoose.Schema({ |
20 | request: mongoose.Schema.Types.Mixed, | 21 | request: mongoose.Schema.Types.Mixed, |
21 | to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Pod' } ] | 22 | endpoint: { |
23 | type: String, | ||
24 | enum: [ values(constants.REQUEST_ENDPOINTS) ] | ||
25 | }, | ||
26 | to: [ | ||
27 | { | ||
28 | type: mongoose.Schema.Types.ObjectId, | ||
29 | ref: 'Pod' | ||
30 | } | ||
31 | ] | ||
22 | }) | 32 | }) |
23 | 33 | ||
24 | RequestSchema.statics = { | 34 | RequestSchema.statics = { |
@@ -93,7 +103,7 @@ function remainingMilliSeconds () { | |||
93 | // --------------------------------------------------------------------------- | 103 | // --------------------------------------------------------------------------- |
94 | 104 | ||
95 | // Make a requests to friends of a certain type | 105 | // Make a requests to friends of a certain type |
96 | function makeRequest (toPod, requestsToMake, callback) { | 106 | function makeRequest (toPod, requestEndpoint, requestsToMake, callback) { |
97 | if (!callback) callback = function () {} | 107 | if (!callback) callback = function () {} |
98 | 108 | ||
99 | const params = { | 109 | const params = { |
@@ -101,7 +111,7 @@ function makeRequest (toPod, requestsToMake, callback) { | |||
101 | encrypt: true, // Security | 111 | encrypt: true, // Security |
102 | sign: true, // To prove our identity | 112 | sign: true, // To prove our identity |
103 | method: 'POST', | 113 | method: 'POST', |
104 | path: '/api/' + constants.API_VERSION + '/remote/videos', | 114 | path: '/api/' + constants.API_VERSION + '/remote/' + requestEndpoint, |
105 | data: requestsToMake // Requests we need to make | 115 | data: requestsToMake // Requests we need to make |
106 | } | 116 | } |
107 | 117 | ||
@@ -144,31 +154,34 @@ function makeRequests () { | |||
144 | 154 | ||
145 | logger.info('Making requests to friends.') | 155 | logger.info('Making requests to friends.') |
146 | 156 | ||
147 | // Requests by pods id | 157 | // We want to group requests by destinations pod and endpoint |
148 | const requestsToMake = {} | 158 | const requestsToMakeGrouped = {} |
149 | 159 | ||
150 | requests.forEach(function (poolRequest) { | 160 | requests.forEach(function (poolRequest) { |
151 | poolRequest.to.forEach(function (toPodId) { | 161 | poolRequest.to.forEach(function (toPodId) { |
152 | if (!requestsToMake[toPodId]) { | 162 | const hashKey = toPodId + poolRequest.endpoint |
153 | requestsToMake[toPodId] = { | 163 | if (!requestsToMakeGrouped[hashKey]) { |
154 | ids: [], | 164 | requestsToMakeGrouped[hashKey] = { |
155 | datas: [] | 165 | toPodId, |
166 | endpoint: poolRequest.endpoint, | ||
167 | ids: [], // pool request ids, to delete them from the DB in the future | ||
168 | datas: [] // requests data, | ||
156 | } | 169 | } |
157 | } | 170 | } |
158 | 171 | ||
159 | requestsToMake[toPodId].ids.push(poolRequest._id) | 172 | requestsToMakeGrouped[hashKey].ids.push(poolRequest._id) |
160 | requestsToMake[toPodId].datas.push(poolRequest.request) | 173 | requestsToMakeGrouped[hashKey].datas.push(poolRequest.request) |
161 | }) | 174 | }) |
162 | }) | 175 | }) |
163 | 176 | ||
164 | const goodPods = [] | 177 | const goodPods = [] |
165 | const badPods = [] | 178 | const badPods = [] |
166 | 179 | ||
167 | eachLimit(Object.keys(requestsToMake), constants.REQUESTS_IN_PARALLEL, function (toPodId, callbackEach) { | 180 | eachLimit(Object.keys(requestsToMakeGrouped), constants.REQUESTS_IN_PARALLEL, function (hashKey, callbackEach) { |
168 | const requestToMake = requestsToMake[toPodId] | 181 | const requestToMake = requestsToMakeGrouped[hashKey] |
169 | 182 | ||
170 | // FIXME: mongodb request inside a loop :/ | 183 | // FIXME: mongodb request inside a loop :/ |
171 | Pod.load(toPodId, function (err, toPod) { | 184 | Pod.load(requestToMake.toPodId, function (err, toPod) { |
172 | if (err) { | 185 | if (err) { |
173 | logger.error('Error finding pod by id.', { err: err }) | 186 | logger.error('Error finding pod by id.', { err: err }) |
174 | return callbackEach() | 187 | return callbackEach() |
@@ -176,21 +189,23 @@ function makeRequests () { | |||
176 | 189 | ||
177 | // Maybe the pod is not our friend anymore so simply remove it | 190 | // Maybe the pod is not our friend anymore so simply remove it |
178 | if (!toPod) { | 191 | if (!toPod) { |
179 | logger.info('Removing %d requests of unexisting pod %s.', requestToMake.ids.length, toPodId) | 192 | const requestIdsToDelete = requestToMake.ids |
180 | removePodOf.call(self, requestToMake.ids, toPodId) | 193 | |
194 | logger.info('Removing %d requests of unexisting pod %s.', requestIdsToDelete.length, requestToMake.toPodId) | ||
195 | removePodOf.call(self, requestIdsToDelete, requestToMake.toPodId) | ||
181 | return callbackEach() | 196 | return callbackEach() |
182 | } | 197 | } |
183 | 198 | ||
184 | makeRequest(toPod, requestToMake.datas, function (success) { | 199 | makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, function (success) { |
185 | if (success === true) { | 200 | if (success === true) { |
186 | logger.debug('Removing requests for %s pod.', toPodId, { requestsIds: requestToMake.ids }) | 201 | logger.debug('Removing requests for %s pod.', requestToMake.toPodId, { requestsIds: requestToMake.ids }) |
187 | 202 | ||
188 | goodPods.push(toPodId) | 203 | goodPods.push(requestToMake.toPodId) |
189 | 204 | ||
190 | // Remove the pod id of these request ids | 205 | // Remove the pod id of these request ids |
191 | removePodOf.call(self, requestToMake.ids, toPodId, callbackEach) | 206 | removePodOf.call(self, requestToMake.ids, requestToMake.toPodId, callbackEach) |
192 | } else { | 207 | } else { |
193 | badPods.push(toPodId) | 208 | badPods.push(requestToMake.toPodId) |
194 | callbackEach() | 209 | callbackEach() |
195 | } | 210 | } |
196 | }) | 211 | }) |
@@ -260,7 +275,7 @@ function listWithLimitAndRandom (limit, callback) { | |||
260 | let start = Math.floor(Math.random() * count) - limit | 275 | let start = Math.floor(Math.random() * count) - limit |
261 | if (start < 0) start = 0 | 276 | if (start < 0) start = 0 |
262 | 277 | ||
263 | self.find({ }, { _id: 1, request: 1, to: 1 }).sort({ _id: 1 }).skip(start).limit(limit).exec(callback) | 278 | self.find().sort({ _id: 1 }).skip(start).limit(limit).exec(callback) |
264 | }) | 279 | }) |
265 | } | 280 | } |
266 | 281 | ||