]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - lib/poolRequests.js
Move the creation of requests in lib instead of model for poolrequests
[github/Chocobozzz/PeerTube.git] / lib / poolRequests.js
index ccc3489ab195d137e281edffac51a5784b6256fa..5b7d5489d071766f6b91e3870045704c61d8f6a3 100644 (file)
 
   var poolRequests = {
     activate: activate,
+    addRequest: addRequest,
     deactivate: deactivate,
     forceSend: forceSend
   }
 
+  function activate () {
+    logger.info('Pool requests activated.')
+    timer = setInterval(makePoolRequests, constants.INTERVAL)
+  }
+
+  function addRequest (id, type, request) {
+    logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
+
+    PoolRequests.findById(id, function (err, entity) {
+      if (err) {
+        logger.error('Cannot find one pool request.', { error: err })
+        return // Abort
+      }
+
+      if (entity) {
+        if (entity.type === type) {
+          logger.error('Cannot insert two same requests.')
+          return // Abort
+        }
+
+        // Remove the request of the other type
+        PoolRequests.removeById(id, function (err) {
+          if (err) {
+            logger.error('Cannot remove a pool request.', { error: err })
+            return // Abort
+          }
+        })
+      } else {
+        PoolRequests.create(id, type, request, function (err) {
+          logger.error('Cannot create a pool request.', { error: err })
+          return // Abort
+        })
+      }
+    })
+  }
+
   function deactivate () {
     logger.info('Pool requests deactivated.')
     clearInterval(timer)
     makePoolRequests()
   }
 
-  function activate () {
-    logger.info('Pool requests activated.')
-    timer = setInterval(makePoolRequests, constants.INTERVAL)
-  }
-
   // ---------------------------------------------------------------------------
 
   module.exports = poolRequests