]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/request-scheduler.js
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')
8 module
.exports
= class RequestScheduler
extends BaseRequestScheduler
{
13 // We limit the size of the requests
14 this.limitPods
= constants
.REQUESTS_LIMIT_PODS
15 this.limitPerPod
= constants
.REQUESTS_LIMIT_PER_POD
17 this.description
= 'requests'
24 getRequestToPodModel () {
25 return db
.RequestToPod
28 buildRequestObjects (requests
) {
29 const requestsToMakeGrouped
= {}
31 Object
.keys(requests
).forEach(toPodId
=> {
32 requests
[toPodId
].forEach(data
=> {
33 const request
= data
.request
35 const hashKey
= toPodId
+ request
.endpoint
37 if (!requestsToMakeGrouped
[hashKey
]) {
38 requestsToMakeGrouped
[hashKey
] = {
40 endpoint: request
.endpoint
,
41 ids: [], // request ids, to delete them from the DB in the future
42 datas: [] // requests data,
46 requestsToMakeGrouped
[hashKey
].ids
.push(request
.id
)
47 requestsToMakeGrouped
[hashKey
].datas
.push(request
.request
)
51 return requestsToMakeGrouped
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
64 // If there are no destination pods abort
65 if (toIds
.length
=== 0) return callback(null)
67 toIds
.forEach(toPod
=> {
68 pods
.push(db
.Pod
.build({ id: toPod
}))
79 const dbRequestOptions
= {
83 return db
.Request
.create(createQuery
, dbRequestOptions
).asCallback((err
, request
) => {
84 if (err
) return callback(err
)
86 return request
.setPods(pods
, dbRequestOptions
).asCallback(callback
)
90 // ---------------------------------------------------------------------------
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
})