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