]>
Commit | Line | Data |
---|---|---|
94831479 | 1 | import * as Bull from 'bull' |
402145b8 | 2 | import { jobStates } from '@server/helpers/custom-validators/jobs' |
9129b769 | 3 | import { CONFIG } from '@server/initializers/config' |
402145b8 | 4 | import { processVideoRedundancy } from '@server/lib/job-queue/handlers/video-redundancy' |
8dc8a34e C |
5 | import { |
6 | ActivitypubFollowPayload, | |
7 | ActivitypubHttpBroadcastPayload, | |
e1c55031 C |
8 | ActivitypubHttpFetcherPayload, |
9 | ActivitypubHttpUnicastPayload, | |
8795d6f2 | 10 | ActorKeysPayload, |
e1c55031 | 11 | EmailPayload, |
8dc8a34e | 12 | JobState, |
e1c55031 C |
13 | JobType, |
14 | RefreshPayload, | |
15 | VideoFileImportPayload, | |
16 | VideoImportPayload, | |
a5cf76af | 17 | VideoLiveEndingPayload, |
e1c55031 C |
18 | VideoRedundancyPayload, |
19 | VideoTranscodingPayload | |
8dc8a34e | 20 | } from '../../../shared/models' |
94a5ff8a | 21 | import { logger } from '../../helpers/logger' |
74dc3bca | 22 | import { JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_JOBS, WEBSERVER } from '../../initializers/constants' |
402145b8 C |
23 | import { Redis } from '../redis' |
24 | import { processActivityPubFollow } from './handlers/activitypub-follow' | |
8dc8a34e C |
25 | import { processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast' |
26 | import { processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher' | |
27 | import { processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast' | |
e1c55031 | 28 | import { refreshAPObject } from './handlers/activitypub-refresher' |
8795d6f2 | 29 | import { processActorKeys } from './handlers/actor-keys' |
402145b8 | 30 | import { processEmail } from './handlers/email' |
e1c55031 | 31 | import { processVideoFileImport } from './handlers/video-file-import' |
402145b8 | 32 | import { processVideoImport } from './handlers/video-import' |
a5cf76af | 33 | import { processVideoLiveEnding } from './handlers/video-live-ending' |
402145b8 C |
34 | import { processVideoTranscoding } from './handlers/video-transcoding' |
35 | import { processVideosViews } from './handlers/video-views' | |
94a5ff8a C |
36 | |
37 | type CreateJobArgument = | |
38 | { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } | | |
39 | { type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload } | | |
40 | { type: 'activitypub-http-fetcher', payload: ActivitypubHttpFetcherPayload } | | |
5350fd8e | 41 | { type: 'activitypub-follow', payload: ActivitypubFollowPayload } | |
28be8916 | 42 | { type: 'video-file-import', payload: VideoFileImportPayload } | |
a0327eed | 43 | { type: 'video-transcoding', payload: VideoTranscodingPayload } | |
fbad87b0 | 44 | { type: 'email', payload: EmailPayload } | |
6b616860 | 45 | { type: 'video-import', payload: VideoImportPayload } | |
04b8c3fb | 46 | { type: 'activitypub-refresher', payload: RefreshPayload } | |
b764380a | 47 | { type: 'videos-views', payload: {} } | |
a5cf76af | 48 | { type: 'video-live-ending', payload: VideoLiveEndingPayload } | |
8795d6f2 | 49 | { type: 'actor-keys', payload: ActorKeysPayload } | |
b764380a | 50 | { type: 'video-redundancy', payload: VideoRedundancyPayload } |
94a5ff8a | 51 | |
a5cf76af C |
52 | type CreateJobOptions = { |
53 | delay?: number | |
77d7e851 | 54 | priority?: number |
a5cf76af C |
55 | } |
56 | ||
a1587156 | 57 | const handlers: { [id in JobType]: (job: Bull.Job) => Promise<any> } = { |
94a5ff8a C |
58 | 'activitypub-http-broadcast': processActivityPubHttpBroadcast, |
59 | 'activitypub-http-unicast': processActivityPubHttpUnicast, | |
60 | 'activitypub-http-fetcher': processActivityPubHttpFetcher, | |
5350fd8e | 61 | 'activitypub-follow': processActivityPubFollow, |
28be8916 | 62 | 'video-file-import': processVideoFileImport, |
a0327eed | 63 | 'video-transcoding': processVideoTranscoding, |
fbad87b0 | 64 | 'email': processEmail, |
6b616860 | 65 | 'video-import': processVideoImport, |
04b8c3fb | 66 | 'videos-views': processVideosViews, |
b764380a | 67 | 'activitypub-refresher': refreshAPObject, |
a5cf76af | 68 | 'video-live-ending': processVideoLiveEnding, |
8795d6f2 | 69 | 'actor-keys': processActorKeys, |
b764380a | 70 | 'video-redundancy': processVideoRedundancy |
94a5ff8a C |
71 | } |
72 | ||
94831479 C |
73 | const jobTypes: JobType[] = [ |
74 | 'activitypub-follow', | |
71e3dfda | 75 | 'activitypub-http-broadcast', |
71e3dfda | 76 | 'activitypub-http-fetcher', |
94831479 C |
77 | 'activitypub-http-unicast', |
78 | 'email', | |
a0327eed | 79 | 'video-transcoding', |
fbad87b0 | 80 | 'video-file-import', |
6b616860 | 81 | 'video-import', |
04b8c3fb | 82 | 'videos-views', |
b764380a | 83 | 'activitypub-refresher', |
a5cf76af | 84 | 'video-redundancy', |
8795d6f2 | 85 | 'actor-keys', |
a5cf76af | 86 | 'video-live-ending' |
71e3dfda C |
87 | ] |
88 | ||
94a5ff8a C |
89 | class JobQueue { |
90 | ||
91 | private static instance: JobQueue | |
92 | ||
a1587156 | 93 | private queues: { [id in JobType]?: Bull.Queue } = {} |
94a5ff8a | 94 | private initialized = false |
2c29ad4f | 95 | private jobRedisPrefix: string |
94a5ff8a | 96 | |
a1587156 C |
97 | private constructor () { |
98 | } | |
94a5ff8a | 99 | |
a1587156 | 100 | init () { |
94a5ff8a C |
101 | // Already initialized |
102 | if (this.initialized === true) return | |
103 | this.initialized = true | |
104 | ||
6dd9de95 | 105 | this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST |
94831479 | 106 | const queueOptions = { |
2c29ad4f | 107 | prefix: this.jobRedisPrefix, |
47f6409b | 108 | redis: Redis.getRedisClientOptions(), |
4a9e71c2 C |
109 | settings: { |
110 | maxStalledCount: 10 // transcoding could be long, so jobs can often be interrupted by restarts | |
111 | } | |
94831479 | 112 | } |
ecb4e35f | 113 | |
9129b769 | 114 | for (const handlerName of (Object.keys(handlers) as JobType[])) { |
94831479 C |
115 | const queue = new Bull(handlerName, queueOptions) |
116 | const handler = handlers[handlerName] | |
94a5ff8a | 117 | |
9129b769 | 118 | queue.process(this.getJobConcurrency(handlerName), handler) |
2b86fe72 | 119 | .catch(err => logger.error('Error in job queue processor %s.', handlerName, { err })) |
d7f83948 C |
120 | |
121 | queue.on('failed', (job, err) => { | |
122 | logger.error('Cannot execute job %d in queue %s.', job.id, handlerName, { payload: job.data, err }) | |
123 | }) | |
3df45638 | 124 | |
94831479 C |
125 | queue.on('error', err => { |
126 | logger.error('Error in job queue %s.', handlerName, { err }) | |
94a5ff8a | 127 | }) |
94831479 C |
128 | |
129 | this.queues[handlerName] = queue | |
94a5ff8a | 130 | } |
6b616860 C |
131 | |
132 | this.addRepeatableJobs() | |
94a5ff8a C |
133 | } |
134 | ||
14f2b3ad C |
135 | terminate () { |
136 | for (const queueName of Object.keys(this.queues)) { | |
137 | const queue = this.queues[queueName] | |
138 | queue.close() | |
139 | } | |
140 | } | |
141 | ||
a5cf76af C |
142 | createJob (obj: CreateJobArgument, options: CreateJobOptions = {}): void { |
143 | this.createJobWithPromise(obj, options) | |
e1c55031 | 144 | .catch(err => logger.error('Cannot create job.', { err, obj })) |
a1587156 C |
145 | } |
146 | ||
a5cf76af | 147 | createJobWithPromise (obj: CreateJobArgument, options: CreateJobOptions = {}) { |
94831479 C |
148 | const queue = this.queues[obj.type] |
149 | if (queue === undefined) { | |
150 | logger.error('Unknown queue %s: cannot create job.', obj.type) | |
a1587156 | 151 | return |
94831479 | 152 | } |
94a5ff8a | 153 | |
94831479 C |
154 | const jobArgs: Bull.JobOptions = { |
155 | backoff: { delay: 60 * 1000, type: 'exponential' }, | |
2b86fe72 | 156 | attempts: JOB_ATTEMPTS[obj.type], |
a5cf76af | 157 | timeout: JOB_TTL[obj.type], |
77d7e851 | 158 | priority: options.priority, |
a5cf76af | 159 | delay: options.delay |
94831479 | 160 | } |
71e3dfda | 161 | |
94831479 | 162 | return queue.add(obj.payload, jobArgs) |
94a5ff8a C |
163 | } |
164 | ||
1061c73f | 165 | async listForApi (options: { |
402145b8 | 166 | state?: JobState |
a1587156 C |
167 | start: number |
168 | count: number | |
169 | asc?: boolean | |
1061c73f C |
170 | jobType: JobType |
171 | }): Promise<Bull.Job[]> { | |
402145b8 C |
172 | const { state, start, count, asc, jobType } = options |
173 | ||
174 | const states = state ? [ state ] : jobStates | |
94831479 | 175 | let results: Bull.Job[] = [] |
94a5ff8a | 176 | |
1061c73f C |
177 | const filteredJobTypes = this.filterJobTypes(jobType) |
178 | ||
1061c73f | 179 | for (const jobType of filteredJobTypes) { |
a1587156 | 180 | const queue = this.queues[jobType] |
94831479 C |
181 | if (queue === undefined) { |
182 | logger.error('Unknown queue %s to list jobs.', jobType) | |
183 | continue | |
184 | } | |
2c29ad4f | 185 | |
402145b8 | 186 | const jobs = await queue.getJobs(states, 0, start + count, asc) |
94831479 C |
187 | results = results.concat(jobs) |
188 | } | |
94a5ff8a | 189 | |
94831479 C |
190 | results.sort((j1: any, j2: any) => { |
191 | if (j1.timestamp < j2.timestamp) return -1 | |
192 | else if (j1.timestamp === j2.timestamp) return 0 | |
94a5ff8a | 193 | |
94831479 | 194 | return 1 |
94a5ff8a | 195 | }) |
94a5ff8a | 196 | |
94831479 | 197 | if (asc === false) results.reverse() |
94a5ff8a | 198 | |
94831479 | 199 | return results.slice(start, start + count) |
94a5ff8a C |
200 | } |
201 | ||
402145b8 C |
202 | async count (state: JobState, jobType?: JobType): Promise<number> { |
203 | const states = state ? [ state ] : jobStates | |
94831479 | 204 | let total = 0 |
3df45638 | 205 | |
1061c73f C |
206 | const filteredJobTypes = this.filterJobTypes(jobType) |
207 | ||
208 | for (const type of filteredJobTypes) { | |
a1587156 | 209 | const queue = this.queues[type] |
94831479 C |
210 | if (queue === undefined) { |
211 | logger.error('Unknown queue %s to count jobs.', type) | |
212 | continue | |
213 | } | |
3df45638 | 214 | |
94831479 | 215 | const counts = await queue.getJobCounts() |
3df45638 | 216 | |
040d6896 RK |
217 | for (const s of states) { |
218 | total += counts[s] | |
219 | } | |
94831479 | 220 | } |
3df45638 | 221 | |
94831479 | 222 | return total |
3df45638 C |
223 | } |
224 | ||
2f5c6b2f | 225 | async removeOldJobs () { |
94831479 C |
226 | for (const key of Object.keys(this.queues)) { |
227 | const queue = this.queues[key] | |
2f5c6b2f | 228 | await queue.clean(JOB_COMPLETED_LIFETIME, 'completed') |
94831479 | 229 | } |
2c29ad4f C |
230 | } |
231 | ||
6b616860 C |
232 | private addRepeatableJobs () { |
233 | this.queues['videos-views'].add({}, { | |
234 | repeat: REPEAT_JOBS['videos-views'] | |
a1587156 | 235 | }).catch(err => logger.error('Cannot add repeatable job.', { err })) |
6b616860 C |
236 | } |
237 | ||
1061c73f C |
238 | private filterJobTypes (jobType?: JobType) { |
239 | if (!jobType) return jobTypes | |
240 | ||
241 | return jobTypes.filter(t => t === jobType) | |
242 | } | |
243 | ||
9129b769 C |
244 | private getJobConcurrency (jobType: JobType) { |
245 | if (jobType === 'video-transcoding') return CONFIG.TRANSCODING.CONCURRENCY | |
246 | if (jobType === 'video-import') return CONFIG.IMPORT.VIDEOS.CONCURRENCY | |
247 | ||
248 | return JOB_CONCURRENCY[jobType] | |
249 | } | |
250 | ||
94a5ff8a C |
251 | static get Instance () { |
252 | return this.instance || (this.instance = new this()) | |
253 | } | |
254 | } | |
255 | ||
256 | // --------------------------------------------------------------------------- | |
257 | ||
258 | export { | |
1061c73f | 259 | jobTypes, |
94a5ff8a C |
260 | JobQueue |
261 | } |