import { JobState, JobType } from '../../../shared/models'
import { logger } from '../../helpers/logger'
import { Redis } from '../redis'
-import { CONFIG, JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_REQUEST_TTL } from '../../initializers'
+import { CONFIG, JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_JOBS } from '../../initializers'
import { ActivitypubHttpBroadcastPayload, processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast'
import { ActivitypubHttpFetcherPayload, processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher'
import { ActivitypubHttpUnicastPayload, processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast'
import { EmailPayload, processEmail } from './handlers/email'
import { processVideoFile, processVideoFileImport, VideoFileImportPayload, VideoFilePayload } from './handlers/video-file'
import { ActivitypubFollowPayload, processActivityPubFollow } from './handlers/activitypub-follow'
+import { processVideoImport, VideoImportPayload } from './handlers/video-import'
+import { processVideosViews } from './handlers/video-views'
+import { refreshAPObject, RefreshPayload } from './handlers/activitypub-refresher'
type CreateJobArgument =
{ type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } |
{ type: 'activitypub-follow', payload: ActivitypubFollowPayload } |
{ type: 'video-file-import', payload: VideoFileImportPayload } |
{ type: 'video-file', payload: VideoFilePayload } |
- { type: 'email', payload: EmailPayload }
+ { type: 'email', payload: EmailPayload } |
+ { type: 'video-import', payload: VideoImportPayload } |
+ { type: 'activitypub-refresher', payload: RefreshPayload } |
+ { type: 'videos-views', payload: {} }
const handlers: { [ id in JobType ]: (job: Bull.Job) => Promise<any>} = {
'activitypub-http-broadcast': processActivityPubHttpBroadcast,
'activitypub-follow': processActivityPubFollow,
'video-file-import': processVideoFileImport,
'video-file': processVideoFile,
- 'email': processEmail
-}
-
-const jobsWithRequestTimeout: { [ id in JobType ]?: boolean } = {
- 'activitypub-http-broadcast': true,
- 'activitypub-http-unicast': true,
- 'activitypub-http-fetcher': true,
- 'activitypub-follow': true
+ 'email': processEmail,
+ 'video-import': processVideoImport,
+ 'videos-views': processVideosViews,
+ 'activitypub-refresher': refreshAPObject
}
const jobTypes: JobType[] = [
'activitypub-http-unicast',
'email',
'video-file',
- 'video-file-import'
+ 'video-file-import',
+ 'video-import',
+ 'videos-views',
+ 'activitypub-refresher'
]
class JobQueue {
this.jobRedisPrefix = 'bull-' + CONFIG.WEBSERVER.HOST
const queueOptions = {
prefix: this.jobRedisPrefix,
- redis: Redis.getRedisClient()
+ redis: Redis.getRedisClient(),
+ settings: {
+ maxStalledCount: 10 // transcoding could be long, so jobs can often be interrupted by restarts
+ }
}
for (const handlerName of Object.keys(handlers)) {
const handler = handlers[handlerName]
queue.process(JOB_CONCURRENCY[handlerName], handler)
- .catch(err => logger.error('Cannot execute job queue %s.', handlerName, { err }))
+ .catch(err => logger.error('Error in job queue processor %s.', handlerName, { err }))
+
+ queue.on('failed', (job, err) => {
+ logger.error('Cannot execute job %d in queue %s.', job.id, handlerName, { payload: job.data, err })
+ })
queue.on('error', err => {
logger.error('Error in job queue %s.', handlerName, { err })
- process.exit(-1)
})
this.queues[handlerName] = queue
}
+
+ this.addRepeatableJobs()
+ }
+
+ terminate () {
+ for (const queueName of Object.keys(this.queues)) {
+ const queue = this.queues[queueName]
+ queue.close()
+ }
}
createJob (obj: CreateJobArgument) {
const jobArgs: Bull.JobOptions = {
backoff: { delay: 60 * 1000, type: 'exponential' },
- attempts: JOB_ATTEMPTS[obj.type]
- }
-
- if (jobsWithRequestTimeout[obj.type] === true) {
- jobArgs.timeout = JOB_REQUEST_TTL
+ attempts: JOB_ATTEMPTS[obj.type],
+ timeout: JOB_TTL[obj.type]
}
return queue.add(obj.payload, jobArgs)
}
}
+ private addRepeatableJobs () {
+ this.queues['videos-views'].add({}, {
+ repeat: REPEAT_JOBS['videos-views']
+ })
+ }
+
static get Instance () {
return this.instance || (this.instance = new this())
}