- // Send the remove requests
- if (requests_to_make.remove.requests.length !== 0) {
- makeRequest('remove', requests_to_make.remove.requests, function (err) {
- if (err) logger.error('Errors when sent remove pool requests.', { error: err })
+ requests.forEach(function (poolRequest) {
+ poolRequest.to.forEach(function (toPodId) {
+ if (!requestsToMake[toPodId]) {
+ requestsToMake[toPodId] = {
+ ids: [],
+ datas: []
+ }
+ }
+
+ requestsToMake[toPodId].ids.push(poolRequest._id)
+ requestsToMake[toPodId].datas.push(poolRequest.request)
+ })
+ })
+
+ const goodPods = []
+ const badPods = []
+
+ async.eachLimit(Object.keys(requestsToMake), constants.REQUESTS_IN_PARALLEL, function (toPodId, callbackEach) {
+ const requestToMake = requestsToMake[toPodId]
+
+ // FIXME: mongodb request inside a loop :/
+ Pods.findById(toPodId, function (err, toPod) {
+ if (err) return logger.error('Error finding pod by id.', { err: err })
+
+ // Maybe the pod is not our friend anymore so simply remove them
+ if (!toPod) {
+ Requests.removePodOf(requestToMake.ids, toPodId)
+ return callbackEach()
+ }
+
+ makeRequest(toPod, requestToMake.datas, function (success) {
+ if (err) {
+ logger.error('Errors when sent request to %s.', toPod.url, { error: err })
+ // Do not stop the process just for one error
+ return callbackEach()
+ }
+
+ if (success === true) {
+ logger.debug('Removing requests for %s pod.', toPodId, { requestsIds: requestToMake.ids })
+
+ // Remove the pod id of these request ids
+ Requests.removePodOf(requestToMake.ids, toPodId)
+ goodPods.push(toPodId)
+ } else {
+ badPods.push(toPodId)
+ }