diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/lib/request/request-scheduler.ts | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/lib/request/request-scheduler.ts')
-rw-r--r-- | server/lib/request/request-scheduler.ts | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/server/lib/request/request-scheduler.ts b/server/lib/request/request-scheduler.ts new file mode 100644 index 000000000..2006a6f03 --- /dev/null +++ b/server/lib/request/request-scheduler.ts | |||
@@ -0,0 +1,104 @@ | |||
1 | const db = require('../../initializers/database') | ||
2 | import { BaseRequestScheduler } from './base-request-scheduler' | ||
3 | import { logger } from '../../helpers' | ||
4 | import { | ||
5 | REQUESTS_LIMIT_PODS, | ||
6 | REQUESTS_LIMIT_PER_POD | ||
7 | } from '../../initializers' | ||
8 | |||
9 | class RequestScheduler extends BaseRequestScheduler { | ||
10 | constructor () { | ||
11 | super() | ||
12 | |||
13 | // We limit the size of the requests | ||
14 | this.limitPods = REQUESTS_LIMIT_PODS | ||
15 | this.limitPerPod = 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 | } | ||
99 | |||
100 | // --------------------------------------------------------------------------- | ||
101 | |||
102 | export { | ||
103 | RequestScheduler | ||
104 | } | ||