2018-07-10 17:02:20 +02:00
|
|
|
import * as Bull from 'bull'
|
2018-02-27 17:42:32 +01:00
|
|
|
import { JobState, JobType } from '../../../shared/models'
|
2018-01-25 15:05:18 +01:00
|
|
|
import { logger } from '../../helpers/logger'
|
2018-05-14 17:51:15 +02:00
|
|
|
import { Redis } from '../redis'
|
2018-08-29 16:26:25 +02:00
|
|
|
import { CONFIG, JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_JOBS } from '../../initializers'
|
2018-01-25 15:05:18 +01:00
|
|
|
import { ActivitypubHttpBroadcastPayload, processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast'
|
|
|
|
import { ActivitypubHttpFetcherPayload, processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher'
|
|
|
|
import { ActivitypubHttpUnicastPayload, processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast'
|
2018-01-30 13:27:07 +01:00
|
|
|
import { EmailPayload, processEmail } from './handlers/email'
|
2018-07-10 17:02:20 +02:00
|
|
|
import { processVideoFile, processVideoFileImport, VideoFileImportPayload, VideoFilePayload } from './handlers/video-file'
|
2018-04-18 15:32:40 +02:00
|
|
|
import { ActivitypubFollowPayload, processActivityPubFollow } from './handlers/activitypub-follow'
|
2018-08-02 15:34:09 +02:00
|
|
|
import { processVideoImport, VideoImportPayload } from './handlers/video-import'
|
2018-11-15 16:18:12 +01:00
|
|
|
import { processVideosViews } from './handlers/video-views'
|
2018-11-20 10:05:51 +01:00
|
|
|
import { refreshAPObject, RefreshPayload } from './handlers/activitypub-refresher'
|
2018-01-25 15:05:18 +01:00
|
|
|
|
|
|
|
type CreateJobArgument =
|
|
|
|
{ type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } |
|
|
|
|
{ type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload } |
|
|
|
|
{ type: 'activitypub-http-fetcher', payload: ActivitypubHttpFetcherPayload } |
|
2018-04-18 15:32:40 +02:00
|
|
|
{ type: 'activitypub-follow', payload: ActivitypubFollowPayload } |
|
2018-06-07 09:43:18 +02:00
|
|
|
{ type: 'video-file-import', payload: VideoFileImportPayload } |
|
2018-01-30 13:27:07 +01:00
|
|
|
{ type: 'video-file', payload: VideoFilePayload } |
|
2018-08-02 15:34:09 +02:00
|
|
|
{ type: 'email', payload: EmailPayload } |
|
2018-08-29 16:26:25 +02:00
|
|
|
{ type: 'video-import', payload: VideoImportPayload } |
|
2018-11-20 10:05:51 +01:00
|
|
|
{ type: 'activitypub-refresher', payload: RefreshPayload } |
|
2018-08-29 16:26:25 +02:00
|
|
|
{ type: 'videos-views', payload: {} }
|
2018-01-25 15:05:18 +01:00
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
const handlers: { [ id in JobType ]: (job: Bull.Job) => Promise<any>} = {
|
2018-01-25 15:05:18 +01:00
|
|
|
'activitypub-http-broadcast': processActivityPubHttpBroadcast,
|
|
|
|
'activitypub-http-unicast': processActivityPubHttpUnicast,
|
|
|
|
'activitypub-http-fetcher': processActivityPubHttpFetcher,
|
2018-04-18 15:32:40 +02:00
|
|
|
'activitypub-follow': processActivityPubFollow,
|
2018-06-07 09:43:18 +02:00
|
|
|
'video-file-import': processVideoFileImport,
|
2018-01-30 13:27:07 +01:00
|
|
|
'video-file': processVideoFile,
|
2018-08-02 15:34:09 +02:00
|
|
|
'email': processEmail,
|
2018-08-29 16:26:25 +02:00
|
|
|
'video-import': processVideoImport,
|
2018-11-20 10:05:51 +01:00
|
|
|
'videos-views': processVideosViews,
|
|
|
|
'activitypub-refresher': refreshAPObject
|
2018-01-25 15:05:18 +01:00
|
|
|
}
|
|
|
|
|
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',
|
|
|
|
'email',
|
|
|
|
'video-file',
|
2018-08-02 15:34:09 +02:00
|
|
|
'video-file-import',
|
2018-08-29 16:26:25 +02:00
|
|
|
'video-import',
|
2018-11-20 10:05:51 +01:00
|
|
|
'videos-views',
|
|
|
|
'activitypub-refresher'
|
2018-05-09 09:08:22 +02:00
|
|
|
]
|
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
class JobQueue {
|
|
|
|
|
|
|
|
private static instance: JobQueue
|
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
private queues: { [ id in JobType ]?: Bull.Queue } = {}
|
2018-01-25 15:05:18 +01:00
|
|
|
private initialized = false
|
2018-02-27 16:57:53 +01:00
|
|
|
private jobRedisPrefix: string
|
2018-01-25 15:05:18 +01:00
|
|
|
|
|
|
|
private constructor () {}
|
|
|
|
|
2018-02-12 11:25:09 +01:00
|
|
|
async init () {
|
2018-01-25 15:05:18 +01:00
|
|
|
// Already initialized
|
|
|
|
if (this.initialized === true) return
|
|
|
|
this.initialized = true
|
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
this.jobRedisPrefix = 'bull-' + CONFIG.WEBSERVER.HOST
|
|
|
|
const queueOptions = {
|
2018-02-27 16:57:53 +01:00
|
|
|
prefix: this.jobRedisPrefix,
|
2018-07-30 19:18:01 +02:00
|
|
|
redis: Redis.getRedisClient(),
|
|
|
|
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
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
for (const handlerName of Object.keys(handlers)) {
|
|
|
|
const queue = new Bull(handlerName, queueOptions)
|
|
|
|
const handler = handlers[handlerName]
|
2018-01-25 15:05:18 +01:00
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
queue.process(JOB_CONCURRENCY[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-01-25 15:05:18 +01:00
|
|
|
})
|
2018-07-10 17:02:20 +02:00
|
|
|
|
|
|
|
this.queues[handlerName] = queue
|
2018-01-25 15:05:18 +01:00
|
|
|
}
|
2018-08-29 16:26:25 +02:00
|
|
|
|
|
|
|
this.addRepeatableJobs()
|
2018-01-25 15:05:18 +01:00
|
|
|
}
|
|
|
|
|
2018-07-30 18:49:54 +02:00
|
|
|
terminate () {
|
|
|
|
for (const queueName of Object.keys(this.queues)) {
|
|
|
|
const queue = this.queues[queueName]
|
|
|
|
queue.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
createJob (obj: CreateJobArgument) {
|
|
|
|
const queue = this.queues[obj.type]
|
|
|
|
if (queue === undefined) {
|
|
|
|
logger.error('Unknown queue %s: cannot create job.', obj.type)
|
2018-07-25 22:01:25 +02:00
|
|
|
throw Error('Unknown queue, cannot create job')
|
2018-07-10 17:02:20 +02:00
|
|
|
}
|
2018-01-25 15:05:18 +01: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]
|
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)
|
2018-01-25 15:05:18 +01:00
|
|
|
}
|
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
async listForApi (state: JobState, start: number, count: number, asc?: boolean): Promise<Bull.Job[]> {
|
|
|
|
let results: Bull.Job[] = []
|
2018-01-25 15:05:18 +01:00
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
// TODO: optimize
|
|
|
|
for (const jobType of jobTypes) {
|
|
|
|
const queue = this.queues[ jobType ]
|
|
|
|
if (queue === undefined) {
|
|
|
|
logger.error('Unknown queue %s to list jobs.', jobType)
|
|
|
|
continue
|
|
|
|
}
|
2018-02-27 16:57:53 +01:00
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
// FIXME: Bull queue typings does not have getJobs method
|
|
|
|
const jobs = await (queue as any).getJobs(state, 0, start + count, asc)
|
|
|
|
results = results.concat(jobs)
|
|
|
|
}
|
2018-01-25 15:05:18 +01:00
|
|
|
|
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-01-25 15:05:18 +01:00
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
return 1
|
2018-01-25 15:05:18 +01:00
|
|
|
})
|
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
if (asc === false) results.reverse()
|
2018-01-25 15:05:18 +01:00
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
return results.slice(start, start + count)
|
2018-01-25 15:05:18 +01:00
|
|
|
}
|
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
async count (state: JobState): Promise<number> {
|
|
|
|
let total = 0
|
2018-02-12 11:25:09 +01:00
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
for (const type of jobTypes) {
|
|
|
|
const queue = this.queues[ type ]
|
|
|
|
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
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
total += counts[ state ]
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-12-20 14:31:11 +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]
|
2018-12-20 14:31:11 +01:00
|
|
|
await queue.clean(JOB_COMPLETED_LIFETIME, 'completed')
|
2018-07-10 17:02:20 +02:00
|
|
|
}
|
2018-02-27 16:57:53 +01:00
|
|
|
}
|
|
|
|
|
2018-08-29 16:26:25 +02:00
|
|
|
private addRepeatableJobs () {
|
|
|
|
this.queues['videos-views'].add({}, {
|
|
|
|
repeat: REPEAT_JOBS['videos-views']
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
JobQueue
|
|
|
|
}
|