]>
Commit | Line | Data |
---|---|---|
c1a7ab7f C |
1 | 'use strict' |
2 | ||
c1a7ab7f | 3 | const constants = require('../initializers/constants') |
9e167724 | 4 | const BaseRequestScheduler = require('./base-request-scheduler') |
c1a7ab7f C |
5 | const db = require('../initializers/database') |
6 | const logger = require('../helpers/logger') | |
c1a7ab7f | 7 | |
9e167724 | 8 | module.exports = class RequestScheduler extends BaseRequestScheduler { |
fe783f6b | 9 | constructor () { |
9e167724 | 10 | super() |
c1a7ab7f | 11 | |
9e167724 C |
12 | // We limit the size of the requests |
13 | this.limitPods = constants.REQUESTS_LIMIT_PODS | |
14 | this.limitPerPod = constants.REQUESTS_LIMIT_PER_POD | |
c1a7ab7f | 15 | |
9e167724 | 16 | this.description = 'requests' |
c1a7ab7f C |
17 | } |
18 | ||
9e167724 C |
19 | getRequestModel () { |
20 | return db.Request | |
c1a7ab7f C |
21 | } |
22 | ||
9e167724 C |
23 | getRequestToPodModel () { |
24 | return db.RequestToPod | |
c1a7ab7f C |
25 | } |
26 | ||
9e167724 C |
27 | buildRequestObjects (requests) { |
28 | const requestsToMakeGrouped = {} | |
29 | ||
30 | Object.keys(requests).forEach(toPodId => { | |
31 | requests[toPodId].forEach(data => { | |
32 | const request = data.request | |
33 | const pod = data.pod | |
34 | const hashKey = toPodId + request.endpoint | |
35 | ||
36 | if (!requestsToMakeGrouped[hashKey]) { | |
37 | requestsToMakeGrouped[hashKey] = { | |
38 | toPod: pod, | |
39 | endpoint: request.endpoint, | |
40 | ids: [], // request ids, to delete them from the DB in the future | |
41 | datas: [] // requests data, | |
42 | } | |
43 | } | |
44 | ||
45 | requestsToMakeGrouped[hashKey].ids.push(request.id) | |
46 | requestsToMakeGrouped[hashKey].datas.push(request.request) | |
47 | }) | |
48 | }) | |
c1a7ab7f | 49 | |
9e167724 | 50 | return requestsToMakeGrouped |
c1a7ab7f C |
51 | } |
52 | ||
53 | // { type, endpoint, data, toIds, transaction } | |
54 | createRequest (options, callback) { | |
55 | const type = options.type | |
56 | const endpoint = options.endpoint | |
57 | const data = options.data | |
58 | const toIds = options.toIds | |
59 | const transaction = options.transaction | |
60 | ||
61 | const pods = [] | |
62 | ||
63 | // If there are no destination pods abort | |
64 | if (toIds.length === 0) return callback(null) | |
65 | ||
66 | toIds.forEach(toPod => { | |
67 | pods.push(db.Pod.build({ id: toPod })) | |
68 | }) | |
69 | ||
70 | const createQuery = { | |
71 | endpoint, | |
72 | request: { | |
73 | type: type, | |
74 | data: data | |
75 | } | |
76 | } | |
77 | ||
78 | const dbRequestOptions = { | |
79 | transaction | |
80 | } | |
81 | ||
82 | return db.Request.create(createQuery, dbRequestOptions).asCallback((err, request) => { | |
83 | if (err) return callback(err) | |
84 | ||
85 | return request.setPods(pods, dbRequestOptions).asCallback(callback) | |
86 | }) | |
87 | } | |
88 | ||
89 | // --------------------------------------------------------------------------- | |
90 | ||
9e167724 C |
91 | afterRequestsHook () { |
92 | // Flush requests with no pod | |
93 | this.getRequestModel().removeWithEmptyTo(err => { | |
94 | if (err) logger.error('Error when removing requests with no pods.', { error: err }) | |
c1a7ab7f C |
95 | }) |
96 | } | |
97 | } |