- // For each requests to make, we add it to the correct request type
- async.each(requests, function (poolRequest, callbackEach) {
- if (REQUEST_SCHEDULER_TYPE.indexOf(poolRequest.type) > -1) {
- const requestTypeToMake = requestsToMake[poolRequest.type]
- requestTypeToMake.requests.push(poolRequest.request)
- requestTypeToMake.ids.push(poolRequest._id)
- } else {
- logger.error('Unkown request type.', { request_type: poolRequest.type })
- return // abort
- }
-
- callbackEach()
- }, function () {
- for (let type of Object.keys(requestsToMake)) {
- const requestTypeToMake = requestsToMake[type]
- // If there are requests for this type
- if (requestTypeToMake.requests.length !== 0) {
- makeRequest(type, requestTypeToMake.requests, function (err) {
- if (err) logger.error('Errors when sent ' + type + ' requests.', { error: err })
-
- // We made the requests, so we can remove them from the scheduler
- Requests.removeRequests(requestTypeToMake.ids)
- })
+ 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()