'use strict'
-const async = require('async')
+const each = require('async/each')
+const eachLimit = require('async/eachLimit')
const map = require('lodash/map')
const mongoose = require('mongoose')
+const waterfall = require('async/waterfall')
const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
const RequestSchema = mongoose.Schema({
request: mongoose.Schema.Types.Mixed,
- to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'users' } ]
+ to: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Pod' } ]
})
RequestSchema.statics = {
activate,
deactivate,
flush,
- forceSend
+ forceSend,
+ list
}
RequestSchema.pre('save', function (next) {
function activate () {
logger.info('Requests scheduler activated.')
- timer = setInterval(makeRequests.bind(this), constants.INTERVAL)
+ timer = setInterval(makeRequests.bind(this), constants.REQUESTS_INTERVAL)
}
function deactivate () {
makeRequests.call(this)
}
+function list (callback) {
+ this.find({ }, callback)
+}
+
// ---------------------------------------------------------------------------
// Make a requests to friends of a certain type
function makeRequests () {
const self = this
- list.call(self, function (err, requests) {
+ listWithLimit.call(self, constants.REQUESTS_LIMIT, function (err, requests) {
if (err) {
logger.error('Cannot get the list of requests.', { err: err })
return // Abort
const goodPods = []
const badPods = []
- async.eachLimit(Object.keys(requestsToMake), constants.REQUESTS_IN_PARALLEL, function (toPodId, callbackEach) {
+ eachLimit(Object.keys(requestsToMake), constants.REQUESTS_IN_PARALLEL, function (toPodId, callbackEach) {
const requestToMake = requestsToMake[toPodId]
// FIXME: mongodb request inside a loop :/
// Remove pods with a score of 0 (too many requests where they were unreachable)
function removeBadPods () {
- async.waterfall([
+ waterfall([
function findBadPods (callback) {
Pod.listBadPods(function (err, pods) {
if (err) {
return callback(null)
}
- async.each(videosList, function (video, callbackEach) {
+ each(videosList, function (video, callbackEach) {
video.remove(callbackEach)
}, function (err) {
if (err) {
return callback(null)
}
- async.each(pods, function (pod, callbackEach) {
+ each(pods, function (pod, callbackEach) {
pod.remove(callbackEach)
}, function (err) {
if (err) return callback(err)
})
}
-function list (callback) {
- this.find({ }, { _id: 1, request: 1, to: 1 }, callback)
+function listWithLimit (limit, callback) {
+ this.find({ }, { _id: 1, request: 1, to: 1 }).sort({ _id: 1 }).limit(limit).exec(callback)
}
function removeAll (callback) {