]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - src/utils.js
Create a constants module to easily modify some constants in a test
[github/Chocobozzz/PeerTube.git] / src / utils.js
index d6b26db4bc5760e7b6fed8d7c08728fd2fb47e1d..5880c6c90409e08ff67febde7fbbe9a22e82d41a 100644 (file)
@@ -1,6 +1,7 @@
 ;(function () {
   'use strict'
 
+  var async = require('async')
   var config = require('config')
   var crypto = require('crypto')
   var fs = require('fs')
@@ -9,6 +10,7 @@
   var replay = require('request-replay')
   var ursa = require('ursa')
 
+  var constants = require('./constants')
   var logger = require('./logger')
 
   var utils = {}
       }
     }
 
-    logger.debug('Sending informations to %s.', to_pod.url, { params: params })
+    logger.debug('Make retry requests to %s.', to_pod.url)
 
-    // Replay 15 times, with factor 3
     replay(
       request.post(params, function (err, response, body) {
-        callbackEach(err, response, body, to_pod.url)
+        callbackEach(err, response, body, params.url, to_pod)
       }),
       {
-        retries: 10,
+        retries: constants.REQUEST_RETRIES,
         factor: 3,
         maxTimeout: Infinity,
         errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
     }
 
     // Make a request for each pod
-    for (var pod of pods) {
+    async.each(pods, function (pod, callback_each_async) {
+      function callbackEachRetryRequest (err, response, body, url, pod) {
+        callbackEach(err, response, body, url, pod, function () {
+          callback_each_async()
+        })
+      }
+
       var params = {
         url: pod.url + all_data.path,
         method: all_data.method
@@ -76,8 +83,6 @@
 
       // Add data with POST requst ?
       if (all_data.method === 'POST' && all_data.data) {
-        logger.debug('Make a POST request.')
-
         // Encrypt data ?
         if (all_data.encrypt === true) {
           var crt = ursa.createPublicKey(pod.publicKey)
                 key: passwordEncrypted
               }
 
-              makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEach)
+              makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest)
             })
           })(crt, params, url, pod, signature)
         } else {
           params.json = { data: all_data.data }
-          makeRetryRequest(params, url, pod, signature, callbackEach)
+          makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
         }
       } else {
-        logger.debug('Make a GET/DELETE request')
-        makeRetryRequest(params, url, pod, signature, callbackEach)
+        makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
       }
-    }
-
-    return callback()
+    }, callback)
   }
 
   utils.certsExist = function (callback) {