X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fjob-queue%2Fjob-queue.ts;h=5d0b797b0ca157aab24e72cb8d0750ce5d5d2f2d;hb=3b01f4c0ac764ecb70efaadfd939ca868c28769c;hp=14e181835a381bf8cde10dffcf5091f297bcb471;hpb=610d0be13b3d01f653ef269271dd667a57c85ef2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/job-queue/job-queue.ts b/server/lib/job-queue/job-queue.ts index 14e181835..5d0b797b0 100644 --- a/server/lib/job-queue/job-queue.ts +++ b/server/lib/job-queue/job-queue.ts @@ -1,4 +1,6 @@ import * as Bull from 'bull' +import { jobStates } from '@server/helpers/custom-validators/jobs' +import { processVideoRedundancy } from '@server/lib/job-queue/handlers/video-redundancy' import { ActivitypubFollowPayload, ActivitypubHttpBroadcastPayload, @@ -10,23 +12,24 @@ import { RefreshPayload, VideoFileImportPayload, VideoImportPayload, + VideoLiveEndingPayload, VideoRedundancyPayload, VideoTranscodingPayload } from '../../../shared/models' import { logger } from '../../helpers/logger' -import { Redis } from '../redis' import { JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_JOBS, WEBSERVER } from '../../initializers/constants' +import { Redis } from '../redis' +import { processActivityPubFollow } from './handlers/activitypub-follow' import { processActivityPubHttpBroadcast } 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 { processEmail } from './handlers/email' -import { processVideoTranscoding } from './handlers/video-transcoding' -import { processActivityPubFollow } from './handlers/activitypub-follow' +import { processVideoFileImport } from './handlers/video-file-import' import { processVideoImport } from './handlers/video-import' +import { processVideoLiveEnding } from './handlers/video-live-ending' +import { processVideoTranscoding } from './handlers/video-transcoding' import { processVideosViews } from './handlers/video-views' -import { refreshAPObject } from './handlers/activitypub-refresher' -import { processVideoFileImport } from './handlers/video-file-import' -import { processVideoRedundancy } from '@server/lib/job-queue/handlers/video-redundancy' type CreateJobArgument = { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } | @@ -39,8 +42,13 @@ type CreateJobArgument = { type: 'video-import', payload: VideoImportPayload } | { type: 'activitypub-refresher', payload: RefreshPayload } | { type: 'videos-views', payload: {} } | + { type: 'video-live-ending', payload: VideoLiveEndingPayload } | { type: 'video-redundancy', payload: VideoRedundancyPayload } +type CreateJobOptions = { + delay?: number +} + const handlers: { [id in JobType]: (job: Bull.Job) => Promise } = { 'activitypub-http-broadcast': processActivityPubHttpBroadcast, 'activitypub-http-unicast': processActivityPubHttpUnicast, @@ -52,6 +60,7 @@ const handlers: { [id in JobType]: (job: Bull.Job) => Promise } = { 'video-import': processVideoImport, 'videos-views': processVideosViews, 'activitypub-refresher': refreshAPObject, + 'video-live-ending': processVideoLiveEnding, 'video-redundancy': processVideoRedundancy } @@ -66,7 +75,8 @@ const jobTypes: JobType[] = [ 'video-import', 'videos-views', 'activitypub-refresher', - 'video-redundancy' + 'video-redundancy', + 'video-live-ending' ] class JobQueue { @@ -122,12 +132,12 @@ class JobQueue { } } - createJob (obj: CreateJobArgument): void { - this.createJobWithPromise(obj) + createJob (obj: CreateJobArgument, options: CreateJobOptions = {}): void { + this.createJobWithPromise(obj, options) .catch(err => logger.error('Cannot create job.', { err, obj })) } - createJobWithPromise (obj: CreateJobArgument) { + createJobWithPromise (obj: CreateJobArgument, options: CreateJobOptions = {}) { const queue = this.queues[obj.type] if (queue === undefined) { logger.error('Unknown queue %s: cannot create job.', obj.type) @@ -137,20 +147,23 @@ class JobQueue { const jobArgs: Bull.JobOptions = { backoff: { delay: 60 * 1000, type: 'exponential' }, attempts: JOB_ATTEMPTS[obj.type], - timeout: JOB_TTL[obj.type] + timeout: JOB_TTL[obj.type], + delay: options.delay } return queue.add(obj.payload, jobArgs) } async listForApi (options: { - state: JobState + state?: JobState start: number count: number asc?: boolean jobType: JobType }): Promise { const { state, start, count, asc, jobType } = options + + const states = state ? [ state ] : jobStates let results: Bull.Job[] = [] const filteredJobTypes = this.filterJobTypes(jobType) @@ -162,7 +175,7 @@ class JobQueue { continue } - const jobs = await queue.getJobs([ state ], 0, start + count, asc) + const jobs = await queue.getJobs(states, 0, start + count, asc) results = results.concat(jobs) } @@ -179,6 +192,7 @@ class JobQueue { } async count (state: JobState, jobType?: JobType): Promise { + const states = state ? [ state ] : jobStates let total = 0 const filteredJobTypes = this.filterJobTypes(jobType) @@ -192,7 +206,9 @@ class JobQueue { const counts = await queue.getJobCounts() - total += counts[state] + for (const s of states) { + total += counts[s] + } } return total