BaseRequestScheduler -> AbstractRequestScheduler
authorChocobozzz <florian.bigard@gmail.com>
Fri, 16 Jun 2017 07:14:41 +0000 (09:14 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Fri, 16 Jun 2017 07:14:41 +0000 (09:14 +0200)
server/controllers/api/requests.ts
server/lib/request/abstract-request-scheduler.ts [new file with mode: 0644]
server/lib/request/base-request-scheduler.ts [deleted file]
server/lib/request/index.ts
server/lib/request/request-scheduler.ts
server/lib/request/request-video-event-scheduler.ts
server/lib/request/request-video-qadu-scheduler.ts

index 2c18fe04608f6ec79161db35d0e739d44e83b9f7..d3f0d958c290f75a0036c00af302c30de66a5352 100644 (file)
@@ -2,7 +2,7 @@ import * as express from 'express'
 import { parallel } from 'async'
 
 import {
-  BaseRequestScheduler,
+  AbstractRequestScheduler,
   getRequestScheduler,
   getRequestVideoQaduScheduler,
   getRequestVideoEventScheduler
@@ -39,7 +39,7 @@ function getStatsRequests (req: express.Request, res: express.Response, next: ex
 
 // ---------------------------------------------------------------------------
 
-function buildRequestSchedulerFunction (requestScheduler: BaseRequestScheduler) {
+function buildRequestSchedulerFunction (requestScheduler: AbstractRequestScheduler) {
   return function (callback) {
     requestScheduler.remainingRequestsCount(function (err, count) {
       if (err) return callback(err)
diff --git a/server/lib/request/abstract-request-scheduler.ts b/server/lib/request/abstract-request-scheduler.ts
new file mode 100644 (file)
index 0000000..e81ab9c
--- /dev/null
@@ -0,0 +1,156 @@
+import * as eachLimit from 'async/eachLimit'
+
+import { database as db } from '../../initializers/database'
+import { logger, makeSecureRequest } from '../../helpers'
+import { PodInstance } from '../../models'
+import {
+  API_VERSION,
+  REQUESTS_IN_PARALLEL,
+  REQUESTS_INTERVAL
+} from '../../initializers'
+
+abstract class AbstractRequestScheduler {
+  requestInterval: number
+  limitPods: number
+  limitPerPod: number
+
+  protected lastRequestTimestamp: number
+  protected timer: NodeJS.Timer
+  protected description: string
+
+  constructor () {
+    this.lastRequestTimestamp = 0
+    this.timer = null
+    this.requestInterval = REQUESTS_INTERVAL
+  }
+
+  abstract getRequestModel ()
+  abstract getRequestToPodModel ()
+  abstract buildRequestObjects (requests: any)
+
+  activate () {
+    logger.info('Requests scheduler activated.')
+    this.lastRequestTimestamp = Date.now()
+
+    this.timer = setInterval(() => {
+      this.lastRequestTimestamp = Date.now()
+      this.makeRequests()
+    }, this.requestInterval)
+  }
+
+  deactivate () {
+    logger.info('Requests scheduler deactivated.')
+    clearInterval(this.timer)
+    this.timer = null
+  }
+
+  forceSend () {
+    logger.info('Force requests scheduler sending.')
+    this.makeRequests()
+  }
+
+  remainingMilliSeconds () {
+    if (this.timer === null) return -1
+
+    return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp)
+  }
+
+  remainingRequestsCount (callback: (err: Error, total: number) => void) {
+    return this.getRequestModel().countTotalRequests(callback)
+  }
+
+  flush (callback: (err: Error) => void) {
+    this.getRequestModel().removeAll(callback)
+  }
+
+  // ---------------------------------------------------------------------------
+
+  // Make a requests to friends of a certain type
+  protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: Object, callback) {
+    if (!callback) callback = function () { /* empty */ }
+
+    const params = {
+      toPod: toPod,
+      sign: true, // Prove our identity
+      method: 'POST' as 'POST',
+      path: '/api/' + API_VERSION + '/remote/' + requestEndpoint,
+      data: requestsToMake // Requests we need to make
+    }
+
+    // Make multiple retry requests to all of pods
+    // The function fire some useful callbacks
+    makeSecureRequest(params, (err, res) => {
+      if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
+        err = err ? err.message : 'Status code not 20x : ' + res.statusCode
+        logger.error('Error sending secure request to %s pod.', toPod.host, { error: err })
+
+        return callback(err)
+      }
+
+      return callback(null)
+    })
+  }
+
+    // Make all the requests of the scheduler
+  protected makeRequests () {
+    this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod, (err, requests) => {
+      if (err) {
+        logger.error('Cannot get the list of "%s".', this.description, { err: err })
+        return // Abort
+      }
+
+      // If there are no requests, abort
+      if (requests.length === 0) {
+        logger.info('No "%s" to make.', this.description)
+        return
+      }
+
+      // We want to group requests by destinations pod and endpoint
+      const requestsToMakeGrouped = this.buildRequestObjects(requests)
+
+      logger.info('Making "%s" to friends.', this.description)
+
+      const goodPods = []
+      const badPods = []
+
+      eachLimit(Object.keys(requestsToMakeGrouped), REQUESTS_IN_PARALLEL, (hashKey, callbackEach) => {
+        const requestToMake = requestsToMakeGrouped[hashKey]
+        const toPod = requestToMake.toPod
+
+        this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, (err) => {
+          if (err) {
+            badPods.push(requestToMake.toPod.id)
+            return callbackEach()
+          }
+
+          logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids })
+          goodPods.push(requestToMake.toPod.id)
+
+          // Remove the pod id of these request ids
+          this.getRequestToPodModel().removeByRequestIdsAndPod(requestToMake.ids, requestToMake.toPod.id, callbackEach)
+
+          this.afterRequestHook()
+        })
+      }, () => {
+        // All the requests were made, we update the pods score
+        db.Pod.updatePodsScore(goodPods, badPods)
+
+        this.afterRequestsHook()
+      })
+    })
+  }
+
+  protected afterRequestHook () {
+   // Nothing to do, let children reimplement it
+  }
+
+  protected afterRequestsHook () {
+   // Nothing to do, let children reimplement it
+  }
+}
+
+// ---------------------------------------------------------------------------
+
+export {
+  AbstractRequestScheduler
+}
diff --git a/server/lib/request/base-request-scheduler.ts b/server/lib/request/base-request-scheduler.ts
deleted file mode 100644 (file)
index 26bdc2b..0000000
+++ /dev/null
@@ -1,156 +0,0 @@
-import * as eachLimit from 'async/eachLimit'
-
-import { database as db } from '../../initializers/database'
-import { logger, makeSecureRequest } from '../../helpers'
-import { PodInstance } from '../../models'
-import {
-  API_VERSION,
-  REQUESTS_IN_PARALLEL,
-  REQUESTS_INTERVAL
-} from '../../initializers'
-
-abstract class BaseRequestScheduler {
-  requestInterval: number
-  limitPods: number
-  limitPerPod: number
-
-  protected lastRequestTimestamp: number
-  protected timer: NodeJS.Timer
-  protected description: string
-
-  constructor () {
-    this.lastRequestTimestamp = 0
-    this.timer = null
-    this.requestInterval = REQUESTS_INTERVAL
-  }
-
-  abstract getRequestModel ()
-  abstract getRequestToPodModel ()
-  abstract buildRequestObjects (requests: any)
-
-  activate () {
-    logger.info('Requests scheduler activated.')
-    this.lastRequestTimestamp = Date.now()
-
-    this.timer = setInterval(() => {
-      this.lastRequestTimestamp = Date.now()
-      this.makeRequests()
-    }, this.requestInterval)
-  }
-
-  deactivate () {
-    logger.info('Requests scheduler deactivated.')
-    clearInterval(this.timer)
-    this.timer = null
-  }
-
-  forceSend () {
-    logger.info('Force requests scheduler sending.')
-    this.makeRequests()
-  }
-
-  remainingMilliSeconds () {
-    if (this.timer === null) return -1
-
-    return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp)
-  }
-
-  remainingRequestsCount (callback: (err: Error, total: number) => void) {
-    return this.getRequestModel().countTotalRequests(callback)
-  }
-
-  flush (callback: (err: Error) => void) {
-    this.getRequestModel().removeAll(callback)
-  }
-
-  // ---------------------------------------------------------------------------
-
-  // Make a requests to friends of a certain type
-  protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: Object, callback) {
-    if (!callback) callback = function () { /* empty */ }
-
-    const params = {
-      toPod: toPod,
-      sign: true, // Prove our identity
-      method: 'POST' as 'POST',
-      path: '/api/' + API_VERSION + '/remote/' + requestEndpoint,
-      data: requestsToMake // Requests we need to make
-    }
-
-    // Make multiple retry requests to all of pods
-    // The function fire some useful callbacks
-    makeSecureRequest(params, (err, res) => {
-      if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
-        err = err ? err.message : 'Status code not 20x : ' + res.statusCode
-        logger.error('Error sending secure request to %s pod.', toPod.host, { error: err })
-
-        return callback(err)
-      }
-
-      return callback(null)
-    })
-  }
-
-    // Make all the requests of the scheduler
-  protected makeRequests () {
-    this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod, (err, requests) => {
-      if (err) {
-        logger.error('Cannot get the list of "%s".', this.description, { err: err })
-        return // Abort
-      }
-
-      // If there are no requests, abort
-      if (requests.length === 0) {
-        logger.info('No "%s" to make.', this.description)
-        return
-      }
-
-      // We want to group requests by destinations pod and endpoint
-      const requestsToMakeGrouped = this.buildRequestObjects(requests)
-
-      logger.info('Making "%s" to friends.', this.description)
-
-      const goodPods = []
-      const badPods = []
-
-      eachLimit(Object.keys(requestsToMakeGrouped), REQUESTS_IN_PARALLEL, (hashKey, callbackEach) => {
-        const requestToMake = requestsToMakeGrouped[hashKey]
-        const toPod = requestToMake.toPod
-
-        this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, (err) => {
-          if (err) {
-            badPods.push(requestToMake.toPod.id)
-            return callbackEach()
-          }
-
-          logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids })
-          goodPods.push(requestToMake.toPod.id)
-
-          // Remove the pod id of these request ids
-          this.getRequestToPodModel().removeByRequestIdsAndPod(requestToMake.ids, requestToMake.toPod.id, callbackEach)
-
-          this.afterRequestHook()
-        })
-      }, () => {
-        // All the requests were made, we update the pods score
-        db.Pod.updatePodsScore(goodPods, badPods)
-
-        this.afterRequestsHook()
-      })
-    })
-  }
-
-  protected afterRequestHook () {
-   // Nothing to do, let children reimplement it
-  }
-
-  protected afterRequestsHook () {
-   // Nothing to do, let children reimplement it
-  }
-}
-
-// ---------------------------------------------------------------------------
-
-export {
-  BaseRequestScheduler
-}
index 110d0ed78befc55b1afb839b853873ad18bee8b0..47d60e5b4342335665a2399f385243ec5f9a1765 100644 (file)
@@ -1,4 +1,4 @@
-export * from './base-request-scheduler'
+export * from './abstract-request-scheduler'
 export * from './request-scheduler'
 export * from './request-video-event-scheduler'
 export * from './request-video-qadu-scheduler'
