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