1 import { database as db } from '../../initializers/database'
2 import { BaseRequestScheduler } from './base-request-scheduler'
3 import { logger } from '../../helpers'
7 } from '../../initializers'
9 class RequestScheduler extends BaseRequestScheduler {
13 // We limit the size of the requests
14 this.limitPods = REQUESTS_LIMIT_PODS
15 this.limitPerPod = REQUESTS_LIMIT_PER_POD
17 this.description = 'requests'
24 getRequestToPodModel () {
25 return db.RequestToPod
28 buildRequestObjects (requests) {
29 const requestsToMakeGrouped = {}
31 Object.keys(requests).forEach(toPodId => {
32 requests[toPodId].forEach(data => {
33 const request = data.request
35 const hashKey = toPodId + request.endpoint
37 if (!requestsToMakeGrouped[hashKey]) {
38 requestsToMakeGrouped[hashKey] = {
40 endpoint: request.endpoint,
41 ids: [], // request ids, to delete them from the DB in the future
42 datas: [] // requests data,
46 requestsToMakeGrouped[hashKey].ids.push(request.id)
47 requestsToMakeGrouped[hashKey].datas.push(request.request)
51 return requestsToMakeGrouped
54 // { type, endpoint, data, toIds, transaction }
55 createRequest (options, callback) {
56 const type = options.type
57 const endpoint = options.endpoint
58 const data = options.data
59 const toIds = options.toIds
60 const transaction = options.transaction
62 // TODO: check the setPods works
65 // If there are no destination pods abort
66 if (toIds.length === 0) return callback(null)
68 toIds.forEach(toPod => {
80 const dbRequestOptions = {
84 return db.Request.create(createQuery, dbRequestOptions).asCallback((err, request) => {
85 if (err) return callback(err)
87 return request.setPods(podIds, dbRequestOptions).asCallback(callback)
91 // ---------------------------------------------------------------------------
93 afterRequestsHook () {
94 // Flush requests with no pod
95 this.getRequestModel().removeWithEmptyTo(err => {
96 if (err) logger.error('Error when removing requests with no pods.', { error: err })
101 // ---------------------------------------------------------------------------