diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-05 17:35:58 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-05 17:35:58 +0200 |
commit | 5a976a8c351d2a9e23ceee1e193fca2893b7f12d (patch) | |
tree | 9e22c1cf835b68082bc3a12adf6a0a7b40e6ce7f /server/lib/request/request-scheduler.js | |
parent | 4e284e97b95d1fe93378b90061272e3abc875078 (diff) | |
download | PeerTube-5a976a8c351d2a9e23ceee1e193fca2893b7f12d.tar.gz PeerTube-5a976a8c351d2a9e23ceee1e193fca2893b7f12d.tar.zst PeerTube-5a976a8c351d2a9e23ceee1e193fca2893b7f12d.zip |
Server: move requests lib in their own directory
Diffstat (limited to 'server/lib/request/request-scheduler.js')
-rw-r--r-- | server/lib/request/request-scheduler.js | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/server/lib/request/request-scheduler.js b/server/lib/request/request-scheduler.js new file mode 100644 index 000000000..555ec3e54 --- /dev/null +++ b/server/lib/request/request-scheduler.js | |||
@@ -0,0 +1,97 @@ | |||
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 | constructor () { | ||
10 | super() | ||
11 | |||
12 | // We limit the size of the requests | ||
13 | this.limitPods = constants.REQUESTS_LIMIT_PODS | ||
14 | this.limitPerPod = constants.REQUESTS_LIMIT_PER_POD | ||
15 | |||
16 | this.description = 'requests' | ||
17 | } | ||
18 | |||
19 | getRequestModel () { | ||
20 | return db.Request | ||
21 | } | ||
22 | |||
23 | getRequestToPodModel () { | ||
24 | return db.RequestToPod | ||
25 | } | ||
26 | |||
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 | }) | ||
49 | |||
50 | return requestsToMakeGrouped | ||
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 | |||
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 }) | ||
95 | }) | ||
96 | } | ||
97 | } | ||