PeerTube/server/lib/job-queue/job-queue.ts

272 lines
8.5 KiB
TypeScript
Raw Normal View History

2018-07-10 17:02:20 +02:00
import * as Bull from 'bull'
2020-12-14 12:00:35 +01:00
import { jobStates } from '@server/helpers/custom-validators/jobs'
import { CONFIG } from '@server/initializers/config'
2020-12-14 12:00:35 +01:00
import { processVideoRedundancy } from '@server/lib/job-queue/handlers/video-redundancy'
2020-04-23 09:32:53 +02:00
import {
ActivitypubFollowPayload,
ActivitypubHttpBroadcastPayload,
ActivitypubHttpFetcherPayload,
ActivitypubHttpUnicastPayload,
ActorKeysPayload,
EmailPayload,
2020-04-23 09:32:53 +02:00
JobState,
JobType,
RefreshPayload,
VideoFileImportPayload,
VideoImportPayload,
VideoLiveEndingPayload,
VideoRedundancyPayload,
VideoTranscodingPayload
2020-04-23 09:32:53 +02:00
} from '../../../shared/models'
import { logger } from '../../helpers/logger'
import { JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_JOBS, WEBSERVER } from '../../initializers/constants'
2020-12-14 12:00:35 +01:00
import { Redis } from '../redis'
import { processActivityPubCleaner } from './handlers/activitypub-cleaner'
2020-12-14 12:00:35 +01:00
import { processActivityPubFollow } from './handlers/activitypub-follow'
2020-04-23 09:32:53 +02:00
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 { processActorKeys } from './handlers/actor-keys'
2020-12-14 12:00:35 +01:00
import { processEmail } from './handlers/email'
import { processVideoFileImport } from './handlers/video-file-import'
2020-12-14 12:00:35 +01:00
import { processVideoImport } from './handlers/video-import'
import { processVideoLiveEnding } from './handlers/video-live-ending'
2020-12-14 12:00:35 +01:00
import { processVideoTranscoding } from './handlers/video-transcoding'
import { processVideosViews } from './handlers/video-views'
type CreateJobArgument =
{ type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } |
{ type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload } |
{ type: 'activitypub-http-fetcher', payload: ActivitypubHttpFetcherPayload } |
{ type: 'activitypub-http-cleaner', payload: {} } |
{ type: 'activitypub-follow', payload: ActivitypubFollowPayload } |
{ type: 'video-file-import', payload: VideoFileImportPayload } |
{ type: 'video-transcoding', payload: VideoTranscodingPayload } |
{ type: 'email', payload: EmailPayload } |
2018-08-29 16:26:25 +02:00
{ type: 'video-import', payload: VideoImportPayload } |
{ type: 'activitypub-refresher', payload: RefreshPayload } |
2020-01-10 10:11:28 +01:00
{ type: 'videos-views', payload: {} } |
{ type: 'video-live-ending', payload: VideoLiveEndingPayload } |
{ type: 'actor-keys', payload: ActorKeysPayload } |
2020-01-10 10:11:28 +01:00
{ type: 'video-redundancy', payload: VideoRedundancyPayload }
type CreateJobOptions = {
delay?: number
priority?: number
}
2020-01-31 16:56:52 +01:00
const handlers: { [id in JobType]: (job: Bull.Job) => Promise<any> } = {
'activitypub-http-broadcast': processActivityPubHttpBroadcast,
'activitypub-http-unicast': processActivityPubHttpUnicast,
'activitypub-http-fetcher': processActivityPubHttpFetcher,
'activitypub-cleaner': processActivityPubCleaner,
'activitypub-follow': processActivityPubFollow,
'video-file-import': processVideoFileImport,
'video-transcoding': processVideoTranscoding,
'email': processEmail,
2018-08-29 16:26:25 +02:00
'video-import': processVideoImport,
'videos-views': processVideosViews,
2020-01-10 10:11:28 +01:00
'activitypub-refresher': refreshAPObject,
'video-live-ending': processVideoLiveEnding,
'actor-keys': processActorKeys,
2020-01-10 10:11:28 +01:00
'video-redundancy': processVideoRedundancy
}
2018-07-10 17:02:20 +02:00
const jobTypes: JobType[] = [
'activitypub-follow',
2018-05-09 09:08:22 +02:00
'activitypub-http-broadcast',
'activitypub-http-fetcher',
2018-07-10 17:02:20 +02:00
'activitypub-http-unicast',
'activitypub-cleaner',
2018-07-10 17:02:20 +02:00
'email',
'video-transcoding',
'video-file-import',
2018-08-29 16:26:25 +02:00
'video-import',
'videos-views',
2020-01-10 10:11:28 +01:00
'activitypub-refresher',
'video-redundancy',
'actor-keys',
'video-live-ending'
2018-05-09 09:08:22 +02:00
]
class JobQueue {
private static instance: JobQueue
2020-01-31 16:56:52 +01:00
private queues: { [id in JobType]?: Bull.Queue } = {}
private initialized = false
private jobRedisPrefix: string
2020-01-31 16:56:52 +01:00
private constructor () {
}
2020-01-31 16:56:52 +01:00
init () {
// Already initialized
if (this.initialized === true) return
this.initialized = true
2019-04-11 11:33:44 +02:00
this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST
2018-07-10 17:02:20 +02:00
const queueOptions = {
prefix: this.jobRedisPrefix,
redis: Redis.getRedisClientOptions(),
settings: {
maxStalledCount: 10 // transcoding could be long, so jobs can often be interrupted by restarts
}
2018-07-10 17:02:20 +02:00
}
2018-01-30 13:27:07 +01:00
for (const handlerName of (Object.keys(handlers) as JobType[])) {
2018-07-10 17:02:20 +02:00
const queue = new Bull(handlerName, queueOptions)
const handler = handlers[handlerName]
queue.process(this.getJobConcurrency(handlerName), handler)
2018-08-03 10:19:51 +02:00
.catch(err => logger.error('Error in job queue processor %s.', handlerName, { err }))
2018-08-03 09:27:30 +02:00
queue.on('failed', (job, err) => {
logger.error('Cannot execute job %d in queue %s.', job.id, handlerName, { payload: job.data, err })
})
2018-02-12 11:25:09 +01:00
2018-07-10 17:02:20 +02:00
queue.on('error', err => {
logger.error('Error in job queue %s.', handlerName, { err })
})
2018-07-10 17:02:20 +02:00
this.queues[handlerName] = queue
}
2018-08-29 16:26:25 +02:00
this.addRepeatableJobs()
}
2018-07-30 18:49:54 +02:00
terminate () {
for (const queueName of Object.keys(this.queues)) {
const queue = this.queues[queueName]
queue.close()
}
}
createJob (obj: CreateJobArgument, options: CreateJobOptions = {}): void {
this.createJobWithPromise(obj, options)
.catch(err => logger.error('Cannot create job.', { err, obj }))
2020-01-31 16:56:52 +01:00
}
createJobWithPromise (obj: CreateJobArgument, options: CreateJobOptions = {}) {
2018-07-10 17:02:20 +02:00
const queue = this.queues[obj.type]
if (queue === undefined) {
logger.error('Unknown queue %s: cannot create job.', obj.type)
2020-01-31 16:56:52 +01:00
return
2018-07-10 17:02:20 +02:00
}
2018-07-10 17:02:20 +02:00
const jobArgs: Bull.JobOptions = {
backoff: { delay: 60 * 1000, type: 'exponential' },
2018-08-03 10:19:51 +02:00
attempts: JOB_ATTEMPTS[obj.type],
timeout: JOB_TTL[obj.type],
priority: options.priority,
delay: options.delay
2018-07-10 17:02:20 +02:00
}
2018-05-09 09:08:22 +02:00
2018-07-10 17:02:20 +02:00
return queue.add(obj.payload, jobArgs)
}
2019-12-04 14:49:59 +01:00
async listForApi (options: {
2020-12-14 12:00:35 +01:00
state?: JobState
2020-01-31 16:56:52 +01:00
start: number
count: number
asc?: boolean
2019-12-04 14:49:59 +01:00
jobType: JobType
}): Promise<Bull.Job[]> {
2020-12-14 12:00:35 +01:00
const { state, start, count, asc, jobType } = options
const states = state ? [ state ] : jobStates
2018-07-10 17:02:20 +02:00
let results: Bull.Job[] = []
2019-12-04 14:49:59 +01:00
const filteredJobTypes = this.filterJobTypes(jobType)
for (const jobType of filteredJobTypes) {
2020-01-31 16:56:52 +01:00
const queue = this.queues[jobType]
2018-07-10 17:02:20 +02:00
if (queue === undefined) {
logger.error('Unknown queue %s to list jobs.', jobType)
continue
}
2020-12-14 12:00:35 +01:00
const jobs = await queue.getJobs(states, 0, start + count, asc)
2018-07-10 17:02:20 +02:00
results = results.concat(jobs)
}
2018-07-10 17:02:20 +02:00
results.sort((j1: any, j2: any) => {
if (j1.timestamp < j2.timestamp) return -1
else if (j1.timestamp === j2.timestamp) return 0
2018-07-10 17:02:20 +02:00
return 1
})
2018-07-10 17:02:20 +02:00
if (asc === false) results.reverse()
2018-07-10 17:02:20 +02:00
return results.slice(start, start + count)
}
2020-12-14 12:00:35 +01:00
async count (state: JobState, jobType?: JobType): Promise<number> {
const states = state ? [ state ] : jobStates
2018-07-10 17:02:20 +02:00
let total = 0
2018-02-12 11:25:09 +01:00
2019-12-04 14:49:59 +01:00
const filteredJobTypes = this.filterJobTypes(jobType)
for (const type of filteredJobTypes) {
2020-01-31 16:56:52 +01:00
const queue = this.queues[type]
2018-07-10 17:02:20 +02:00
if (queue === undefined) {
logger.error('Unknown queue %s to count jobs.', type)
continue
}
2018-02-12 11:25:09 +01:00
2018-07-10 17:02:20 +02:00
const counts = await queue.getJobCounts()
2018-02-12 11:25:09 +01:00
2020-12-13 19:27:25 +01:00
for (const s of states) {
total += counts[s]
}
2018-07-10 17:02:20 +02:00
}
2018-02-12 11:25:09 +01:00
2018-07-10 17:02:20 +02:00
return total
2018-02-12 11:25:09 +01:00
}
async removeOldJobs () {
2018-07-10 17:02:20 +02:00
for (const key of Object.keys(this.queues)) {
const queue = this.queues[key]
await queue.clean(JOB_COMPLETED_LIFETIME, 'completed')
2018-07-10 17:02:20 +02:00
}
}
2018-08-29 16:26:25 +02:00
private addRepeatableJobs () {
this.queues['videos-views'].add({}, {
repeat: REPEAT_JOBS['videos-views']
2020-01-31 16:56:52 +01:00
}).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']
}).catch(err => logger.error('Cannot add repeatable job.', { err }))
}
2018-08-29 16:26:25 +02:00
}
2019-12-04 14:49:59 +01:00
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
return JOB_CONCURRENCY[jobType]
}
static get Instance () {
return this.instance || (this.instance = new this())
}
}
// ---------------------------------------------------------------------------
export {
2019-12-04 14:49:59 +01:00
jobTypes,
JobQueue
}