index 69d840eeb140705547c14ab15b19e63fb56f011d..38c0cca3dd4b8e559d4bd009dc2da02a21cc537e 100644 (file)
@@ -1,7 +1,7 @@
 import * as Sequelize from 'sequelize'
 
 import { database as db } from '../../initializers/database'
-import { BaseRequestScheduler } from './base-request-scheduler'
+import { AbstractRequestScheduler } from './abstract-request-scheduler'
 import { logger } from '../../helpers'
 import {
   REQUESTS_LIMIT_PODS,
@@ -16,7 +16,7 @@ export type RequestSchedulerOptions = {
   transaction: Sequelize.Transaction
 }
 
-class RequestScheduler extends BaseRequestScheduler {
+class RequestScheduler extends AbstractRequestScheduler {
   constructor () {
     super()
 
index 9da82585ef321b8103fa789af4306a057e5d8f5d..c9d1651177a89ce7bec12577da74bc521077e65a 100644 (file)
@@ -1,7 +1,7 @@
 import * as Sequelize from 'sequelize'
 
 import { database as db } from '../../initializers/database'
-import { BaseRequestScheduler } from './base-request-scheduler'
+import { AbstractRequestScheduler } from './abstract-request-scheduler'
 import {
   REQUESTS_VIDEO_EVENT_LIMIT_PODS,
   REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
@@ -15,7 +15,7 @@ export type RequestVideoEventSchedulerOptions = {
   transaction?: Sequelize.Transaction
 }
 
-class RequestVideoEventScheduler extends BaseRequestScheduler {
+class RequestVideoEventScheduler extends AbstractRequestScheduler {
   constructor () {
     super()
 
index 436fd8e5035755370bbdf50acafdfc8ffbd002dc..49970a2e6a8c8b390a5f41af59d027d962985ba6 100644 (file)
@@ -1,7 +1,7 @@
 import * as Sequelize from 'sequelize'
 
 import { database as db } from '../../initializers/database'
-import { BaseRequestScheduler } from './base-request-scheduler'
+import { AbstractRequestScheduler } from './abstract-request-scheduler'
 import { logger } from '../../helpers'
 import {
   REQUESTS_VIDEO_QADU_LIMIT_PODS,
@@ -16,7 +16,7 @@ export type RequestVideoQaduSchedulerOptions = {
   transaction?: Sequelize.Transaction
 }
 
-class RequestVideoQaduScheduler extends BaseRequestScheduler {
+class RequestVideoQaduScheduler extends AbstractRequestScheduler {
   constructor () {
     super()