]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/request.js
Server: fix remaining milli seconds before the next requests feature
[github/Chocobozzz/PeerTube.git] / server / models / request.js
index 4e510e18aec84d040aeea42092f63374369e5d11..2d50d94e0c26805d2c2298c15b5a0d3132b0eedc 100644 (file)
@@ -14,6 +14,7 @@ const Pod = mongoose.model('Pod')
 const Video = mongoose.model('Video')
 
 let timer = null
+let lastRequestTimestamp = 0
 
 // ---------------------------------------------------------------------------
 
@@ -27,7 +28,8 @@ RequestSchema.statics = {
   deactivate,
   flush,
   forceSend,
-  list
+  list,
+  remainingMilliSeconds
 }
 
 RequestSchema.pre('save', function (next) {
@@ -54,12 +56,19 @@ mongoose.model('Request', RequestSchema)
 
 function activate () {
   logger.info('Requests scheduler activated.')
-  timer = setInterval(makeRequests.bind(this), constants.REQUESTS_INTERVAL)
+  lastRequestTimestamp = Date.now()
+
+  const self = this
+  timer = setInterval(function () {
+    lastRequestTimestamp = Date.now()
+    makeRequests.call(self)
+  }, constants.REQUESTS_INTERVAL)
 }
 
 function deactivate () {
   logger.info('Requests scheduler deactivated.')
   clearInterval(timer)
+  timer = null
 }
 
 function flush () {
@@ -77,6 +86,12 @@ function list (callback) {
   this.find({ }, callback)
 }
 
+function remainingMilliSeconds () {
+  if (timer === null) return -1
+
+  return constants.REQUESTS_INTERVAL - (Date.now() - lastRequestTimestamp)
+}
+
 // ---------------------------------------------------------------------------
 
 // Make a requests to friends of a certain type
@@ -96,7 +111,13 @@ function makeRequest (toPod, requestsToMake, callback) {
   // The function fire some useful callbacks
   requests.makeSecureRequest(params, function (err, res) {
     if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
-      logger.error('Error sending secure request to %s pod.', toPod.url, { error: err || new Error('Status code not 20x') })
+      logger.error(
+        'Error sending secure request to %s pod.',
+        toPod.url,
+        {
+          error: err || new Error('Status code not 20x : ' + res.statusCode)
+        }
+      )
 
       return callback(false)
     }
@@ -153,19 +174,13 @@ function makeRequests () {
           return callbackEach()
         }
 
-        // Maybe the pod is not our friend anymore so simply remove them
+        // Maybe the pod is not our friend anymore so simply remove it
         if (!toPod) {
           removePodOf.call(self, requestToMake.ids, toPodId)
           return callbackEach()
         }
 
         makeRequest(toPod, requestToMake.datas, function (success) {
-          if (err) {
-            logger.error('Errors when sent request to %s.', toPod.url, { error: err })
-            // Do not stop the process just for one error
-            return callbackEach()
-          }
-
           if (success === true) {
             logger.debug('Removing requests for %s pod.', toPodId, { requestsIds: requestToMake.ids })