1 import * as Bull from 'bull'
3 ActivitypubFollowPayload,
4 ActivitypubHttpBroadcastPayload,
5 ActivitypubHttpFetcherPayload,
6 ActivitypubHttpUnicastPayload,
11 VideoFileImportPayload,
13 VideoRedundancyPayload,
14 VideoTranscodingPayload
15 } from '../../../shared/models'
16 import { logger } from '../../helpers/logger'
17 import { Redis } from '../redis'
18 import { JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_JOBS, WEBSERVER } from '../../initializers/constants'
19 import { processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast'
20 import { processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher'
21 import { processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast'
22 import { processEmail } from './handlers/email'
23 import { processVideoTranscoding } from './handlers/video-transcoding'
24 import { processActivityPubFollow } from './handlers/activitypub-follow'
25 import { processVideoImport } from './handlers/video-import'
26 import { processVideosViews } from './handlers/video-views'
27 import { refreshAPObject } from './handlers/activitypub-refresher'
28 import { processVideoFileImport } from './handlers/video-file-import'
29 import { processVideoRedundancy } from '@server/lib/job-queue/handlers/video-redundancy'
31 type CreateJobArgument =
32 { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } |
33 { type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload } |
34 { type: 'activitypub-http-fetcher', payload: ActivitypubHttpFetcherPayload } |
35 { type: 'activitypub-follow', payload: ActivitypubFollowPayload } |
36 { type: 'video-file-import', payload: VideoFileImportPayload } |
37 { type: 'video-transcoding', payload: VideoTranscodingPayload } |
38 { type: 'email', payload: EmailPayload } |
39 { type: 'video-import', payload: VideoImportPayload } |
40 { type: 'activitypub-refresher', payload: RefreshPayload } |
41 { type: 'videos-views', payload: {} } |
42 { type: 'video-redundancy', payload: VideoRedundancyPayload }
44 const handlers: { [id in JobType]: (job: Bull.Job) => Promise<any> } = {
45 'activitypub-http-broadcast': processActivityPubHttpBroadcast,
46 'activitypub-http-unicast': processActivityPubHttpUnicast,
47 'activitypub-http-fetcher': processActivityPubHttpFetcher,
48 'activitypub-follow': processActivityPubFollow,
49 'video-file-import': processVideoFileImport,
50 'video-transcoding': processVideoTranscoding,
51 'email': processEmail,
52 'video-import': processVideoImport,
53 'videos-views': processVideosViews,
54 'activitypub-refresher': refreshAPObject,
55 'video-redundancy': processVideoRedundancy
58 const jobTypes: JobType[] = [
60 'activitypub-http-broadcast',
61 'activitypub-http-fetcher',
62 'activitypub-http-unicast',
68 'activitypub-refresher',
74 private static instance: JobQueue
76 private queues: { [id in JobType]?: Bull.Queue } = {}
77 private initialized = false
78 private jobRedisPrefix: string
80 private constructor () {
84 // Already initialized
85 if (this.initialized === true) return
86 this.initialized = true
88 this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST
89 const queueOptions = {
90 prefix: this.jobRedisPrefix,
91 redis: Redis.getRedisClientOptions(),
93 maxStalledCount: 10 // transcoding could be long, so jobs can often be interrupted by restarts
97 for (const handlerName of Object.keys(handlers)) {
98 const queue = new Bull(handlerName, queueOptions)
99 const handler = handlers[handlerName]
101 queue.process(JOB_CONCURRENCY[handlerName], handler)
102 .catch(err => logger.error('Error in job queue processor %s.', handlerName, { err }))
104 queue.on('failed', (job, err) => {
105 logger.error('Cannot execute job %d in queue %s.', job.id, handlerName, { payload: job.data, err })
108 queue.on('error', err => {
109 logger.error('Error in job queue %s.', handlerName, { err })
112 this.queues[handlerName] = queue
115 this.addRepeatableJobs()
119 for (const queueName of Object.keys(this.queues)) {
120 const queue = this.queues[queueName]
125 createJob (obj: CreateJobArgument): void {
126 this.createJobWithPromise(obj)
127 .catch(err => logger.error('Cannot create job.', { err, obj }))
130 createJobWithPromise (obj: CreateJobArgument) {
131 const queue = this.queues[obj.type]
132 if (queue === undefined) {
133 logger.error('Unknown queue %s: cannot create job.', obj.type)
137 const jobArgs: Bull.JobOptions = {
138 backoff: { delay: 60 * 1000, type: 'exponential' },
139 attempts: JOB_ATTEMPTS[obj.type],
140 timeout: JOB_TTL[obj.type]
143 return queue.add(obj.payload, jobArgs)
146 async listForApi (options: {
152 }): Promise<Bull.Job[]> {
153 const { state, start, count, asc, jobType } = options
154 let results: Bull.Job[] = []
156 const filteredJobTypes = this.filterJobTypes(jobType)
158 for (const jobType of filteredJobTypes) {
159 const queue = this.queues[jobType]
160 if (queue === undefined) {
161 logger.error('Unknown queue %s to list jobs.', jobType)
165 const jobs = await queue.getJobs([ state ], 0, start + count, asc)
166 results = results.concat(jobs)
169 results.sort((j1: any, j2: any) => {
170 if (j1.timestamp < j2.timestamp) return -1
171 else if (j1.timestamp === j2.timestamp) return 0
176 if (asc === false) results.reverse()
178 return results.slice(start, start + count)
181 async count (state: JobState, jobType?: JobType): Promise<number> {
184 const filteredJobTypes = this.filterJobTypes(jobType)
186 for (const type of filteredJobTypes) {
187 const queue = this.queues[type]
188 if (queue === undefined) {
189 logger.error('Unknown queue %s to count jobs.', type)
193 const counts = await queue.getJobCounts()
195 total += counts[state]
201 async removeOldJobs () {
202 for (const key of Object.keys(this.queues)) {
203 const queue = this.queues[key]
204 await queue.clean(JOB_COMPLETED_LIFETIME, 'completed')
208 private addRepeatableJobs () {
209 this.queues['videos-views'].add({}, {
210 repeat: REPEAT_JOBS['videos-views']
211 }).catch(err => logger.error('Cannot add repeatable job.', { err }))
214 private filterJobTypes (jobType?: JobType) {
215 if (!jobType) return jobTypes
217 return jobTypes.filter(t => t === jobType)
220 static get Instance () {
221 return this.instance || (this.instance = new this())
225 // ---------------------------------------------------------------------------