X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fjob-queue%2Fjob-queue.ts;h=866aa1ed0eef876a9e45b719f3b12e753ec9e450;hb=60b880acdfa85eab5c9ec09ba1283f82ae58ec85;hp=ce24763f13ad06a5978349727eccd866e6582a51;hpb=714e33a7428b71ef98129ce85a4bd64140bcd912;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/job-queue/job-queue.ts b/server/lib/job-queue/job-queue.ts index ce24763f1..866aa1ed0 100644 --- a/server/lib/job-queue/job-queue.ts +++ b/server/lib/job-queue/job-queue.ts @@ -1,20 +1,38 @@ -import Bull, { Job, JobOptions, Queue } from 'bull' +import { + FlowJob, + FlowProducer, + Job, + JobsOptions, + Queue, + QueueEvents, + QueueEventsOptions, + QueueOptions, + QueueScheduler, + QueueSchedulerOptions, + Worker, + WorkerOptions +} from 'bullmq' import { jobStates } from '@server/helpers/custom-validators/jobs' import { CONFIG } from '@server/initializers/config' import { processVideoRedundancy } from '@server/lib/job-queue/handlers/video-redundancy' +import { pick, timeoutPromise } from '@shared/core-utils' import { ActivitypubFollowPayload, ActivitypubHttpBroadcastPayload, ActivitypubHttpFetcherPayload, ActivitypubHttpUnicastPayload, ActorKeysPayload, + AfterVideoChannelImportPayload, DeleteResumableUploadMetaFilePayload, EmailPayload, + FederateVideoPayload, JobState, JobType, ManageVideoTorrentPayload, MoveObjectStoragePayload, + NotifyPayload, RefreshPayload, + VideoChannelImportPayload, VideoFileImportPayload, VideoImportPayload, VideoLiveEndingPayload, @@ -23,25 +41,39 @@ import { VideoTranscodingPayload } from '../../../shared/models' import { logger } from '../../helpers/logger' -import { JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_JOBS, WEBSERVER } from '../../initializers/constants' +import { + JOB_ATTEMPTS, + JOB_CONCURRENCY, + JOB_REMOVAL_OPTIONS, + JOB_TTL, + REPEAT_JOBS, + WEBSERVER +} from '../../initializers/constants' +import { Hooks } from '../plugins/hooks' +import { Redis } from '../redis' import { processActivityPubCleaner } from './handlers/activitypub-cleaner' import { processActivityPubFollow } from './handlers/activitypub-follow' -import { processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast' +import { processActivityPubHttpSequentialBroadcast, processActivityPubParallelHttpBroadcast } from './handlers/activitypub-http-broadcast' import { processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher' import { processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast' import { refreshAPObject } from './handlers/activitypub-refresher' import { processActorKeys } from './handlers/actor-keys' +import { processAfterVideoChannelImport } from './handlers/after-video-channel-import' import { processEmail } from './handlers/email' +import { processFederateVideo } from './handlers/federate-video' import { processManageVideoTorrent } from './handlers/manage-video-torrent' import { onMoveToObjectStorageFailure, processMoveToObjectStorage } from './handlers/move-to-object-storage' +import { processNotify } from './handlers/notify' +import { processVideoChannelImport } from './handlers/video-channel-import' import { processVideoFileImport } from './handlers/video-file-import' import { processVideoImport } from './handlers/video-import' import { processVideoLiveEnding } from './handlers/video-live-ending' import { processVideoStudioEdition } from './handlers/video-studio-edition' import { processVideoTranscoding } from './handlers/video-transcoding' import { processVideosViewsStats } from './handlers/video-views-stats' +import { parseDurationToMs } from '@server/helpers/core-utils' -type CreateJobArgument = +export type CreateJobArgument = { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } | { type: 'activitypub-http-broadcast-parallel', payload: ActivitypubHttpBroadcastPayload } | { type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload } | @@ -60,7 +92,12 @@ type CreateJobArgument = { type: 'delete-resumable-upload-meta-file', payload: DeleteResumableUploadMetaFilePayload } | { type: 'video-studio-edition', payload: VideoStudioEditionPayload } | { type: 'manage-video-torrent', payload: ManageVideoTorrentPayload } | - { type: 'move-to-object-storage', payload: MoveObjectStoragePayload } + { type: 'move-to-object-storage', payload: MoveObjectStoragePayload } | + { type: 'video-channel-import', payload: VideoChannelImportPayload } | + { type: 'after-video-channel-import', payload: AfterVideoChannelImportPayload } | + { type: 'notify', payload: NotifyPayload } | + { type: 'move-to-object-storage', payload: MoveObjectStoragePayload } | + { type: 'federate-video', payload: FederateVideoPayload } export type CreateJobOptions = { delay?: number @@ -68,8 +105,8 @@ export type CreateJobOptions = { } const handlers: { [id in JobType]: (job: Job) => Promise } = { - 'activitypub-http-broadcast': processActivityPubHttpBroadcast, - 'activitypub-http-broadcast-parallel': processActivityPubHttpBroadcast, + 'activitypub-http-broadcast': processActivityPubHttpSequentialBroadcast, + 'activitypub-http-broadcast-parallel': processActivityPubParallelHttpBroadcast, 'activitypub-http-unicast': processActivityPubHttpUnicast, 'activitypub-http-fetcher': processActivityPubHttpFetcher, 'activitypub-cleaner': processActivityPubCleaner, @@ -85,7 +122,11 @@ const handlers: { [id in JobType]: (job: Job) => Promise } = { 'video-redundancy': processVideoRedundancy, 'move-to-object-storage': processMoveToObjectStorage, 'manage-video-torrent': processManageVideoTorrent, - 'video-studio-edition': processVideoStudioEdition + 'video-studio-edition': processVideoStudioEdition, + 'video-channel-import': processVideoChannelImport, + 'after-video-channel-import': processAfterVideoChannelImport, + 'notify': processNotify, + 'federate-video': processFederateVideo } const errorHandlers: { [id in JobType]?: (job: Job, err: any) => Promise } = { @@ -110,7 +151,11 @@ const jobTypes: JobType[] = [ 'video-live-ending', 'move-to-object-storage', 'manage-video-torrent', - 'video-studio-edition' + 'video-studio-edition', + 'video-channel-import', + 'after-video-channel-import', + 'notify', + 'federate-video' ] const silentFailure = new Set([ 'activitypub-http-unicast' ]) @@ -119,112 +164,245 @@ class JobQueue { private static instance: JobQueue + private workers: { [id in JobType]?: Worker } = {} private queues: { [id in JobType]?: Queue } = {} + private queueSchedulers: { [id in JobType]?: QueueScheduler } = {} + private queueEvents: { [id in JobType]?: QueueEvents } = {} + + private flowProducer: FlowProducer + private initialized = false private jobRedisPrefix: string private constructor () { } - init (produceOnly = false) { + init () { // Already initialized if (this.initialized === true) return this.initialized = true this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST - const queueOptions: Bull.QueueOptions = { + for (const handlerName of (Object.keys(handlers) as JobType[])) { + this.buildWorker(handlerName) + this.buildQueue(handlerName) + this.buildQueueScheduler(handlerName) + this.buildQueueEvent(handlerName) + } + + this.flowProducer = new FlowProducer({ + connection: Redis.getRedisClientOptions('FlowProducer'), + prefix: this.jobRedisPrefix + }) + this.flowProducer.on('error', err => { logger.error('Error in flow producer', { err }) }) + + this.addRepeatableJobs() + } + + private buildWorker (handlerName: JobType) { + const workerOptions: WorkerOptions = { + autorun: false, + concurrency: this.getJobConcurrency(handlerName), prefix: this.jobRedisPrefix, - redis: { - password: CONFIG.REDIS.AUTH, - db: CONFIG.REDIS.DB, - host: CONFIG.REDIS.HOSTNAME, - port: CONFIG.REDIS.PORT, - path: CONFIG.REDIS.SOCKET - }, - settings: { - maxStalledCount: 10 // transcoding could be long, so jobs can often be interrupted by restarts - } + connection: Redis.getRedisClientOptions('Worker') } - for (const handlerName of (Object.keys(handlers) as JobType[])) { - const queue = new Bull(handlerName, queueOptions) + const handler = function (job: Job) { + const timeout = JOB_TTL[handlerName] + const p = handlers[handlerName](job) - if (produceOnly) { - queue.pause(true) - .catch(err => logger.error('Cannot pause queue %s in produced only job queue', handlerName, { err })) - } + if (!timeout) return p - const handler = handlers[handlerName] + return timeoutPromise(p, timeout) + } - queue.process(this.getJobConcurrency(handlerName), handler) - .catch(err => logger.error('Error in job queue processor %s.', handlerName, { err })) + const processor = async (jobArg: Job) => { + const job = await Hooks.wrapObject(jobArg, 'filter:job-queue.process.params', { type: handlerName }) - queue.on('failed', (job, err) => { - const logLevel = silentFailure.has(handlerName) - ? 'debug' - : 'error' + return Hooks.wrapPromiseFun(handler, job, 'filter:job-queue.process.result') + } - logger.log(logLevel, 'Cannot execute job %d in queue %s.', job.id, handlerName, { payload: job.data, err }) + const worker = new Worker(handlerName, processor, workerOptions) - if (errorHandlers[job.name]) { - errorHandlers[job.name](job, err) - .catch(err => logger.error('Cannot run error handler for job failure %d in queue %s.', job.id, handlerName, { err })) - } - }) + worker.on('failed', (job, err) => { + const logLevel = silentFailure.has(handlerName) + ? 'debug' + : 'error' - queue.on('error', err => { - logger.error('Error in job queue %s.', handlerName, { err }) - }) + logger.log(logLevel, 'Cannot execute job %s in queue %s.', job.id, handlerName, { payload: job.data, err }) + + if (errorHandlers[job.name]) { + errorHandlers[job.name](job, err) + .catch(err => logger.error('Cannot run error handler for job failure %d in queue %s.', job.id, handlerName, { err })) + } + }) + + worker.on('error', err => { logger.error('Error in job worker %s.', handlerName, { err }) }) + + this.workers[handlerName] = worker + } - this.queues[handlerName] = queue + private buildQueue (handlerName: JobType) { + const queueOptions: QueueOptions = { + connection: Redis.getRedisClientOptions('Queue'), + prefix: this.jobRedisPrefix } - this.addRepeatableJobs() + const queue = new Queue(handlerName, queueOptions) + queue.on('error', err => { logger.error('Error in job queue %s.', handlerName, { err }) }) + + this.queues[handlerName] = queue } - terminate () { - for (const queueName of Object.keys(this.queues)) { - const queue = this.queues[queueName] - queue.close() + private buildQueueScheduler (handlerName: JobType) { + const queueSchedulerOptions: QueueSchedulerOptions = { + autorun: false, + connection: Redis.getRedisClientOptions('QueueScheduler'), + prefix: this.jobRedisPrefix, + maxStalledCount: 10 } + + const queueScheduler = new QueueScheduler(handlerName, queueSchedulerOptions) + queueScheduler.on('error', err => { logger.error('Error in job queue scheduler %s.', handlerName, { err }) }) + + this.queueSchedulers[handlerName] = queueScheduler + } + + private buildQueueEvent (handlerName: JobType) { + const queueEventsOptions: QueueEventsOptions = { + autorun: false, + connection: Redis.getRedisClientOptions('QueueEvent'), + prefix: this.jobRedisPrefix + } + + const queueEvents = new QueueEvents(handlerName, queueEventsOptions) + queueEvents.on('error', err => { logger.error('Error in job queue events %s.', handlerName, { err }) }) + + this.queueEvents[handlerName] = queueEvents + } + + // --------------------------------------------------------------------------- + + async terminate () { + const promises = Object.keys(this.workers) + .map(handlerName => { + const worker: Worker = this.workers[handlerName] + const queue: Queue = this.queues[handlerName] + const queueScheduler: QueueScheduler = this.queueSchedulers[handlerName] + const queueEvent: QueueEvents = this.queueEvents[handlerName] + + return Promise.all([ + worker.close(false), + queue.close(), + queueScheduler.close(), + queueEvent.close() + ]) + }) + + return Promise.all(promises) + } + + start () { + const promises = Object.keys(this.workers) + .map(handlerName => { + const worker: Worker = this.workers[handlerName] + const queueScheduler: QueueScheduler = this.queueSchedulers[handlerName] + const queueEvent: QueueEvents = this.queueEvents[handlerName] + + return Promise.all([ + worker.run(), + queueScheduler.run(), + queueEvent.run() + ]) + }) + + return Promise.all(promises) } async pause () { - for (const handler of Object.keys(this.queues)) { - await this.queues[handler].pause(true) + for (const handlerName of Object.keys(this.workers)) { + const worker: Worker = this.workers[handlerName] + + await worker.pause() } } - async resume () { - for (const handler of Object.keys(this.queues)) { - await this.queues[handler].resume(true) + resume () { + for (const handlerName of Object.keys(this.workers)) { + const worker: Worker = this.workers[handlerName] + + worker.resume() } } - createJob (obj: CreateJobArgument, options: CreateJobOptions = {}): void { - this.createJobWithPromise(obj, options) - .catch(err => logger.error('Cannot create job.', { err, obj })) + // --------------------------------------------------------------------------- + + createJobAsync (options: CreateJobArgument & CreateJobOptions): void { + this.createJob(options) + .catch(err => logger.error('Cannot create job.', { err, options })) } - createJobWithPromise (obj: CreateJobArgument, options: CreateJobOptions = {}) { - const queue: Queue = this.queues[obj.type] + createJob (options: CreateJobArgument & CreateJobOptions) { + const queue: Queue = this.queues[options.type] if (queue === undefined) { - logger.error('Unknown queue %s: cannot create job.', obj.type) + logger.error('Unknown queue %s: cannot create job.', options.type) return } - const jobArgs: JobOptions = { + const jobOptions = this.buildJobOptions(options.type as JobType, pick(options, [ 'priority', 'delay' ])) + + return queue.add('job', options.payload, jobOptions) + } + + createSequentialJobFlow (...jobs: ((CreateJobArgument & CreateJobOptions) | undefined)[]) { + let lastJob: FlowJob + + for (const job of jobs) { + if (!job) continue + + lastJob = { + ...this.buildJobFlowOption(job), + + children: lastJob + ? [ lastJob ] + : [] + } + } + + return this.flowProducer.add(lastJob) + } + + createJobWithChildren (parent: CreateJobArgument & CreateJobOptions, children: (CreateJobArgument & CreateJobOptions)[]) { + return this.flowProducer.add({ + ...this.buildJobFlowOption(parent), + + children: children.map(c => this.buildJobFlowOption(c)) + }) + } + + private buildJobFlowOption (job: CreateJobArgument & CreateJobOptions): FlowJob { + return { + name: 'job', + data: job.payload, + queueName: job.type, + opts: this.buildJobOptions(job.type as JobType, pick(job, [ 'priority', 'delay' ])) + } + } + + private buildJobOptions (type: JobType, options: CreateJobOptions = {}): JobsOptions { + return { backoff: { delay: 60 * 1000, type: 'exponential' }, - attempts: JOB_ATTEMPTS[obj.type], - timeout: JOB_TTL[obj.type], + attempts: JOB_ATTEMPTS[type], priority: options.priority, - delay: options.delay - } + delay: options.delay, - return queue.add(obj.payload, jobArgs) + ...this.buildJobRemovalOptions(type) + } } + // --------------------------------------------------------------------------- + async listForApi (options: { state?: JobState start: number @@ -234,13 +412,14 @@ class JobQueue { }): Promise { const { state, start, count, asc, jobType } = options - const states = state ? [ state ] : jobStates - let results: Job[] = [] + const states = this.buildStateFilter(state) + const filteredJobTypes = this.buildTypeFilter(jobType) - const filteredJobTypes = this.filterJobTypes(jobType) + let results: Job[] = [] for (const jobType of filteredJobTypes) { - const queue = this.queues[jobType] + const queue: Queue = this.queues[jobType] + if (queue === undefined) { logger.error('Unknown queue %s to list jobs.', jobType) continue @@ -264,9 +443,9 @@ class JobQueue { async count (state: JobState, jobType?: JobType): Promise { const states = state ? [ state ] : jobStates - let total = 0 + const filteredJobTypes = this.buildTypeFilter(jobType) - const filteredJobTypes = this.filterJobTypes(jobType) + let total = 0 for (const type of filteredJobTypes) { const queue = this.queues[type] @@ -285,31 +464,55 @@ class JobQueue { return total } + private buildStateFilter (state?: JobState) { + if (!state) return jobStates + + const states = [ state ] + + // Include parent if filtering on waiting + if (state === 'waiting') states.push('waiting-children') + + return states + } + + private buildTypeFilter (jobType?: JobType) { + if (!jobType) return jobTypes + + return jobTypes.filter(t => t === jobType) + } + + async getStats () { + const promises = jobTypes.map(async t => ({ jobType: t, counts: await this.queues[t].getJobCounts() })) + + return Promise.all(promises) + } + + // --------------------------------------------------------------------------- + async removeOldJobs () { for (const key of Object.keys(this.queues)) { - const queue = this.queues[key] - await queue.clean(JOB_COMPLETED_LIFETIME, 'completed') + const queue: Queue = this.queues[key] + await queue.clean(parseDurationToMs('7 days'), 1000, 'completed') + await queue.clean(parseDurationToMs('7 days'), 1000, 'failed') } } private addRepeatableJobs () { - this.queues['videos-views-stats'].add({}, { - repeat: REPEAT_JOBS['videos-views-stats'] + this.queues['videos-views-stats'].add('job', {}, { + repeat: REPEAT_JOBS['videos-views-stats'], + + ...this.buildJobRemovalOptions('videos-views-stats') }).catch(err => logger.error('Cannot add repeatable job.', { err })) if (CONFIG.FEDERATION.VIDEOS.CLEANUP_REMOTE_INTERACTIONS) { - this.queues['activitypub-cleaner'].add({}, { - repeat: REPEAT_JOBS['activitypub-cleaner'] + this.queues['activitypub-cleaner'].add('job', {}, { + repeat: REPEAT_JOBS['activitypub-cleaner'], + + ...this.buildJobRemovalOptions('activitypub-cleaner') }).catch(err => logger.error('Cannot add repeatable job.', { err })) } } - private filterJobTypes (jobType?: JobType) { - if (!jobType) return jobTypes - - return jobTypes.filter(t => t === jobType) - } - private getJobConcurrency (jobType: JobType) { if (jobType === 'video-transcoding') return CONFIG.TRANSCODING.CONCURRENCY if (jobType === 'video-import') return CONFIG.IMPORT.VIDEOS.CONCURRENCY @@ -317,6 +520,23 @@ class JobQueue { return JOB_CONCURRENCY[jobType] } + private buildJobRemovalOptions (queueName: string) { + return { + removeOnComplete: { + // Wants seconds + age: (JOB_REMOVAL_OPTIONS.SUCCESS[queueName] || JOB_REMOVAL_OPTIONS.SUCCESS.DEFAULT) / 1000, + + count: JOB_REMOVAL_OPTIONS.COUNT + }, + removeOnFail: { + // Wants seconds + age: (JOB_REMOVAL_OPTIONS.FAILURE[queueName] || JOB_REMOVAL_OPTIONS.FAILURE.DEFAULT) / 1000, + + count: JOB_REMOVAL_OPTIONS.COUNT / 1000 + } + } + } + static get Instance () { return this.instance || (this.instance = new this()) }