From 15a302943d84bc0978b84fe33110c4daa451d311 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jun 2017 09:14:41 +0200 Subject: BaseRequestScheduler -> AbstractRequestScheduler --- server/controllers/api/requests.ts | 4 +- server/lib/request/abstract-request-scheduler.ts | 156 +++++++++++++++++++++ server/lib/request/base-request-scheduler.ts | 156 --------------------- server/lib/request/index.ts | 2 +- server/lib/request/request-scheduler.ts | 4 +- .../lib/request/request-video-event-scheduler.ts | 4 +- server/lib/request/request-video-qadu-scheduler.ts | 4 +- 7 files changed, 165 insertions(+), 165 deletions(-) create mode 100644 server/lib/request/abstract-request-scheduler.ts delete mode 100644 server/lib/request/base-request-scheduler.ts (limited to 'server') diff --git a/server/controllers/api/requests.ts b/server/controllers/api/requests.ts index 2c18fe046..d3f0d958c 100644 --- a/server/controllers/api/requests.ts +++ b/server/controllers/api/requests.ts @@ -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 index 000000000..e81ab9c36 --- /dev/null +++ b/server/lib/request/abstract-request-scheduler.ts @@ -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 index 26bdc2bff..000000000 --- a/server/lib/request/base-request-scheduler.ts +++ /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 -} diff --git a/server/lib/request/index.ts b/server/lib/request/index.ts index 110d0ed78..47d60e5b4 100644 --- a/server/lib/request/index.ts +++ b/server/lib/request/index.ts @@ -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' diff --git a/server/lib/request/request-scheduler.ts b/server/lib/request/request-scheduler.ts index 69d840eeb..38c0cca3d 100644 --- a/server/lib/request/request-scheduler.ts +++ b/server/lib/request/request-scheduler.ts @@ -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() diff --git a/server/lib/request/request-video-event-scheduler.ts b/server/lib/request/request-video-event-scheduler.ts index 9da82585e..c9d165117 100644 --- a/server/lib/request/request-video-event-scheduler.ts +++ b/server/lib/request/request-video-event-scheduler.ts @@ -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() diff --git a/server/lib/request/request-video-qadu-scheduler.ts b/server/lib/request/request-video-qadu-scheduler.ts index 436fd8e50..49970a2e6 100644 --- a/server/lib/request/request-video-qadu-scheduler.ts +++ b/server/lib/request/request-video-qadu-scheduler.ts @@ -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() -- cgit v1.2.3