]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/job-queue.ts
Migrate to bull
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / job-queue.ts
1 import * as Bull from 'bull'
2 import { JobState, JobType } from '../../../shared/models'
3 import { logger } from '../../helpers/logger'
4 import { CONFIG, JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_REQUEST_TTL } from '../../initializers'
5 import { ActivitypubHttpBroadcastPayload, processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast'
6 import { ActivitypubHttpFetcherPayload, processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher'
7 import { ActivitypubHttpUnicastPayload, processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast'
8 import { EmailPayload, processEmail } from './handlers/email'
9 import { processVideoFile, processVideoFileImport, VideoFileImportPayload, VideoFilePayload } from './handlers/video-file'
10 import { ActivitypubFollowPayload, processActivityPubFollow } from './handlers/activitypub-follow'
11
12 type CreateJobArgument =
13 { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } |
14 { type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload } |
15 { type: 'activitypub-http-fetcher', payload: ActivitypubHttpFetcherPayload } |
16 { type: 'activitypub-follow', payload: ActivitypubFollowPayload } |
17 { type: 'video-file-import', payload: VideoFileImportPayload } |
18 { type: 'video-file', payload: VideoFilePayload } |
19 { type: 'email', payload: EmailPayload }
20
21 const handlers: { [ id in JobType ]: (job: Bull.Job) => Promise<any>} = {
22 'activitypub-http-broadcast': processActivityPubHttpBroadcast,
23 'activitypub-http-unicast': processActivityPubHttpUnicast,
24 'activitypub-http-fetcher': processActivityPubHttpFetcher,
25 'activitypub-follow': processActivityPubFollow,
26 'video-file-import': processVideoFileImport,
27 'video-file': processVideoFile,
28 'email': processEmail
29 }
30
31 const jobsWithRequestTimeout: { [ id in JobType ]?: boolean } = {
32 'activitypub-http-broadcast': true,
33 'activitypub-http-unicast': true,
34 'activitypub-http-fetcher': true,
35 'activitypub-follow': true
36 }
37
38 const jobTypes: JobType[] = [
39 'activitypub-follow',
40 'activitypub-http-broadcast',
41 'activitypub-http-fetcher',
42 'activitypub-http-unicast',
43 'email',
44 'video-file',
45 'video-file-import'
46 ]
47
48 class JobQueue {
49
50 private static instance: JobQueue
51
52 private queues: { [ id in JobType ]?: Bull.Queue } = {}
53 private initialized = false
54 private jobRedisPrefix: string
55
56 private constructor () {}
57
58 async init () {
59 // Already initialized
60 if (this.initialized === true) return
61 this.initialized = true
62
63 this.jobRedisPrefix = 'bull-' + CONFIG.WEBSERVER.HOST
64 const queueOptions = {
65 prefix: this.jobRedisPrefix,
66 redis: {
67 host: CONFIG.REDIS.HOSTNAME,
68 port: CONFIG.REDIS.PORT,
69 auth: CONFIG.REDIS.AUTH,
70 db: CONFIG.REDIS.DB
71 }
72 }
73
74 for (const handlerName of Object.keys(handlers)) {
75 const queue = new Bull(handlerName, queueOptions)
76 const handler = handlers[handlerName]
77
78 queue.process(JOB_CONCURRENCY[handlerName], handler)
79 .catch(err => logger.error('Cannot execute job queue %s.', handlerName, { err }))
80
81 queue.on('error', err => {
82 logger.error('Error in job queue %s.', handlerName, { err })
83 process.exit(-1)
84 })
85
86 this.queues[handlerName] = queue
87 }
88 }
89
90 createJob (obj: CreateJobArgument) {
91 const queue = this.queues[obj.type]
92 if (queue === undefined) {
93 logger.error('Unknown queue %s: cannot create job.', obj.type)
94 return
95 }
96
97 const jobArgs: Bull.JobOptions = {
98 backoff: { delay: 60 * 1000, type: 'exponential' },
99 attempts: JOB_ATTEMPTS[obj.type]
100 }
101
102 if (jobsWithRequestTimeout[obj.type] === true) {
103 jobArgs.timeout = JOB_REQUEST_TTL
104 }
105
106 return queue.add(obj.payload, jobArgs)
107 }
108
109 async listForApi (state: JobState, start: number, count: number, asc?: boolean): Promise<Bull.Job[]> {
110 let results: Bull.Job[] = []
111
112 // TODO: optimize
113 for (const jobType of jobTypes) {
114 const queue = this.queues[ jobType ]
115 if (queue === undefined) {
116 logger.error('Unknown queue %s to list jobs.', jobType)
117 continue
118 }
119
120 // FIXME: Bull queue typings does not have getJobs method
121 const jobs = await (queue as any).getJobs(state, 0, start + count, asc)
122 results = results.concat(jobs)
123 }
124
125 results.sort((j1: any, j2: any) => {
126 if (j1.timestamp < j2.timestamp) return -1
127 else if (j1.timestamp === j2.timestamp) return 0
128
129 return 1
130 })
131
132 if (asc === false) results.reverse()
133
134 return results.slice(start, start + count)
135 }
136
137 async count (state: JobState): Promise<number> {
138 let total = 0
139
140 for (const type of jobTypes) {
141 const queue = this.queues[ type ]
142 if (queue === undefined) {
143 logger.error('Unknown queue %s to count jobs.', type)
144 continue
145 }
146
147 const counts = await queue.getJobCounts()
148
149 total += counts[ state ]
150 }
151
152 return total
153 }
154
155 removeOldJobs () {
156 for (const key of Object.keys(this.queues)) {
157 const queue = this.queues[key]
158 queue.clean(JOB_COMPLETED_LIFETIME, 'completed')
159 }
160 }
161
162 static get Instance () {
163 return this.instance || (this.instance = new this())
164 }
165 }
166
167 // ---------------------------------------------------------------------------
168
169 export {
170 JobQueue
171 }