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