2022-08-08 04:42:08 -04:00
|
|
|
import {
|
2022-08-08 09:48:17 -04:00
|
|
|
FlowJob,
|
|
|
|
FlowProducer,
|
2022-08-08 04:42:08 -04:00
|
|
|
Job,
|
|
|
|
JobsOptions,
|
|
|
|
Queue,
|
|
|
|
QueueEvents,
|
|
|
|
QueueEventsOptions,
|
|
|
|
QueueOptions,
|
|
|
|
Worker,
|
|
|
|
WorkerOptions
|
|
|
|
} from 'bullmq'
|
2023-07-31 08:34:36 -04:00
|
|
|
import { pick, timeoutPromise } from '@peertube/peertube-core-utils'
|
2020-04-23 03:32:53 -04:00
|
|
|
import {
|
|
|
|
ActivitypubFollowPayload,
|
|
|
|
ActivitypubHttpBroadcastPayload,
|
2020-04-23 05:36:50 -04:00
|
|
|
ActivitypubHttpFetcherPayload,
|
|
|
|
ActivitypubHttpUnicastPayload,
|
2021-02-26 08:22:25 -05:00
|
|
|
ActorKeysPayload,
|
2022-08-10 03:53:39 -04:00
|
|
|
AfterVideoChannelImportPayload,
|
2021-10-25 11:42:20 -04:00
|
|
|
DeleteResumableUploadMetaFilePayload,
|
2020-04-23 05:36:50 -04:00
|
|
|
EmailPayload,
|
2022-08-08 09:48:17 -04:00
|
|
|
FederateVideoPayload,
|
2023-06-01 08:51:16 -04:00
|
|
|
GenerateStoryboardPayload,
|
2020-04-23 03:32:53 -04:00
|
|
|
JobState,
|
2020-04-23 05:36:50 -04:00
|
|
|
JobType,
|
2022-03-16 10:34:21 -04:00
|
|
|
ManageVideoTorrentPayload,
|
2021-08-17 02:26:20 -04:00
|
|
|
MoveObjectStoragePayload,
|
2022-08-08 09:48:17 -04:00
|
|
|
NotifyPayload,
|
2020-04-23 05:36:50 -04:00
|
|
|
RefreshPayload,
|
2023-04-21 08:55:10 -04:00
|
|
|
TranscodingJobBuilderPayload,
|
2022-08-10 03:53:39 -04:00
|
|
|
VideoChannelImportPayload,
|
2020-04-23 05:36:50 -04:00
|
|
|
VideoFileImportPayload,
|
|
|
|
VideoImportPayload,
|
2020-09-25 04:04:21 -04:00
|
|
|
VideoLiveEndingPayload,
|
2020-04-23 05:36:50 -04:00
|
|
|
VideoRedundancyPayload,
|
2022-03-22 11:58:49 -04:00
|
|
|
VideoStudioEditionPayload,
|
2020-04-23 05:36:50 -04:00
|
|
|
VideoTranscodingPayload
|
2023-07-31 08:34:36 -04:00
|
|
|
} from '@peertube/peertube-models'
|
|
|
|
import { parseDurationToMs } from '@server/helpers/core-utils.js'
|
|
|
|
import { jobStates } from '@server/helpers/custom-validators/jobs.js'
|
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
|
|
|
import { processVideoRedundancy } from '@server/lib/job-queue/handlers/video-redundancy.js'
|
|
|
|
import { logger } from '../../helpers/logger.js'
|
|
|
|
import { JOB_ATTEMPTS, JOB_CONCURRENCY, JOB_REMOVAL_OPTIONS, JOB_TTL, REPEAT_JOBS, WEBSERVER } from '../../initializers/constants.js'
|
|
|
|
import { Hooks } from '../plugins/hooks.js'
|
|
|
|
import { Redis } from '../redis.js'
|
|
|
|
import { processActivityPubCleaner } from './handlers/activitypub-cleaner.js'
|
|
|
|
import { processActivityPubFollow } from './handlers/activitypub-follow.js'
|
|
|
|
import {
|
|
|
|
processActivityPubHttpSequentialBroadcast,
|
|
|
|
processActivityPubParallelHttpBroadcast
|
|
|
|
} from './handlers/activitypub-http-broadcast.js'
|
|
|
|
import { processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher.js'
|
|
|
|
import { processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast.js'
|
|
|
|
import { refreshAPObject } from './handlers/activitypub-refresher.js'
|
|
|
|
import { processActorKeys } from './handlers/actor-keys.js'
|
|
|
|
import { processAfterVideoChannelImport } from './handlers/after-video-channel-import.js'
|
|
|
|
import { processEmail } from './handlers/email.js'
|
|
|
|
import { processFederateVideo } from './handlers/federate-video.js'
|
|
|
|
import { processGenerateStoryboard } from './handlers/generate-storyboard.js'
|
|
|
|
import { processManageVideoTorrent } from './handlers/manage-video-torrent.js'
|
|
|
|
import { onMoveToObjectStorageFailure, processMoveToObjectStorage } from './handlers/move-to-object-storage.js'
|
|
|
|
import { processNotify } from './handlers/notify.js'
|
|
|
|
import { processTranscodingJobBuilder } from './handlers/transcoding-job-builder.js'
|
|
|
|
import { processVideoChannelImport } from './handlers/video-channel-import.js'
|
|
|
|
import { processVideoFileImport } from './handlers/video-file-import.js'
|
|
|
|
import { processVideoImport } from './handlers/video-import.js'
|
|
|
|
import { processVideoLiveEnding } from './handlers/video-live-ending.js'
|
|
|
|
import { processVideoStudioEdition } from './handlers/video-studio-edition.js'
|
|
|
|
import { processVideoTranscoding } from './handlers/video-transcoding.js'
|
|
|
|
import { processVideosViewsStats } from './handlers/video-views-stats.js'
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
export type CreateJobArgument =
|
2018-01-25 09:05:18 -05:00
|
|
|
{ type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } |
|
2022-06-17 08:08:13 -04:00
|
|
|
{ type: 'activitypub-http-broadcast-parallel', payload: ActivitypubHttpBroadcastPayload } |
|
2018-01-25 09:05:18 -05:00
|
|
|
{ type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload } |
|
|
|
|
{ type: 'activitypub-http-fetcher', payload: ActivitypubHttpFetcherPayload } |
|
2023-04-21 08:55:10 -04:00
|
|
|
{ type: 'activitypub-cleaner', payload: {} } |
|
2018-04-18 09:32:40 -04:00
|
|
|
{ type: 'activitypub-follow', payload: ActivitypubFollowPayload } |
|
2018-06-07 03:43:18 -04:00
|
|
|
{ type: 'video-file-import', payload: VideoFileImportPayload } |
|
2019-03-19 12:00:08 -04:00
|
|
|
{ type: 'video-transcoding', payload: VideoTranscodingPayload } |
|
2018-08-02 09:34:09 -04:00
|
|
|
{ type: 'email', payload: EmailPayload } |
|
2023-04-21 08:55:10 -04:00
|
|
|
{ type: 'transcoding-job-builder', payload: TranscodingJobBuilderPayload } |
|
2018-08-29 10:26:25 -04:00
|
|
|
{ type: 'video-import', payload: VideoImportPayload } |
|
2018-11-20 04:05:51 -05:00
|
|
|
{ type: 'activitypub-refresher', payload: RefreshPayload } |
|
2021-11-09 04:11:20 -05:00
|
|
|
{ type: 'videos-views-stats', payload: {} } |
|
2020-09-25 04:04:21 -04:00
|
|
|
{ type: 'video-live-ending', payload: VideoLiveEndingPayload } |
|
2021-02-26 08:22:25 -05:00
|
|
|
{ type: 'actor-keys', payload: ActorKeysPayload } |
|
2021-08-17 02:26:20 -04:00
|
|
|
{ type: 'video-redundancy', payload: VideoRedundancyPayload } |
|
2021-10-25 11:42:20 -04:00
|
|
|
{ type: 'delete-resumable-upload-meta-file', payload: DeleteResumableUploadMetaFilePayload } |
|
2022-03-22 11:58:49 -04:00
|
|
|
{ type: 'video-studio-edition', payload: VideoStudioEditionPayload } |
|
2022-03-16 10:34:21 -04:00
|
|
|
{ type: 'manage-video-torrent', payload: ManageVideoTorrentPayload } |
|
2022-08-10 03:53:39 -04:00
|
|
|
{ type: 'move-to-object-storage', payload: MoveObjectStoragePayload } |
|
|
|
|
{ type: 'video-channel-import', payload: VideoChannelImportPayload } |
|
|
|
|
{ type: 'after-video-channel-import', payload: AfterVideoChannelImportPayload } |
|
2022-08-08 09:48:17 -04:00
|
|
|
{ type: 'notify', payload: NotifyPayload } |
|
|
|
|
{ type: 'move-to-object-storage', payload: MoveObjectStoragePayload } |
|
2023-06-01 08:51:16 -04:00
|
|
|
{ type: 'federate-video', payload: FederateVideoPayload } |
|
|
|
|
{ type: 'generate-video-storyboard', payload: GenerateStoryboardPayload }
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2021-08-17 02:26:20 -04:00
|
|
|
export type CreateJobOptions = {
|
2020-09-25 04:04:21 -04:00
|
|
|
delay?: number
|
2021-01-21 10:57:21 -05:00
|
|
|
priority?: number
|
2023-05-02 07:51:06 -04:00
|
|
|
failParentOnFailure?: boolean
|
2020-09-25 04:04:21 -04:00
|
|
|
}
|
|
|
|
|
2021-08-27 08:32:44 -04:00
|
|
|
const handlers: { [id in JobType]: (job: Job) => Promise<any> } = {
|
2021-02-26 10:26:27 -05:00
|
|
|
'activitypub-cleaner': processActivityPubCleaner,
|
2018-04-18 09:32:40 -04:00
|
|
|
'activitypub-follow': processActivityPubFollow,
|
2023-04-21 08:55:10 -04:00
|
|
|
'activitypub-http-broadcast-parallel': processActivityPubParallelHttpBroadcast,
|
|
|
|
'activitypub-http-broadcast': processActivityPubHttpSequentialBroadcast,
|
|
|
|
'activitypub-http-fetcher': processActivityPubHttpFetcher,
|
|
|
|
'activitypub-http-unicast': processActivityPubHttpUnicast,
|
|
|
|
'activitypub-refresher': refreshAPObject,
|
|
|
|
'actor-keys': processActorKeys,
|
|
|
|
'after-video-channel-import': processAfterVideoChannelImport,
|
2018-08-02 09:34:09 -04:00
|
|
|
'email': processEmail,
|
2023-04-21 08:55:10 -04:00
|
|
|
'federate-video': processFederateVideo,
|
|
|
|
'transcoding-job-builder': processTranscodingJobBuilder,
|
|
|
|
'manage-video-torrent': processManageVideoTorrent,
|
|
|
|
'move-to-object-storage': processMoveToObjectStorage,
|
|
|
|
'notify': processNotify,
|
|
|
|
'video-channel-import': processVideoChannelImport,
|
|
|
|
'video-file-import': processVideoFileImport,
|
2018-08-29 10:26:25 -04:00
|
|
|
'video-import': processVideoImport,
|
2020-09-25 04:04:21 -04:00
|
|
|
'video-live-ending': processVideoLiveEnding,
|
2021-08-17 02:26:20 -04:00
|
|
|
'video-redundancy': processVideoRedundancy,
|
2022-08-08 09:48:17 -04:00
|
|
|
'video-studio-edition': processVideoStudioEdition,
|
2023-04-21 08:55:10 -04:00
|
|
|
'video-transcoding': processVideoTranscoding,
|
2023-06-01 08:51:16 -04:00
|
|
|
'videos-views-stats': processVideosViewsStats,
|
|
|
|
'generate-video-storyboard': processGenerateStoryboard
|
2018-01-25 09:05:18 -05:00
|
|
|
}
|
|
|
|
|
2022-06-16 08:29:53 -04:00
|
|
|
const errorHandlers: { [id in JobType]?: (job: Job, err: any) => Promise<any> } = {
|
|
|
|
'move-to-object-storage': onMoveToObjectStorageFailure
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:02:20 -04:00
|
|
|
const jobTypes: JobType[] = [
|
2023-04-21 08:55:10 -04:00
|
|
|
'activitypub-cleaner',
|
2018-07-10 11:02:20 -04:00
|
|
|
'activitypub-follow',
|
2022-06-17 08:08:13 -04:00
|
|
|
'activitypub-http-broadcast-parallel',
|
2023-04-21 08:55:10 -04:00
|
|
|
'activitypub-http-broadcast',
|
2018-05-09 03:08:22 -04:00
|
|
|
'activitypub-http-fetcher',
|
2018-07-10 11:02:20 -04:00
|
|
|
'activitypub-http-unicast',
|
2023-04-21 08:55:10 -04:00
|
|
|
'activitypub-refresher',
|
|
|
|
'actor-keys',
|
|
|
|
'after-video-channel-import',
|
2018-07-10 11:02:20 -04:00
|
|
|
'email',
|
2023-04-21 08:55:10 -04:00
|
|
|
'federate-video',
|
2023-06-01 08:51:16 -04:00
|
|
|
'generate-video-storyboard',
|
2023-04-21 08:55:10 -04:00
|
|
|
'manage-video-torrent',
|
|
|
|
'move-to-object-storage',
|
|
|
|
'notify',
|
2023-06-01 08:51:16 -04:00
|
|
|
'transcoding-job-builder',
|
2023-04-21 08:55:10 -04:00
|
|
|
'video-channel-import',
|
2018-08-02 09:34:09 -04:00
|
|
|
'video-file-import',
|
2018-08-29 10:26:25 -04:00
|
|
|
'video-import',
|
2021-08-17 02:26:20 -04:00
|
|
|
'video-live-ending',
|
2023-04-21 08:55:10 -04:00
|
|
|
'video-redundancy',
|
2022-08-08 09:48:17 -04:00
|
|
|
'video-studio-edition',
|
2023-04-21 08:55:10 -04:00
|
|
|
'video-transcoding',
|
|
|
|
'videos-views-stats'
|
2018-05-09 03:08:22 -04:00
|
|
|
]
|
|
|
|
|
2022-05-18 06:01:02 -04:00
|
|
|
const silentFailure = new Set<JobType>([ 'activitypub-http-unicast' ])
|
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
class JobQueue {
|
|
|
|
|
|
|
|
private static instance: JobQueue
|
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
private workers: { [id in JobType]?: Worker } = {}
|
2021-08-27 08:32:44 -04:00
|
|
|
private queues: { [id in JobType]?: Queue } = {}
|
2022-08-08 04:42:08 -04:00
|
|
|
private queueEvents: { [id in JobType]?: QueueEvents } = {}
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
private flowProducer: FlowProducer
|
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
private initialized = false
|
2018-02-27 10:57:53 -05:00
|
|
|
private jobRedisPrefix: string
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2020-01-31 10:56:52 -05:00
|
|
|
private constructor () {
|
|
|
|
}
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2022-09-14 05:35:58 -04:00
|
|
|
init () {
|
2018-01-25 09:05:18 -05:00
|
|
|
// Already initialized
|
|
|
|
if (this.initialized === true) return
|
|
|
|
this.initialized = true
|
|
|
|
|
2019-04-11 05:33:44 -04:00
|
|
|
this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST
|
2022-01-14 09:06:33 -05:00
|
|
|
|
2023-01-09 08:42:52 -05:00
|
|
|
for (const handlerName of Object.keys(handlers)) {
|
2022-09-14 05:35:58 -04:00
|
|
|
this.buildWorker(handlerName)
|
2022-08-08 04:42:08 -04:00
|
|
|
this.buildQueue(handlerName)
|
2022-09-14 05:35:58 -04:00
|
|
|
this.buildQueueEvent(handlerName)
|
2022-08-08 04:42:08 -04:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
this.flowProducer = new FlowProducer({
|
2022-11-14 10:26:20 -05:00
|
|
|
connection: Redis.getRedisClientOptions('FlowProducer'),
|
2022-08-08 09:48:17 -04:00
|
|
|
prefix: this.jobRedisPrefix
|
|
|
|
})
|
2022-08-11 05:30:06 -04:00
|
|
|
this.flowProducer.on('error', err => { logger.error('Error in flow producer', { err }) })
|
2022-08-08 09:48:17 -04:00
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
this.addRepeatableJobs()
|
|
|
|
}
|
|
|
|
|
2022-09-14 05:35:58 -04:00
|
|
|
private buildWorker (handlerName: JobType) {
|
2022-08-08 04:42:08 -04:00
|
|
|
const workerOptions: WorkerOptions = {
|
2022-09-14 05:35:58 -04:00
|
|
|
autorun: false,
|
2022-08-08 04:42:08 -04:00
|
|
|
concurrency: this.getJobConcurrency(handlerName),
|
2018-02-27 10:57:53 -05:00
|
|
|
prefix: this.jobRedisPrefix,
|
2023-02-16 08:10:11 -05:00
|
|
|
connection: Redis.getRedisClientOptions('Worker'),
|
|
|
|
maxStalledCount: 10
|
2018-07-10 11:02:20 -04:00
|
|
|
}
|
2018-01-30 07:27:07 -05:00
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
const handler = function (job: Job) {
|
|
|
|
const timeout = JOB_TTL[handlerName]
|
|
|
|
const p = handlers[handlerName](job)
|
2021-11-09 05:05:35 -05:00
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
if (!timeout) return p
|
2021-11-09 05:05:35 -05:00
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
return timeoutPromise(p, timeout)
|
|
|
|
}
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
const processor = async (jobArg: Job<any>) => {
|
|
|
|
const job = await Hooks.wrapObject(jobArg, 'filter:job-queue.process.params', { type: handlerName })
|
2022-08-02 09:29:00 -04:00
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
return Hooks.wrapPromiseFun(handler, job, 'filter:job-queue.process.result')
|
|
|
|
}
|
2018-08-03 03:27:30 -04:00
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
const worker = new Worker(handlerName, processor, workerOptions)
|
2022-05-18 06:01:02 -04:00
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
worker.on('failed', (job, err) => {
|
|
|
|
const logLevel = silentFailure.has(handlerName)
|
|
|
|
? 'debug'
|
|
|
|
: 'error'
|
2022-06-16 08:29:53 -04:00
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
logger.log(logLevel, 'Cannot execute job %s in queue %s.', job.id, handlerName, { payload: job.data, err })
|
2018-02-12 05:25:09 -05:00
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
if (errorHandlers[job.name]) {
|
|
|
|
errorHandlers[job.name](job, err)
|
|
|
|
.catch(err => logger.error('Cannot run error handler for job failure %d in queue %s.', job.id, handlerName, { err }))
|
|
|
|
}
|
|
|
|
})
|
2018-07-10 11:02:20 -04:00
|
|
|
|
2022-08-11 05:30:06 -04:00
|
|
|
worker.on('error', err => { logger.error('Error in job worker %s.', handlerName, { err }) })
|
2022-08-08 04:42:08 -04:00
|
|
|
|
|
|
|
this.workers[handlerName] = worker
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildQueue (handlerName: JobType) {
|
|
|
|
const queueOptions: QueueOptions = {
|
2022-11-14 10:26:20 -05:00
|
|
|
connection: Redis.getRedisClientOptions('Queue'),
|
2022-08-08 04:42:08 -04:00
|
|
|
prefix: this.jobRedisPrefix
|
2018-01-25 09:05:18 -05:00
|
|
|
}
|
2018-08-29 10:26:25 -04:00
|
|
|
|
2022-08-11 05:30:06 -04:00
|
|
|
const queue = new Queue(handlerName, queueOptions)
|
|
|
|
queue.on('error', err => { logger.error('Error in job queue %s.', handlerName, { err }) })
|
|
|
|
|
|
|
|
this.queues[handlerName] = queue
|
2022-08-08 04:42:08 -04:00
|
|
|
}
|
|
|
|
|
2022-09-14 05:35:58 -04:00
|
|
|
private buildQueueEvent (handlerName: JobType) {
|
2022-08-08 04:42:08 -04:00
|
|
|
const queueEventsOptions: QueueEventsOptions = {
|
2022-09-14 05:35:58 -04:00
|
|
|
autorun: false,
|
2022-11-14 10:26:20 -05:00
|
|
|
connection: Redis.getRedisClientOptions('QueueEvent'),
|
2022-08-08 04:42:08 -04:00
|
|
|
prefix: this.jobRedisPrefix
|
2018-07-30 12:49:54 -04:00
|
|
|
}
|
2022-08-11 05:30:06 -04:00
|
|
|
|
|
|
|
const queueEvents = new QueueEvents(handlerName, queueEventsOptions)
|
|
|
|
queueEvents.on('error', err => { logger.error('Error in job queue events %s.', handlerName, { err }) })
|
|
|
|
|
|
|
|
this.queueEvents[handlerName] = queueEvents
|
2022-08-08 04:42:08 -04:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-08-08 04:42:08 -04:00
|
|
|
async terminate () {
|
|
|
|
const promises = Object.keys(this.workers)
|
|
|
|
.map(handlerName => {
|
|
|
|
const worker: Worker = this.workers[handlerName]
|
|
|
|
const queue: Queue = this.queues[handlerName]
|
|
|
|
const queueEvent: QueueEvents = this.queueEvents[handlerName]
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
worker.close(false),
|
|
|
|
queue.close(),
|
|
|
|
queueEvent.close()
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
return Promise.all(promises)
|
2018-07-30 12:49:54 -04:00
|
|
|
}
|
|
|
|
|
2022-09-14 05:35:58 -04:00
|
|
|
start () {
|
|
|
|
const promises = Object.keys(this.workers)
|
|
|
|
.map(handlerName => {
|
|
|
|
const worker: Worker = this.workers[handlerName]
|
|
|
|
const queueEvent: QueueEvents = this.queueEvents[handlerName]
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
worker.run(),
|
|
|
|
queueEvent.run()
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
return Promise.all(promises)
|
|
|
|
}
|
|
|
|
|
2022-01-19 08:23:00 -05:00
|
|
|
async pause () {
|
2022-08-09 05:35:07 -04:00
|
|
|
for (const handlerName of Object.keys(this.workers)) {
|
|
|
|
const worker: Worker = this.workers[handlerName]
|
2022-08-08 04:42:08 -04:00
|
|
|
|
|
|
|
await worker.pause()
|
2022-01-19 08:23:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 07:21:18 -04:00
|
|
|
resume () {
|
2022-08-09 05:35:07 -04:00
|
|
|
for (const handlerName of Object.keys(this.workers)) {
|
|
|
|
const worker: Worker = this.workers[handlerName]
|
2022-08-08 04:42:08 -04:00
|
|
|
|
|
|
|
worker.resume()
|
2022-01-19 08:23:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
createJobAsync (options: CreateJobArgument & CreateJobOptions): void {
|
|
|
|
this.createJob(options)
|
|
|
|
.catch(err => logger.error('Cannot create job.', { err, options }))
|
2020-01-31 10:56:52 -05:00
|
|
|
}
|
|
|
|
|
2022-08-10 03:53:39 -04:00
|
|
|
createJob (options: CreateJobArgument & CreateJobOptions) {
|
2022-08-08 09:48:17 -04:00
|
|
|
const queue: Queue = this.queues[options.type]
|
2018-07-10 11:02:20 -04:00
|
|
|
if (queue === undefined) {
|
2022-08-08 09:48:17 -04:00
|
|
|
logger.error('Unknown queue %s: cannot create job.', options.type)
|
2020-01-31 10:56:52 -05:00
|
|
|
return
|
2018-07-10 11:02:20 -04:00
|
|
|
}
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
const jobOptions = this.buildJobOptions(options.type as JobType, pick(options, [ 'priority', 'delay' ]))
|
|
|
|
|
|
|
|
return queue.add('job', options.payload, jobOptions)
|
|
|
|
}
|
|
|
|
|
2022-08-10 03:53:39 -04:00
|
|
|
createSequentialJobFlow (...jobs: ((CreateJobArgument & CreateJobOptions) | undefined)[]) {
|
2022-08-08 09:48:17 -04:00
|
|
|
let lastJob: FlowJob
|
|
|
|
|
|
|
|
for (const job of jobs) {
|
|
|
|
if (!job) continue
|
|
|
|
|
|
|
|
lastJob = {
|
2022-08-09 03:09:31 -04:00
|
|
|
...this.buildJobFlowOption(job),
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
children: lastJob
|
|
|
|
? [ lastJob ]
|
|
|
|
: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.flowProducer.add(lastJob)
|
|
|
|
}
|
|
|
|
|
2022-08-10 03:53:39 -04:00
|
|
|
createJobWithChildren (parent: CreateJobArgument & CreateJobOptions, children: (CreateJobArgument & CreateJobOptions)[]) {
|
2022-08-09 03:09:31 -04:00
|
|
|
return this.flowProducer.add({
|
|
|
|
...this.buildJobFlowOption(parent),
|
|
|
|
|
|
|
|
children: children.map(c => this.buildJobFlowOption(c))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-23 11:13:01 -05:00
|
|
|
private buildJobFlowOption (job: CreateJobArgument & CreateJobOptions): FlowJob {
|
2022-08-09 03:09:31 -04:00
|
|
|
return {
|
|
|
|
name: 'job',
|
|
|
|
data: job.payload,
|
|
|
|
queueName: job.type,
|
2023-05-02 07:51:06 -04:00
|
|
|
opts: {
|
|
|
|
failParentOnFailure: true,
|
|
|
|
|
|
|
|
...this.buildJobOptions(job.type as JobType, pick(job, [ 'priority', 'delay', 'failParentOnFailure' ]))
|
|
|
|
}
|
2022-08-09 03:09:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
private buildJobOptions (type: JobType, options: CreateJobOptions = {}): JobsOptions {
|
|
|
|
return {
|
2018-07-10 11:02:20 -04:00
|
|
|
backoff: { delay: 60 * 1000, type: 'exponential' },
|
2022-08-08 09:48:17 -04:00
|
|
|
attempts: JOB_ATTEMPTS[type],
|
2021-01-21 10:57:21 -05:00
|
|
|
priority: options.priority,
|
2022-11-23 11:13:01 -05:00
|
|
|
delay: options.delay,
|
|
|
|
|
|
|
|
...this.buildJobRemovalOptions(type)
|
2018-07-10 11:02:20 -04:00
|
|
|
}
|
2018-01-25 09:05:18 -05:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-12-04 08:49:59 -05:00
|
|
|
async listForApi (options: {
|
2020-12-14 06:00:35 -05:00
|
|
|
state?: JobState
|
2020-01-31 10:56:52 -05:00
|
|
|
start: number
|
|
|
|
count: number
|
|
|
|
asc?: boolean
|
2019-12-04 08:49:59 -05:00
|
|
|
jobType: JobType
|
2021-08-27 08:32:44 -04:00
|
|
|
}): Promise<Job[]> {
|
2020-12-14 06:00:35 -05:00
|
|
|
const { state, start, count, asc, jobType } = options
|
|
|
|
|
2022-08-09 05:35:07 -04:00
|
|
|
const states = this.buildStateFilter(state)
|
|
|
|
const filteredJobTypes = this.buildTypeFilter(jobType)
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2022-08-09 05:35:07 -04:00
|
|
|
let results: Job[] = []
|
2019-12-04 08:49:59 -05:00
|
|
|
|
|
|
|
for (const jobType of filteredJobTypes) {
|
2022-08-08 04:42:08 -04:00
|
|
|
const queue: Queue = this.queues[jobType]
|
|
|
|
|
2018-07-10 11:02:20 -04:00
|
|
|
if (queue === undefined) {
|
|
|
|
logger.error('Unknown queue %s to list jobs.', jobType)
|
|
|
|
continue
|
|
|
|
}
|
2018-02-27 10:57:53 -05:00
|
|
|
|
2020-12-14 06:00:35 -05:00
|
|
|
const jobs = await queue.getJobs(states, 0, start + count, asc)
|
2018-07-10 11:02:20 -04:00
|
|
|
results = results.concat(jobs)
|
|
|
|
}
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2018-07-10 11:02:20 -04: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 09:05:18 -05:00
|
|
|
|
2018-07-10 11:02:20 -04:00
|
|
|
return 1
|
2018-01-25 09:05:18 -05:00
|
|
|
})
|
|
|
|
|
2018-07-10 11:02:20 -04:00
|
|
|
if (asc === false) results.reverse()
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2018-07-10 11:02:20 -04:00
|
|
|
return results.slice(start, start + count)
|
2018-01-25 09:05:18 -05:00
|
|
|
}
|
|
|
|
|
2020-12-14 06:00:35 -05:00
|
|
|
async count (state: JobState, jobType?: JobType): Promise<number> {
|
|
|
|
const states = state ? [ state ] : jobStates
|
2022-08-09 05:35:07 -04:00
|
|
|
const filteredJobTypes = this.buildTypeFilter(jobType)
|
2018-02-12 05:25:09 -05:00
|
|
|
|
2022-08-09 05:35:07 -04:00
|
|
|
let total = 0
|
2019-12-04 08:49:59 -05:00
|
|
|
|
|
|
|
for (const type of filteredJobTypes) {
|
2020-01-31 10:56:52 -05:00
|
|
|
const queue = this.queues[type]
|
2018-07-10 11:02:20 -04:00
|
|
|
if (queue === undefined) {
|
|
|
|
logger.error('Unknown queue %s to count jobs.', type)
|
|
|
|
continue
|
|
|
|
}
|
2018-02-12 05:25:09 -05:00
|
|
|
|
2018-07-10 11:02:20 -04:00
|
|
|
const counts = await queue.getJobCounts()
|
2018-02-12 05:25:09 -05:00
|
|
|
|
2020-12-13 13:27:25 -05:00
|
|
|
for (const s of states) {
|
|
|
|
total += counts[s]
|
|
|
|
}
|
2018-07-10 11:02:20 -04:00
|
|
|
}
|
2018-02-12 05:25:09 -05:00
|
|
|
|
2018-07-10 11:02:20 -04:00
|
|
|
return total
|
2018-02-12 05:25:09 -05:00
|
|
|
}
|
|
|
|
|
2022-08-09 05:35:07 -04:00
|
|
|
private buildStateFilter (state?: JobState) {
|
|
|
|
if (!state) return jobStates
|
|
|
|
|
|
|
|
const states = [ state ]
|
|
|
|
|
|
|
|
// Include parent if filtering on waiting
|
|
|
|
if (state === 'waiting') states.push('waiting-children')
|
|
|
|
|
|
|
|
return states
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildTypeFilter (jobType?: JobType) {
|
|
|
|
if (!jobType) return jobTypes
|
|
|
|
|
|
|
|
return jobTypes.filter(t => t === jobType)
|
|
|
|
}
|
|
|
|
|
2022-07-05 09:43:21 -04:00
|
|
|
async getStats () {
|
|
|
|
const promises = jobTypes.map(async t => ({ jobType: t, counts: await this.queues[t].getJobCounts() }))
|
|
|
|
|
|
|
|
return Promise.all(promises)
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-12-20 08:31:11 -05:00
|
|
|
async removeOldJobs () {
|
2018-07-10 11:02:20 -04:00
|
|
|
for (const key of Object.keys(this.queues)) {
|
2022-08-08 04:42:08 -04:00
|
|
|
const queue: Queue = this.queues[key]
|
2022-11-28 09:16:06 -05:00
|
|
|
await queue.clean(parseDurationToMs('7 days'), 1000, 'completed')
|
|
|
|
await queue.clean(parseDurationToMs('7 days'), 1000, 'failed')
|
2018-07-10 11:02:20 -04:00
|
|
|
}
|
2018-02-27 10:57:53 -05:00
|
|
|
}
|
|
|
|
|
2018-08-29 10:26:25 -04:00
|
|
|
private addRepeatableJobs () {
|
2022-08-08 04:42:08 -04:00
|
|
|
this.queues['videos-views-stats'].add('job', {}, {
|
2022-11-23 11:13:01 -05:00
|
|
|
repeat: REPEAT_JOBS['videos-views-stats'],
|
|
|
|
|
|
|
|
...this.buildJobRemovalOptions('videos-views-stats')
|
2020-01-31 10:56:52 -05:00
|
|
|
}).catch(err => logger.error('Cannot add repeatable job.', { err }))
|
2021-02-26 10:26:27 -05:00
|
|
|
|
|
|
|
if (CONFIG.FEDERATION.VIDEOS.CLEANUP_REMOTE_INTERACTIONS) {
|
2022-08-08 04:42:08 -04:00
|
|
|
this.queues['activitypub-cleaner'].add('job', {}, {
|
2022-11-23 11:13:01 -05:00
|
|
|
repeat: REPEAT_JOBS['activitypub-cleaner'],
|
|
|
|
|
|
|
|
...this.buildJobRemovalOptions('activitypub-cleaner')
|
2021-02-26 10:26:27 -05:00
|
|
|
}).catch(err => logger.error('Cannot add repeatable job.', { err }))
|
|
|
|
}
|
2018-08-29 10:26:25 -04:00
|
|
|
}
|
|
|
|
|
2021-02-08 04:51:10 -05: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]
|
|
|
|
}
|
|
|
|
|
2022-11-23 11:13:01 -05:00
|
|
|
private buildJobRemovalOptions (queueName: string) {
|
|
|
|
return {
|
|
|
|
removeOnComplete: {
|
|
|
|
// Wants seconds
|
|
|
|
age: (JOB_REMOVAL_OPTIONS.SUCCESS[queueName] || JOB_REMOVAL_OPTIONS.SUCCESS.DEFAULT) / 1000,
|
|
|
|
|
|
|
|
count: JOB_REMOVAL_OPTIONS.COUNT
|
|
|
|
},
|
|
|
|
removeOnFail: {
|
|
|
|
// Wants seconds
|
|
|
|
age: (JOB_REMOVAL_OPTIONS.FAILURE[queueName] || JOB_REMOVAL_OPTIONS.FAILURE.DEFAULT) / 1000,
|
|
|
|
|
|
|
|
count: JOB_REMOVAL_OPTIONS.COUNT / 1000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2019-12-04 08:49:59 -05:00
|
|
|
jobTypes,
|
2018-01-25 09:05:18 -05:00
|
|
|
JobQueue
|
|
|
|
}
|