-'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()
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 })
}
// 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 })
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
})
}
- 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
+}