]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/request.js
Server: reorganize express validators
[github/Chocobozzz/PeerTube.git] / server / models / request.js
index db6ad54096f847de35b4934f657d52d8ef5adebc..4d521919a11185fd4cb3c522bd1f99be1e6061f3 100644 (file)
@@ -1,8 +1,10 @@
 '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')
@@ -102,7 +104,7 @@ function makeRequest (toPod, requestsToMake, callback) {
 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
@@ -136,7 +138,7 @@ function makeRequests () {
     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 :/
@@ -183,7 +185,7 @@ function makeRequests () {
 
 // 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) {
@@ -217,7 +219,7 @@ function removeBadPods () {
         return callback(null)
       }
 
-      async.each(videosList, function (video, callbackEach) {
+      each(videosList, function (video, callbackEach) {
         video.remove(callbackEach)
       }, function (err) {
         if (err) {
@@ -237,7 +239,7 @@ function removeBadPods () {
         return callback(null)
       }
 
-      async.each(pods, function (pod, callbackEach) {
+      each(pods, function (pod, callbackEach) {
         pod.remove(callbackEach)
       }, function (err) {
         if (err) return callback(err)
@@ -269,8 +271,8 @@ function updatePodsScore (goodPods, badPods) {
   })
 }
 
-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) {