]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/request/base-request-scheduler.ts
First typescript iteration
[github/Chocobozzz/PeerTube.git] / server / lib / request / base-request-scheduler.ts
similarity index 72%
rename from server/lib/request/base-request-scheduler.js
rename to server/lib/request/base-request-scheduler.ts
index 782448340cea95030aa97b055b96de290c209017..7fc88b5f150066c84f28f18b14eb7a7e8ff6c1c7 100644 (file)
@@ -1,19 +1,31 @@
-'use strict'
+import { eachLimit } from 'async/eachLimit'
 
-const eachLimit = require('async/eachLimit')
-
-const constants = require('../../initializers/constants')
 const db = require('../../initializers/database')
-const logger = require('../../helpers/logger')
-const requests = require('../../helpers/requests')
-
-module.exports = class BaseRequestScheduler {
-  constructor (options) {
+import { logger, makeSecureRequest } from '../../helpers'
+import {
+  API_VERSION,
+  REQUESTS_IN_PARALLEL,
+  REQUESTS_INTERVAL
+} from '../../initializers'
+
+abstract class BaseRequestScheduler {
+  protected lastRequestTimestamp: number
+  protected timer: NodeJS.Timer
+  protected requestInterval: number
+  protected limitPods: number
+  protected limitPerPod: number
+  protected description: string
+
+  constructor () {
     this.lastRequestTimestamp = 0
     this.timer = null
-    this.requestInterval = constants.REQUESTS_INTERVAL
+    this.requestInterval = REQUESTS_INTERVAL
   }
 
+  abstract getRequestModel ()
+  abstract getRequestToPodModel ()
+  abstract buildRequestObjects (requests: any)
+
   activate () {
     logger.info('Requests scheduler activated.')
     this.lastRequestTimestamp = Date.now()
@@ -38,30 +50,34 @@ module.exports = class BaseRequestScheduler {
   remainingMilliSeconds () {
     if (this.timer === null) return -1
 
-    return constants.REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp)
+    return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp)
   }
 
   remainingRequestsCount (callback) {
     return this.getRequestModel().countTotalRequests(callback)
   }
 
+  flush (callback) {
+    this.getRequestModel().removeAll(callback)
+  }
+
   // ---------------------------------------------------------------------------
 
   // Make a requests to friends of a certain type
-  makeRequest (toPod, requestEndpoint, requestsToMake, callback) {
-    if (!callback) callback = function () {}
+  protected makeRequest (toPod, requestEndpoint, requestsToMake, callback) {
+    if (!callback) callback = function () { /* empty */ }
 
     const params = {
       toPod: toPod,
       sign: true, // Prove our identity
       method: 'POST',
-      path: '/api/' + constants.API_VERSION + '/remote/' + requestEndpoint,
+      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
-    requests.makeSecureRequest(params, (err, res) => {
+    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 })
@@ -74,7 +90,7 @@ module.exports = class BaseRequestScheduler {
   }
 
     // Make all the requests of the scheduler
-  makeRequests () {
+  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 })
@@ -95,7 +111,7 @@ module.exports = class BaseRequestScheduler {
       const goodPods = []
       const badPods = []
 
-      eachLimit(Object.keys(requestsToMakeGrouped), constants.REQUESTS_IN_PARALLEL, (hashKey, callbackEach) => {
+      eachLimit(Object.keys(requestsToMakeGrouped), REQUESTS_IN_PARALLEL, (hashKey, callbackEach) => {
         const requestToMake = requestsToMakeGrouped[hashKey]
         const toPod = requestToMake.toPod
 
@@ -122,15 +138,17 @@ module.exports = class BaseRequestScheduler {
     })
   }
 
-  flush (callback) {
-    this.getRequestModel().removeAll(callback)
-  }
-
-  afterRequestHook () {
+  protected afterRequestHook () {
    // Nothing to do, let children reimplement it
   }
 
-  afterRequestsHook () {
+  protected afterRequestsHook () {
    // Nothing to do, let children reimplement it
   }
 }
+
+// ---------------------------------------------------------------------------
+
+export {
+  BaseRequestScheduler
+}