]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/request/request-scheduler.ts
Create types for model enums
[github/Chocobozzz/PeerTube.git] / server / lib / request / request-scheduler.ts
CommitLineData
69818c93
C
1import * as Sequelize from 'sequelize'
2
e02643f3 3import { database as db } from '../../initializers/database'
15a30294 4import { AbstractRequestScheduler } from './abstract-request-scheduler'
65fcc311
C
5import { logger } from '../../helpers'
6import {
7 REQUESTS_LIMIT_PODS,
8 REQUESTS_LIMIT_PER_POD
9} from '../../initializers'
ee9e7b61 10import { RequestEndpoint } from '../../../shared'
65fcc311 11
69818c93
C
12export type RequestSchedulerOptions = {
13 type: string
ee9e7b61 14 endpoint: RequestEndpoint
69818c93
C
15 data: Object
16 toIds: number[]
17 transaction: Sequelize.Transaction
18}
19
15a30294 20class RequestScheduler extends AbstractRequestScheduler {
fe783f6b 21 constructor () {
9e167724 22 super()
c1a7ab7f 23
9e167724 24 // We limit the size of the requests
65fcc311
C
25 this.limitPods = REQUESTS_LIMIT_PODS
26 this.limitPerPod = REQUESTS_LIMIT_PER_POD
c1a7ab7f 27
9e167724 28 this.description = 'requests'
c1a7ab7f
C
29 }
30
9e167724
C
31 getRequestModel () {
32 return db.Request
c1a7ab7f
C
33 }
34
9e167724
C
35 getRequestToPodModel () {
36 return db.RequestToPod
c1a7ab7f
C
37 }
38
69818c93 39 buildRequestObjects (requests: { [ toPodId: number ]: any }) {
9e167724
C
40 const requestsToMakeGrouped = {}
41
42 Object.keys(requests).forEach(toPodId => {
43 requests[toPodId].forEach(data => {
44 const request = data.request
45 const pod = data.pod
46 const hashKey = toPodId + request.endpoint
47
48 if (!requestsToMakeGrouped[hashKey]) {
49 requestsToMakeGrouped[hashKey] = {
50 toPod: pod,
51 endpoint: request.endpoint,
52 ids: [], // request ids, to delete them from the DB in the future
53 datas: [] // requests data,
54 }
55 }
56
57 requestsToMakeGrouped[hashKey].ids.push(request.id)
58 requestsToMakeGrouped[hashKey].datas.push(request.request)
59 })
60 })
c1a7ab7f 61
9e167724 62 return requestsToMakeGrouped
c1a7ab7f
C
63 }
64
69818c93 65 createRequest ({ type, endpoint, data, toIds, transaction }: RequestSchedulerOptions, callback: (err: Error) => void) {
e02643f3
C
66 // TODO: check the setPods works
67 const podIds = []
c1a7ab7f
C
68
69 // If there are no destination pods abort
70 if (toIds.length === 0) return callback(null)
71
72 toIds.forEach(toPod => {
e02643f3 73 podIds.push(toPod)
c1a7ab7f
C
74 })
75
76 const createQuery = {
77 endpoint,
78 request: {
79 type: type,
80 data: data
81 }
82 }
83
69818c93 84 const dbRequestOptions: Sequelize.CreateOptions = {
c1a7ab7f
C
85 transaction
86 }
87
88 return db.Request.create(createQuery, dbRequestOptions).asCallback((err, request) => {
89 if (err) return callback(err)
90
e02643f3 91 return request.setPods(podIds, dbRequestOptions).asCallback(callback)
c1a7ab7f
C
92 })
93 }
94
95 // ---------------------------------------------------------------------------
96
9e167724
C
97 afterRequestsHook () {
98 // Flush requests with no pod
99 this.getRequestModel().removeWithEmptyTo(err => {
100 if (err) logger.error('Error when removing requests with no pods.', { error: err })
c1a7ab7f
C
101 })
102 }
103}
65fcc311
C
104
105// ---------------------------------------------------------------------------
106
107export {
108 RequestScheduler
109}