2017-02-18 04:29:36 -05:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const constants = require('../initializers/constants')
|
2017-02-21 15:35:59 -05:00
|
|
|
const BaseRequestScheduler = require('./base-request-scheduler')
|
2017-02-18 04:29:36 -05:00
|
|
|
const db = require('../initializers/database')
|
|
|
|
const logger = require('../helpers/logger')
|
|
|
|
|
2017-02-21 15:35:59 -05:00
|
|
|
module.exports = class RequestScheduler extends BaseRequestScheduler {
|
2017-02-18 12:37:26 -05:00
|
|
|
constructor () {
|
2017-02-21 15:35:59 -05:00
|
|
|
super()
|
2017-02-18 04:29:36 -05:00
|
|
|
|
2017-02-21 15:35:59 -05:00
|
|
|
// We limit the size of the requests
|
|
|
|
this.limitPods = constants.REQUESTS_LIMIT_PODS
|
|
|
|
this.limitPerPod = constants.REQUESTS_LIMIT_PER_POD
|
2017-02-18 04:29:36 -05:00
|
|
|
|
2017-02-21 15:35:59 -05:00
|
|
|
this.description = 'requests'
|
2017-02-18 04:29:36 -05:00
|
|
|
}
|
|
|
|
|
2017-02-21 15:35:59 -05:00
|
|
|
getRequestModel () {
|
|
|
|
return db.Request
|
2017-02-18 04:29:36 -05:00
|
|
|
}
|
|
|
|
|
2017-02-21 15:35:59 -05:00
|
|
|
getRequestToPodModel () {
|
|
|
|
return db.RequestToPod
|
2017-02-18 04:29:36 -05:00
|
|
|
}
|
|
|
|
|
2017-02-21 15:35:59 -05:00
|
|
|
buildRequestObjects (requests) {
|
|
|
|
const requestsToMakeGrouped = {}
|
|
|
|
|
|
|
|
Object.keys(requests).forEach(toPodId => {
|
|
|
|
requests[toPodId].forEach(data => {
|
|
|
|
const request = data.request
|
|
|
|
const pod = data.pod
|
|
|
|
const hashKey = toPodId + request.endpoint
|
|
|
|
|
|
|
|
if (!requestsToMakeGrouped[hashKey]) {
|
|
|
|
requestsToMakeGrouped[hashKey] = {
|
|
|
|
toPod: pod,
|
|
|
|
endpoint: request.endpoint,
|
|
|
|
ids: [], // request ids, to delete them from the DB in the future
|
|
|
|
datas: [] // requests data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
requestsToMakeGrouped[hashKey].ids.push(request.id)
|
|
|
|
requestsToMakeGrouped[hashKey].datas.push(request.request)
|
|
|
|
})
|
|
|
|
})
|
2017-02-18 04:29:36 -05:00
|
|
|
|
2017-02-21 15:35:59 -05:00
|
|
|
return requestsToMakeGrouped
|
2017-02-18 04:29:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// { type, endpoint, data, toIds, transaction }
|
|
|
|
createRequest (options, callback) {
|
|
|
|
const type = options.type
|
|
|
|
const endpoint = options.endpoint
|
|
|
|
const data = options.data
|
|
|
|
const toIds = options.toIds
|
|
|
|
const transaction = options.transaction
|
|
|
|
|
|
|
|
const pods = []
|
|
|
|
|
|
|
|
// If there are no destination pods abort
|
|
|
|
if (toIds.length === 0) return callback(null)
|
|
|
|
|
|
|
|
toIds.forEach(toPod => {
|
|
|
|
pods.push(db.Pod.build({ id: toPod }))
|
|
|
|
})
|
|
|
|
|
|
|
|
const createQuery = {
|
|
|
|
endpoint,
|
|
|
|
request: {
|
|
|
|
type: type,
|
|
|
|
data: data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const dbRequestOptions = {
|
|
|
|
transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.Request.create(createQuery, dbRequestOptions).asCallback((err, request) => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return request.setPods(pods, dbRequestOptions).asCallback(callback)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-02-21 15:35:59 -05:00
|
|
|
afterRequestsHook () {
|
|
|
|
// Flush requests with no pod
|
|
|
|
this.getRequestModel().removeWithEmptyTo(err => {
|
|
|
|
if (err) logger.error('Error when removing requests with no pods.', { error: err })
|
2017-02-18 04:29:36 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|