2017-06-10 15:15:25 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-06-16 02:14:41 -05:00
|
|
|
import { AbstractRequestScheduler } from './abstract-request-scheduler'
|
2017-05-15 15:22:03 -05:00
|
|
|
import { logger } from '../../helpers'
|
|
|
|
import {
|
|
|
|
REQUESTS_LIMIT_PODS,
|
|
|
|
REQUESTS_LIMIT_PER_POD
|
|
|
|
} from '../../initializers'
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
export type RequestSchedulerOptions = {
|
|
|
|
type: string
|
|
|
|
endpoint: string
|
|
|
|
data: Object
|
|
|
|
toIds: number[]
|
|
|
|
transaction: Sequelize.Transaction
|
|
|
|
}
|
|
|
|
|
2017-06-16 02:14:41 -05:00
|
|
|
class RequestScheduler extends AbstractRequestScheduler {
|
2017-02-18 11:37:26 -06:00
|
|
|
constructor () {
|
2017-02-21 14:35:59 -06:00
|
|
|
super()
|
2017-02-18 03:29:36 -06:00
|
|
|
|
2017-02-21 14:35:59 -06:00
|
|
|
// We limit the size of the requests
|
2017-05-15 15:22:03 -05:00
|
|
|
this.limitPods = REQUESTS_LIMIT_PODS
|
|
|
|
this.limitPerPod = REQUESTS_LIMIT_PER_POD
|
2017-02-18 03:29:36 -06:00
|
|
|
|
2017-02-21 14:35:59 -06:00
|
|
|
this.description = 'requests'
|
2017-02-18 03:29:36 -06:00
|
|
|
}
|
|
|
|
|
2017-02-21 14:35:59 -06:00
|
|
|
getRequestModel () {
|
|
|
|
return db.Request
|
2017-02-18 03:29:36 -06:00
|
|
|
}
|
|
|
|
|
2017-02-21 14:35:59 -06:00
|
|
|
getRequestToPodModel () {
|
|
|
|
return db.RequestToPod
|
2017-02-18 03:29:36 -06:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
buildRequestObjects (requests: { [ toPodId: number ]: any }) {
|
2017-02-21 14:35:59 -06:00
|
|
|
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 03:29:36 -06:00
|
|
|
|
2017-02-21 14:35:59 -06:00
|
|
|
return requestsToMakeGrouped
|
2017-02-18 03:29:36 -06:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
createRequest ({ type, endpoint, data, toIds, transaction }: RequestSchedulerOptions, callback: (err: Error) => void) {
|
2017-05-22 13:58:25 -05:00
|
|
|
// TODO: check the setPods works
|
|
|
|
const podIds = []
|
2017-02-18 03:29:36 -06:00
|
|
|
|
|
|
|
// If there are no destination pods abort
|
|
|
|
if (toIds.length === 0) return callback(null)
|
|
|
|
|
|
|
|
toIds.forEach(toPod => {
|
2017-05-22 13:58:25 -05:00
|
|
|
podIds.push(toPod)
|
2017-02-18 03:29:36 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
const createQuery = {
|
|
|
|
endpoint,
|
|
|
|
request: {
|
|
|
|
type: type,
|
|
|
|
data: data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
const dbRequestOptions: Sequelize.CreateOptions = {
|
2017-02-18 03:29:36 -06:00
|
|
|
transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.Request.create(createQuery, dbRequestOptions).asCallback((err, request) => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
return request.setPods(podIds, dbRequestOptions).asCallback(callback)
|
2017-02-18 03:29:36 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-02-21 14:35:59 -06: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 03:29:36 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
RequestScheduler
|
|
|
|
}
|