From f5028693a896a3076dd286ac0030e3d8f78f5ebf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 25 Oct 2017 16:03:33 +0200 Subject: Use async/await in lib and initializers --- server/lib/request/abstract-request-scheduler.ts | 121 +++++++++++---------- server/lib/request/request-scheduler.ts | 16 ++- .../lib/request/request-video-event-scheduler.ts | 20 ++-- server/lib/request/request-video-qadu-scheduler.ts | 34 +++--- 4 files changed, 96 insertions(+), 95 deletions(-) (limited to 'server/lib/request') diff --git a/server/lib/request/abstract-request-scheduler.ts b/server/lib/request/abstract-request-scheduler.ts index ce4e2ffd2..f46682824 100644 --- a/server/lib/request/abstract-request-scheduler.ts +++ b/server/lib/request/abstract-request-scheduler.ts @@ -1,5 +1,5 @@ import { isEmpty } from 'lodash' -import * as Promise from 'bluebird' +import * as Bluebird from 'bluebird' import { database as db } from '../../initializers/database' import { logger, makeSecureRequest } from '../../helpers' @@ -76,7 +76,7 @@ abstract class AbstractRequestScheduler { // --------------------------------------------------------------------------- // Make a requests to friends of a certain type - protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: any) { + protected async makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: any) { const params = { toPod: toPod, method: 'POST' as 'POST', @@ -86,72 +86,75 @@ abstract class AbstractRequestScheduler { // Make multiple retry requests to all of pods // The function fire some useful callbacks - return makeSecureRequest(params) - .then(({ response, body }) => { - if (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204) { - throw new Error('Status code not 20x : ' + response.statusCode) - } - }) - .catch(err => { - logger.error('Error sending secure request to %s pod.', toPod.host, err) - - throw err - }) + try { + const { response } = await makeSecureRequest(params) + if (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204) { + throw new Error('Status code not 20x : ' + response.statusCode) + } + } catch (err) { + logger.error('Error sending secure request to %s pod.', toPod.host, err) + + throw err + } } // Make all the requests of the scheduler - protected makeRequests () { - return this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod) - .then((requestsGrouped: T) => { - // We want to group requests by destinations pod and endpoint - const requestsToMake = this.buildRequestsObjects(requestsGrouped) - - // If there are no requests, abort - if (isEmpty(requestsToMake) === true) { - logger.info('No "%s" to make.', this.description) - return { goodPods: [], badPods: [] } - } - - logger.info('Making "%s" to friends.', this.description) - - const goodPods: number[] = [] - const badPods: number[] = [] - - return Promise.map(Object.keys(requestsToMake), hashKey => { - const requestToMake = requestsToMake[hashKey] - const toPod: PodInstance = requestToMake.toPod - - return this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas) - .then(() => { - logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids }) - goodPods.push(requestToMake.toPod.id) - - this.afterRequestHook() - - // Remove the pod id of these request ids - return this.getRequestToPodModel().removeByRequestIdsAndPod(requestToMake.ids, requestToMake.toPod.id) - }) - .catch(err => { - badPods.push(requestToMake.toPod.id) - logger.info('Cannot make request to %s.', toPod.host, err) - }) - }, { concurrency: REQUESTS_IN_PARALLEL }).then(() => ({ goodPods, badPods })) - }) - .then(({ goodPods, badPods }) => { - this.afterRequestsHook() - - // All the requests were made, we update the pods score - return db.Pod.updatePodsScore(goodPods, badPods) - }) - .catch(err => logger.error('Cannot get the list of "%s".', this.description, { error: err.stack })) + protected async makeRequests () { + let requestsGrouped: T + + try { + requestsGrouped = await this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod) + } catch (err) { + logger.error('Cannot get the list of "%s".', this.description, { error: err.stack }) + throw err + } + + // We want to group requests by destinations pod and endpoint + const requestsToMake = this.buildRequestsObjects(requestsGrouped) + + // If there are no requests, abort + if (isEmpty(requestsToMake) === true) { + logger.info('No "%s" to make.', this.description) + return { goodPods: [], badPods: [] } + } + + logger.info('Making "%s" to friends.', this.description) + + const goodPods: number[] = [] + const badPods: number[] = [] + + await Bluebird.map(Object.keys(requestsToMake), async hashKey => { + const requestToMake = requestsToMake[hashKey] + const toPod: PodInstance = requestToMake.toPod + + try { + await this.makeRequest(toPod, requestToMake.endpoint, requestToMake.datas) + logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids }) + goodPods.push(requestToMake.toPod.id) + + this.afterRequestHook() + + // Remove the pod id of these request ids + await this.getRequestToPodModel() + .removeByRequestIdsAndPod(requestToMake.ids, requestToMake.toPod.id) + } catch (err) { + badPods.push(requestToMake.toPod.id) + logger.info('Cannot make request to %s.', toPod.host, err) + } + }, { concurrency: REQUESTS_IN_PARALLEL }) + + this.afterRequestsHook() + + // All the requests were made, we update the pods score + await db.Pod.updatePodsScore(goodPods, badPods) } protected afterRequestHook () { - // Nothing to do, let children reimplement it + // Nothing to do, let children re-implement it } protected afterRequestsHook () { - // Nothing to do, let children reimplement it + // Nothing to do, let children re-implement it } } diff --git a/server/lib/request/request-scheduler.ts b/server/lib/request/request-scheduler.ts index 696875dcf..c3f7f6429 100644 --- a/server/lib/request/request-scheduler.ts +++ b/server/lib/request/request-scheduler.ts @@ -37,8 +37,8 @@ class RequestScheduler extends AbstractRequestScheduler { buildRequestsObjects (requestsGrouped: RequestsGrouped) { const requestsToMakeGrouped: RequestsObjects = {} - Object.keys(requestsGrouped).forEach(toPodId => { - requestsGrouped[toPodId].forEach(data => { + for (const toPodId of Object.keys(requestsGrouped)) { + for (const data of requestsGrouped[toPodId]) { const request = data.request const pod = data.pod const hashKey = toPodId + request.endpoint @@ -54,13 +54,13 @@ class RequestScheduler extends AbstractRequestScheduler { requestsToMakeGrouped[hashKey].ids.push(request.id) requestsToMakeGrouped[hashKey].datas.push(request.request) - }) - }) + } + } return requestsToMakeGrouped } - createRequest ({ type, endpoint, data, toIds, transaction }: RequestSchedulerOptions) { + async createRequest ({ type, endpoint, data, toIds, transaction }: RequestSchedulerOptions) { // If there are no destination pods abort if (toIds.length === 0) return undefined @@ -76,10 +76,8 @@ class RequestScheduler extends AbstractRequestScheduler { transaction } - return db.Request.create(createQuery, dbRequestOptions) - .then(request => { - return request.setPods(toIds, dbRequestOptions) - }) + const request = await db.Request.create(createQuery, dbRequestOptions) + await request.setPods(toIds, dbRequestOptions) } // --------------------------------------------------------------------------- diff --git a/server/lib/request/request-video-event-scheduler.ts b/server/lib/request/request-video-event-scheduler.ts index 680232732..5f21287f0 100644 --- a/server/lib/request/request-video-event-scheduler.ts +++ b/server/lib/request/request-video-event-scheduler.ts @@ -59,8 +59,8 @@ class RequestVideoEventScheduler extends AbstractRequestScheduler { - eventRequests[toPodId].forEach(eventToProcess => { + for (const toPodId of Object.keys(eventRequests)) { + for (const eventToProcess of eventRequests[toPodId]) { if (!eventsPerVideoPerPod[toPodId]) eventsPerVideoPerPod[toPodId] = {} if (!requestsToMakeGrouped[toPodId]) { @@ -81,17 +81,17 @@ class RequestVideoEventScheduler extends AbstractRequestScheduler { + for (const toPodId of Object.keys(eventsPerVideoPerPod)) { const eventsForPod = eventsPerVideoPerPod[toPodId] - Object.keys(eventsForPod).forEach(uuid => { + for (const uuid of Object.keys(eventsForPod)) { const eventsForVideo = eventsForPod[uuid] - Object.keys(eventsForVideo).forEach(eventType => { + for (const eventType of Object.keys(eventsForVideo)) { requestsToMakeGrouped[toPodId].datas.push({ data: { uuid, @@ -99,9 +99,9 @@ class RequestVideoEventScheduler extends AbstractRequestScheduler = {} - Object.keys(requests).forEach(toPodId => { - requests[toPodId].forEach(data => { + for (const toPodId of Object.keys(requests)) { + for (const data of requests[toPodId]) { const request = data.request const video = data.video const pod = data.pod @@ -105,39 +105,39 @@ class RequestVideoQaduScheduler extends AbstractRequestScheduler { - Object.keys(requestsToMakeGrouped[hashKey].videos).forEach(videoUUID => { + for (const hashKey of Object.keys(requestsToMakeGrouped)) { + for (const videoUUID of Object.keys(requestsToMakeGrouped[hashKey].videos)) { const videoData = requestsToMakeGrouped[hashKey].videos[videoUUID] requestsToMakeGrouped[hashKey].datas.push({ data: videoData }) - }) + } // We don't need it anymore, it was just to build our data array delete requestsToMakeGrouped[hashKey].videos - }) + } return requestsToMakeGrouped } - createRequest ({ type, videoId, transaction }: RequestVideoQaduSchedulerOptions) { + async createRequest ({ type, videoId, transaction }: RequestVideoQaduSchedulerOptions) { const dbRequestOptions: Sequelize.BulkCreateOptions = {} if (transaction) dbRequestOptions.transaction = transaction // Send the update to all our friends - return db.Pod.listAllIds(transaction).then(podIds => { - const queries = [] - podIds.forEach(podId => { - queries.push({ type, videoId, podId }) - }) - - return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions) - }) + const podIds = await db.Pod.listAllIds(transaction) + const queries = [] + for (const podId of podIds) { + queries.push({ type, videoId, podId }) + } + + await db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions) + return undefined } } -- cgit v1.2.3