]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const constants = require('../initializers/constants') | |
4 | const BaseRequestScheduler = require('./base-request-scheduler') | |
5 | const db = require('../initializers/database') | |
6 | const logger = require('../helpers/logger') | |
7 | ||
8 | module.exports = class RequestScheduler extends BaseRequestScheduler { | |
9 | ||
10 | constructor () { | |
11 | super() | |
12 | ||
13 | // We limit the size of the requests | |
14 | this.limitPods = constants.REQUESTS_LIMIT_PODS | |
15 | this.limitPerPod = constants.REQUESTS_LIMIT_PER_POD | |
16 | ||
17 | this.description = 'requests' | |
18 | } | |
19 | ||
20 | getRequestModel () { | |
21 | return db.Request | |
22 | } | |
23 | ||
24 | getRequestToPodModel () { | |
25 | return db.RequestToPod | |
26 | } | |
27 | ||
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 | }) | |
50 | ||
51 | return requestsToMakeGrouped | |
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 | ||
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 }) | |
96 | }) | |
97 | } | |
98 | } |