1 import * as Sequelize from 'sequelize'
3 import { database as db } from '../../initializers/database'
4 import { AbstractRequestScheduler } from './abstract-request-scheduler'
5 import { logger } from '../../helpers'
9 } from '../../initializers'
11 export type RequestSchedulerOptions = {
16 transaction: Sequelize.Transaction
19 class RequestScheduler extends AbstractRequestScheduler {
23 // We limit the size of the requests
24 this.limitPods = REQUESTS_LIMIT_PODS
25 this.limitPerPod = REQUESTS_LIMIT_PER_POD
27 this.description = 'requests'
34 getRequestToPodModel () {
35 return db.RequestToPod
38 buildRequestObjects (requests: { [ toPodId: number ]: any }) {
39 const requestsToMakeGrouped = {}
41 Object.keys(requests).forEach(toPodId => {
42 requests[toPodId].forEach(data => {
43 const request = data.request
45 const hashKey = toPodId + request.endpoint
47 if (!requestsToMakeGrouped[hashKey]) {
48 requestsToMakeGrouped[hashKey] = {
50 endpoint: request.endpoint,
51 ids: [], // request ids, to delete them from the DB in the future
52 datas: [] // requests data,
56 requestsToMakeGrouped[hashKey].ids.push(request.id)
57 requestsToMakeGrouped[hashKey].datas.push(request.request)
61 return requestsToMakeGrouped
64 createRequest ({ type, endpoint, data, toIds, transaction }: RequestSchedulerOptions, callback: (err: Error) => void) {
65 // TODO: check the setPods works
68 // If there are no destination pods abort
69 if (toIds.length === 0) return callback(null)
71 toIds.forEach(toPod => {
83 const dbRequestOptions: Sequelize.CreateOptions = {
87 return db.Request.create(createQuery, dbRequestOptions).asCallback((err, request) => {
88 if (err) return callback(err)
90 return request.setPods(podIds, dbRequestOptions).asCallback(callback)
94 // ---------------------------------------------------------------------------
96 afterRequestsHook () {
97 // Flush requests with no pod
98 this.getRequestModel().removeWithEmptyTo(err => {
99 if (err) logger.error('Error when removing requests with no pods.', { error: err })
104 // ---------------------------------------------------------------------------