]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/request/request-scheduler.ts
26ffbfb868f892e5f384a3caf059dd728600d15d
[github/Chocobozzz/PeerTube.git] / server / lib / request / request-scheduler.ts
1 import { database as db } from '../../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 // TODO: check the setPods works
63 const podIds = []
64
65 // If there are no destination pods abort
66 if (toIds.length === 0) return callback(null)
67
68 toIds.forEach(toPod => {
69 podIds.push(toPod)
70 })
71
72 const createQuery = {
73 endpoint,
74 request: {
75 type: type,
76 data: data
77 }
78 }
79
80 const dbRequestOptions = {
81 transaction
82 }
83
84 return db.Request.create(createQuery, dbRequestOptions).asCallback((err, request) => {
85 if (err) return callback(err)
86
87 return request.setPods(podIds, dbRequestOptions).asCallback(callback)
88 })
89 }
90
91 // ---------------------------------------------------------------------------
92
93 afterRequestsHook () {
94 // Flush requests with no pod
95 this.getRequestModel().removeWithEmptyTo(err => {
96 if (err) logger.error('Error when removing requests with no pods.', { error: err })
97 })
98 }
99 }
100
101 // ---------------------------------------------------------------------------
102
103 export {
104 RequestScheduler
105 }