Server: randomize the requests list

We don't want to stuck with the same failing requests
pull/24/head
Chocobozzz 2016-10-23 19:14:28 +02:00
parent 5e9acecaeb
commit 43666d616d
1 changed files with 14 additions and 3 deletions

View File

@ -128,7 +128,9 @@ function makeRequest (toPod, requestsToMake, callback) {
function makeRequests () { function makeRequests () {
const self = this const self = this
listWithLimit.call(self, constants.REQUESTS_LIMIT, function (err, requests) { // We limit the size of the requests (REQUESTS_LIMIT)
// We don't want to stuck with the same failing requests so we get a random list
listWithLimitAndRandom.call(self, constants.REQUESTS_LIMIT, function (err, requests) {
if (err) { if (err) {
logger.error('Cannot get the list of requests.', { err: err }) logger.error('Cannot get the list of requests.', { err: err })
return // Abort return // Abort
@ -249,8 +251,17 @@ function updatePodsScore (goodPods, badPods) {
}) })
} }
function listWithLimit (limit, callback) { function listWithLimitAndRandom (limit, callback) {
this.find({ }, { _id: 1, request: 1, to: 1 }).sort({ _id: 1 }).limit(limit).exec(callback) const self = this
self.count(function (err, count) {
if (err) return callback(err)
let start = Math.floor(Math.random() * count) - limit
if (start < 0) start = 0
self.find({ }, { _id: 1, request: 1, to: 1 }).sort({ _id: 1 }).skip(start).limit(limit).exec(callback)
})
} }
function removeAll (callback) { function removeAll (callback) {