]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/job-queue.ts
Prevent job failure on concurrent HLS transcoding
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / job-queue.ts
CommitLineData
5a921e7b 1import {
bd911b54
C
2 FlowJob,
3 FlowProducer,
5a921e7b
C
4 Job,
5 JobsOptions,
6 Queue,
7 QueueEvents,
8 QueueEventsOptions,
9 QueueOptions,
10 QueueScheduler,
11 QueueSchedulerOptions,
12 Worker,
13 WorkerOptions
14} from 'bullmq'
402145b8 15import { jobStates } from '@server/helpers/custom-validators/jobs'
9129b769 16import { CONFIG } from '@server/initializers/config'
402145b8 17import { processVideoRedundancy } from '@server/lib/job-queue/handlers/video-redundancy'
bd911b54 18import { pick, timeoutPromise } from '@shared/core-utils'
8dc8a34e
C
19import {
20 ActivitypubFollowPayload,
21 ActivitypubHttpBroadcastPayload,
e1c55031
C
22 ActivitypubHttpFetcherPayload,
23 ActivitypubHttpUnicastPayload,
8795d6f2 24 ActorKeysPayload,
276250f0 25 DeleteResumableUploadMetaFilePayload,
e1c55031 26 EmailPayload,
bd911b54 27 FederateVideoPayload,
8dc8a34e 28 JobState,
e1c55031 29 JobType,
f012319a 30 ManageVideoTorrentPayload,
0305db28 31 MoveObjectStoragePayload,
bd911b54 32 NotifyPayload,
e1c55031
C
33 RefreshPayload,
34 VideoFileImportPayload,
35 VideoImportPayload,
a5cf76af 36 VideoLiveEndingPayload,
e1c55031 37 VideoRedundancyPayload,
92e66e04 38 VideoStudioEditionPayload,
e1c55031 39 VideoTranscodingPayload
8dc8a34e 40} from '../../../shared/models'
94a5ff8a 41import { logger } from '../../helpers/logger'
74dc3bca 42import { JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_JOBS, WEBSERVER } from '../../initializers/constants'
22df69fd 43import { Hooks } from '../plugins/hooks'
74d249bc 44import { processActivityPubCleaner } from './handlers/activitypub-cleaner'
402145b8 45import { processActivityPubFollow } from './handlers/activitypub-follow'
8dc8a34e
C
46import { processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast'
47import { processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher'
48import { processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast'
e1c55031 49import { refreshAPObject } from './handlers/activitypub-refresher'
8795d6f2 50import { processActorKeys } from './handlers/actor-keys'
402145b8 51import { processEmail } from './handlers/email'
bd911b54 52import { processFederateVideo } from './handlers/federate-video'
f012319a 53import { processManageVideoTorrent } from './handlers/manage-video-torrent'
32567717 54import { onMoveToObjectStorageFailure, processMoveToObjectStorage } from './handlers/move-to-object-storage'
bd911b54 55import { processNotify } from './handlers/notify'
e1c55031 56import { processVideoFileImport } from './handlers/video-file-import'
402145b8 57import { processVideoImport } from './handlers/video-import'
a5cf76af 58import { processVideoLiveEnding } from './handlers/video-live-ending'
92e66e04 59import { processVideoStudioEdition } from './handlers/video-studio-edition'
402145b8 60import { processVideoTranscoding } from './handlers/video-transcoding'
51353d9a 61import { processVideosViewsStats } from './handlers/video-views-stats'
94a5ff8a 62
bd911b54 63export type CreateJobArgument =
94a5ff8a 64 { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } |
f27b7a75 65 { type: 'activitypub-http-broadcast-parallel', payload: ActivitypubHttpBroadcastPayload } |
94a5ff8a
C
66 { type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload } |
67 { type: 'activitypub-http-fetcher', payload: ActivitypubHttpFetcherPayload } |
74d249bc 68 { type: 'activitypub-http-cleaner', payload: {} } |
5350fd8e 69 { type: 'activitypub-follow', payload: ActivitypubFollowPayload } |
28be8916 70 { type: 'video-file-import', payload: VideoFileImportPayload } |
a0327eed 71 { type: 'video-transcoding', payload: VideoTranscodingPayload } |
fbad87b0 72 { type: 'email', payload: EmailPayload } |
6b616860 73 { type: 'video-import', payload: VideoImportPayload } |
04b8c3fb 74 { type: 'activitypub-refresher', payload: RefreshPayload } |
51353d9a 75 { type: 'videos-views-stats', payload: {} } |
a5cf76af 76 { type: 'video-live-ending', payload: VideoLiveEndingPayload } |
8795d6f2 77 { type: 'actor-keys', payload: ActorKeysPayload } |
0305db28 78 { type: 'video-redundancy', payload: VideoRedundancyPayload } |
276250f0 79 { type: 'delete-resumable-upload-meta-file', payload: DeleteResumableUploadMetaFilePayload } |
92e66e04 80 { type: 'video-studio-edition', payload: VideoStudioEditionPayload } |
f012319a 81 { type: 'manage-video-torrent', payload: ManageVideoTorrentPayload } |
bd911b54
C
82 { type: 'notify', payload: NotifyPayload } |
83 { type: 'move-to-object-storage', payload: MoveObjectStoragePayload } |
84 { type: 'federate-video', payload: FederateVideoPayload }
94a5ff8a 85
0305db28 86export type CreateJobOptions = {
a5cf76af 87 delay?: number
77d7e851 88 priority?: number
a5cf76af
C
89}
90
41fb13c3 91const handlers: { [id in JobType]: (job: Job) => Promise<any> } = {
94a5ff8a 92 'activitypub-http-broadcast': processActivityPubHttpBroadcast,
f27b7a75 93 'activitypub-http-broadcast-parallel': processActivityPubHttpBroadcast,
94a5ff8a
C
94 'activitypub-http-unicast': processActivityPubHttpUnicast,
95 'activitypub-http-fetcher': processActivityPubHttpFetcher,
74d249bc 96 'activitypub-cleaner': processActivityPubCleaner,
5350fd8e 97 'activitypub-follow': processActivityPubFollow,
28be8916 98 'video-file-import': processVideoFileImport,
a0327eed 99 'video-transcoding': processVideoTranscoding,
fbad87b0 100 'email': processEmail,
6b616860 101 'video-import': processVideoImport,
51353d9a 102 'videos-views-stats': processVideosViewsStats,
b764380a 103 'activitypub-refresher': refreshAPObject,
a5cf76af 104 'video-live-ending': processVideoLiveEnding,
8795d6f2 105 'actor-keys': processActorKeys,
0305db28 106 'video-redundancy': processVideoRedundancy,
c729caf6 107 'move-to-object-storage': processMoveToObjectStorage,
f012319a 108 'manage-video-torrent': processManageVideoTorrent,
bd911b54
C
109 'notify': processNotify,
110 'video-studio-edition': processVideoStudioEdition,
111 'federate-video': processFederateVideo
94a5ff8a
C
112}
113
32567717
C
114const errorHandlers: { [id in JobType]?: (job: Job, err: any) => Promise<any> } = {
115 'move-to-object-storage': onMoveToObjectStorageFailure
116}
117
94831479
C
118const jobTypes: JobType[] = [
119 'activitypub-follow',
71e3dfda 120 'activitypub-http-broadcast',
f27b7a75 121 'activitypub-http-broadcast-parallel',
71e3dfda 122 'activitypub-http-fetcher',
94831479 123 'activitypub-http-unicast',
74d249bc 124 'activitypub-cleaner',
94831479 125 'email',
a0327eed 126 'video-transcoding',
fbad87b0 127 'video-file-import',
6b616860 128 'video-import',
51353d9a 129 'videos-views-stats',
b764380a 130 'activitypub-refresher',
a5cf76af 131 'video-redundancy',
8795d6f2 132 'actor-keys',
0305db28 133 'video-live-ending',
c729caf6 134 'move-to-object-storage',
f012319a 135 'manage-video-torrent',
bd911b54
C
136 'video-studio-edition',
137 'notify',
138 'federate-video'
71e3dfda
C
139]
140
941d28cc
C
141const silentFailure = new Set<JobType>([ 'activitypub-http-unicast' ])
142
94a5ff8a
C
143class JobQueue {
144
145 private static instance: JobQueue
146
5a921e7b 147 private workers: { [id in JobType]?: Worker } = {}
41fb13c3 148 private queues: { [id in JobType]?: Queue } = {}
5a921e7b
C
149 private queueSchedulers: { [id in JobType]?: QueueScheduler } = {}
150 private queueEvents: { [id in JobType]?: QueueEvents } = {}
151
bd911b54
C
152 private flowProducer: FlowProducer
153
94a5ff8a 154 private initialized = false
2c29ad4f 155 private jobRedisPrefix: string
94a5ff8a 156
a1587156
C
157 private constructor () {
158 }
94a5ff8a 159
e1ab52d7 160 init (produceOnly = false) {
94a5ff8a
C
161 // Already initialized
162 if (this.initialized === true) return
163 this.initialized = true
164
6dd9de95 165 this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST
ff4d2c73 166
5a921e7b
C
167 for (const handlerName of (Object.keys(handlers) as JobType[])) {
168 this.buildWorker(handlerName, produceOnly)
169 this.buildQueue(handlerName)
170 this.buildQueueScheduler(handlerName, produceOnly)
171 this.buildQueueEvent(handlerName, produceOnly)
172 }
173
bd911b54
C
174 this.flowProducer = new FlowProducer({
175 connection: this.getRedisConnection(),
176 prefix: this.jobRedisPrefix
177 })
178
5a921e7b
C
179 this.addRepeatableJobs()
180 }
181
182 private buildWorker (handlerName: JobType, produceOnly: boolean) {
183 const workerOptions: WorkerOptions = {
184 autorun: !produceOnly,
185 concurrency: this.getJobConcurrency(handlerName),
2c29ad4f 186 prefix: this.jobRedisPrefix,
5a921e7b 187 connection: this.getRedisConnection()
94831479 188 }
ecb4e35f 189
5a921e7b
C
190 const handler = function (job: Job) {
191 const timeout = JOB_TTL[handlerName]
192 const p = handlers[handlerName](job)
e1ab52d7 193
5a921e7b 194 if (!timeout) return p
e1ab52d7 195
5a921e7b
C
196 return timeoutPromise(p, timeout)
197 }
94a5ff8a 198
5a921e7b
C
199 const processor = async (jobArg: Job<any>) => {
200 const job = await Hooks.wrapObject(jobArg, 'filter:job-queue.process.params', { type: handlerName })
22df69fd 201
5a921e7b
C
202 return Hooks.wrapPromiseFun(handler, job, 'filter:job-queue.process.result')
203 }
d7f83948 204
5a921e7b 205 const worker = new Worker(handlerName, processor, workerOptions)
941d28cc 206
5a921e7b
C
207 worker.on('failed', (job, err) => {
208 const logLevel = silentFailure.has(handlerName)
209 ? 'debug'
210 : 'error'
32567717 211
5a921e7b 212 logger.log(logLevel, 'Cannot execute job %s in queue %s.', job.id, handlerName, { payload: job.data, err })
3df45638 213
5a921e7b
C
214 if (errorHandlers[job.name]) {
215 errorHandlers[job.name](job, err)
216 .catch(err => logger.error('Cannot run error handler for job failure %d in queue %s.', job.id, handlerName, { err }))
217 }
218 })
94831479 219
5a921e7b
C
220 worker.on('error', err => {
221 logger.error('Error in job queue %s.', handlerName, { err })
222 })
223
224 this.workers[handlerName] = worker
225 }
226
227 private buildQueue (handlerName: JobType) {
228 const queueOptions: QueueOptions = {
229 connection: this.getRedisConnection(),
230 prefix: this.jobRedisPrefix
94a5ff8a 231 }
6b616860 232
5a921e7b
C
233 this.queues[handlerName] = new Queue(handlerName, queueOptions)
234 }
235
236 private buildQueueScheduler (handlerName: JobType, produceOnly: boolean) {
237 const queueSchedulerOptions: QueueSchedulerOptions = {
238 autorun: !produceOnly,
239 connection: this.getRedisConnection(),
240 prefix: this.jobRedisPrefix,
241 maxStalledCount: 10
242 }
243 this.queueSchedulers[handlerName] = new QueueScheduler(handlerName, queueSchedulerOptions)
94a5ff8a
C
244 }
245
5a921e7b
C
246 private buildQueueEvent (handlerName: JobType, produceOnly: boolean) {
247 const queueEventsOptions: QueueEventsOptions = {
248 autorun: !produceOnly,
249 connection: this.getRedisConnection(),
250 prefix: this.jobRedisPrefix
14f2b3ad 251 }
5a921e7b
C
252 this.queueEvents[handlerName] = new QueueEvents(handlerName, queueEventsOptions)
253 }
254
255 private getRedisConnection () {
256 return {
257 password: CONFIG.REDIS.AUTH,
258 db: CONFIG.REDIS.DB,
259 host: CONFIG.REDIS.HOSTNAME,
260 port: CONFIG.REDIS.PORT,
261 path: CONFIG.REDIS.SOCKET
262 }
263 }
264
bd911b54
C
265 // ---------------------------------------------------------------------------
266
5a921e7b
C
267 async terminate () {
268 const promises = Object.keys(this.workers)
269 .map(handlerName => {
270 const worker: Worker = this.workers[handlerName]
271 const queue: Queue = this.queues[handlerName]
272 const queueScheduler: QueueScheduler = this.queueSchedulers[handlerName]
273 const queueEvent: QueueEvents = this.queueEvents[handlerName]
274
275 return Promise.all([
276 worker.close(false),
277 queue.close(),
278 queueScheduler.close(),
279 queueEvent.close()
280 ])
281 })
282
283 return Promise.all(promises)
14f2b3ad
C
284 }
285
419b520c 286 async pause () {
e2b2c726
C
287 for (const handlerName of Object.keys(this.workers)) {
288 const worker: Worker = this.workers[handlerName]
5a921e7b
C
289
290 await worker.pause()
419b520c
C
291 }
292 }
293
51335c72 294 resume () {
e2b2c726
C
295 for (const handlerName of Object.keys(this.workers)) {
296 const worker: Worker = this.workers[handlerName]
5a921e7b
C
297
298 worker.resume()
419b520c
C
299 }
300 }
301
bd911b54
C
302 // ---------------------------------------------------------------------------
303
304 createJobAsync (options: CreateJobArgument & CreateJobOptions): void {
305 this.createJob(options)
306 .catch(err => logger.error('Cannot create job.', { err, options }))
a1587156
C
307 }
308
bd911b54
C
309 async createJob (options: CreateJobArgument & CreateJobOptions) {
310 const queue: Queue = this.queues[options.type]
94831479 311 if (queue === undefined) {
bd911b54 312 logger.error('Unknown queue %s: cannot create job.', options.type)
a1587156 313 return
94831479 314 }
94a5ff8a 315
bd911b54
C
316 const jobOptions = this.buildJobOptions(options.type as JobType, pick(options, [ 'priority', 'delay' ]))
317
318 return queue.add('job', options.payload, jobOptions)
319 }
320
321 async createSequentialJobFlow (...jobs: ((CreateJobArgument & CreateJobOptions) | undefined)[]) {
322 let lastJob: FlowJob
323
324 for (const job of jobs) {
325 if (!job) continue
326
327 lastJob = {
b42c2c7e
C
328 ...this.buildJobFlowOption(job),
329
bd911b54
C
330 children: lastJob
331 ? [ lastJob ]
332 : []
333 }
334 }
335
336 return this.flowProducer.add(lastJob)
337 }
338
b42c2c7e
C
339 async createJobWithChildren (parent: CreateJobArgument & CreateJobOptions, children: (CreateJobArgument & CreateJobOptions)[]) {
340 return this.flowProducer.add({
341 ...this.buildJobFlowOption(parent),
342
343 children: children.map(c => this.buildJobFlowOption(c))
344 })
345 }
346
347 private buildJobFlowOption (job: CreateJobArgument & CreateJobOptions) {
348 return {
349 name: 'job',
350 data: job.payload,
351 queueName: job.type,
352 opts: this.buildJobOptions(job.type as JobType, pick(job, [ 'priority', 'delay' ]))
353 }
354 }
355
bd911b54
C
356 private buildJobOptions (type: JobType, options: CreateJobOptions = {}): JobsOptions {
357 return {
94831479 358 backoff: { delay: 60 * 1000, type: 'exponential' },
bd911b54 359 attempts: JOB_ATTEMPTS[type],
77d7e851 360 priority: options.priority,
a5cf76af 361 delay: options.delay
94831479 362 }
94a5ff8a
C
363 }
364
bd911b54
C
365 // ---------------------------------------------------------------------------
366
1061c73f 367 async listForApi (options: {
402145b8 368 state?: JobState
a1587156
C
369 start: number
370 count: number
371 asc?: boolean
1061c73f 372 jobType: JobType
41fb13c3 373 }): Promise<Job[]> {
402145b8
C
374 const { state, start, count, asc, jobType } = options
375
e2b2c726
C
376 const states = this.buildStateFilter(state)
377 const filteredJobTypes = this.buildTypeFilter(jobType)
94a5ff8a 378
e2b2c726 379 let results: Job[] = []
1061c73f 380
1061c73f 381 for (const jobType of filteredJobTypes) {
5a921e7b
C
382 const queue: Queue = this.queues[jobType]
383
94831479
C
384 if (queue === undefined) {
385 logger.error('Unknown queue %s to list jobs.', jobType)
386 continue
387 }
2c29ad4f 388
402145b8 389 const jobs = await queue.getJobs(states, 0, start + count, asc)
94831479
C
390 results = results.concat(jobs)
391 }
94a5ff8a 392
94831479
C
393 results.sort((j1: any, j2: any) => {
394 if (j1.timestamp < j2.timestamp) return -1
395 else if (j1.timestamp === j2.timestamp) return 0
94a5ff8a 396
94831479 397 return 1
94a5ff8a 398 })
94a5ff8a 399
94831479 400 if (asc === false) results.reverse()
94a5ff8a 401
94831479 402 return results.slice(start, start + count)
94a5ff8a
C
403 }
404
402145b8
C
405 async count (state: JobState, jobType?: JobType): Promise<number> {
406 const states = state ? [ state ] : jobStates
e2b2c726 407 const filteredJobTypes = this.buildTypeFilter(jobType)
3df45638 408
e2b2c726 409 let total = 0
1061c73f
C
410
411 for (const type of filteredJobTypes) {
a1587156 412 const queue = this.queues[type]
94831479
C
413 if (queue === undefined) {
414 logger.error('Unknown queue %s to count jobs.', type)
415 continue
416 }
3df45638 417
94831479 418 const counts = await queue.getJobCounts()
3df45638 419
040d6896
RK
420 for (const s of states) {
421 total += counts[s]
422 }
94831479 423 }
3df45638 424
94831479 425 return total
3df45638
C
426 }
427
e2b2c726
C
428 private buildStateFilter (state?: JobState) {
429 if (!state) return jobStates
430
431 const states = [ state ]
432
433 // Include parent if filtering on waiting
434 if (state === 'waiting') states.push('waiting-children')
435
436 return states
437 }
438
439 private buildTypeFilter (jobType?: JobType) {
440 if (!jobType) return jobTypes
441
442 return jobTypes.filter(t => t === jobType)
443 }
444
630d0a1b
C
445 async getStats () {
446 const promises = jobTypes.map(async t => ({ jobType: t, counts: await this.queues[t].getJobCounts() }))
447
448 return Promise.all(promises)
449 }
450
bd911b54
C
451 // ---------------------------------------------------------------------------
452
2f5c6b2f 453 async removeOldJobs () {
94831479 454 for (const key of Object.keys(this.queues)) {
5a921e7b
C
455 const queue: Queue = this.queues[key]
456 await queue.clean(JOB_COMPLETED_LIFETIME, 100, 'completed')
94831479 457 }
2c29ad4f
C
458 }
459
6b616860 460 private addRepeatableJobs () {
5a921e7b 461 this.queues['videos-views-stats'].add('job', {}, {
51353d9a 462 repeat: REPEAT_JOBS['videos-views-stats']
a1587156 463 }).catch(err => logger.error('Cannot add repeatable job.', { err }))
74d249bc
C
464
465 if (CONFIG.FEDERATION.VIDEOS.CLEANUP_REMOTE_INTERACTIONS) {
5a921e7b 466 this.queues['activitypub-cleaner'].add('job', {}, {
74d249bc
C
467 repeat: REPEAT_JOBS['activitypub-cleaner']
468 }).catch(err => logger.error('Cannot add repeatable job.', { err }))
469 }
6b616860
C
470 }
471
9129b769
C
472 private getJobConcurrency (jobType: JobType) {
473 if (jobType === 'video-transcoding') return CONFIG.TRANSCODING.CONCURRENCY
474 if (jobType === 'video-import') return CONFIG.IMPORT.VIDEOS.CONCURRENCY
475
476 return JOB_CONCURRENCY[jobType]
477 }
478
94a5ff8a
C
479 static get Instance () {
480 return this.instance || (this.instance = new this())
481 }
482}
483
484// ---------------------------------------------------------------------------
485
486export {
1061c73f 487 jobTypes,
94a5ff8a
C
488 JobQueue
489}