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

341 lines
11 KiB
TypeScript
Raw Normal View History

2021-08-27 07:32:44 -05:00
import Bull, { Job, JobOptions, Queue } from 'bull'
2020-12-14 05:00:35 -06:00
import { jobStates } from '@server/helpers/custom-validators/jobs'
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,
ActivitypubHttpFetcherPayload,
ActivitypubHttpUnicastPayload,
ActorKeysPayload,
DeleteResumableUploadMetaFilePayload,
EmailPayload,
2020-04-23 02:32:53 -05:00
JobState,
JobType,
ManageVideoTorrentPayload,
Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz <me@florianbigard.com>
2021-08-17 01:26:20 -05:00
MoveObjectStoragePayload,
RefreshPayload,
VideoFileImportPayload,
VideoImportPayload,
VideoLiveEndingPayload,
VideoRedundancyPayload,
2022-03-22 10:58:49 -05:00
VideoStudioEditionPayload,
VideoTranscodingPayload
2020-04-23 02:32:53 -05: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'
2022-08-02 08:29:00 -05:00
import { Hooks } from '../plugins/hooks'
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'
import { refreshAPObject } from './handlers/activitypub-refresher'
import { processActorKeys } from './handlers/actor-keys'
2020-12-14 05:00:35 -06:00
import { processEmail } from './handlers/email'
import { processManageVideoTorrent } from './handlers/manage-video-torrent'
import { onMoveToObjectStorageFailure, processMoveToObjectStorage } from './handlers/move-to-object-storage'
import { processVideoFileImport } from './handlers/video-file-import'
2020-12-14 05:00:35 -06:00
import { processVideoImport } from './handlers/video-import'
import { processVideoLiveEnding } from './handlers/video-live-ending'
2022-03-22 10:58:49 -05:00
import { processVideoStudioEdition } from './handlers/video-studio-edition'
2020-12-14 05:00:35 -06:00
import { processVideoTranscoding } from './handlers/video-transcoding'
import { processVideosViewsStats } from './handlers/video-views-stats'
type CreateJobArgument =
{ type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } |
2022-06-17 07:08:13 -05:00
{ type: 'activitypub-http-broadcast-parallel', 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 09:26:25 -05:00
{ type: 'video-import', payload: VideoImportPayload } |
{ type: 'activitypub-refresher', payload: RefreshPayload } |
{ type: 'videos-views-stats', payload: {} } |
{ type: 'video-live-ending', payload: VideoLiveEndingPayload } |
{ type: 'actor-keys', payload: ActorKeysPayload } |
Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz <me@florianbigard.com>
2021-08-17 01:26:20 -05:00
{ type: 'video-redundancy', payload: VideoRedundancyPayload } |
{ type: 'delete-resumable-upload-meta-file', payload: DeleteResumableUploadMetaFilePayload } |
2022-03-22 10:58:49 -05:00
{ type: 'video-studio-edition', payload: VideoStudioEditionPayload } |
{ type: 'manage-video-torrent', payload: ManageVideoTorrentPayload } |
Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz <me@florianbigard.com>
2021-08-17 01:26:20 -05:00
{ type: 'move-to-object-storage', payload: MoveObjectStoragePayload }
Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz <me@florianbigard.com>
2021-08-17 01:26:20 -05:00
export type CreateJobOptions = {
delay?: number
priority?: number
}
2021-08-27 07:32:44 -05:00
const handlers: { [id in JobType]: (job: Job) => Promise<any> } = {
'activitypub-http-broadcast': processActivityPubHttpBroadcast,
2022-06-17 07:08:13 -05:00
'activitypub-http-broadcast-parallel': 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 09:26:25 -05:00
'video-import': processVideoImport,
'videos-views-stats': processVideosViewsStats,
2020-01-10 03:11:28 -06:00
'activitypub-refresher': refreshAPObject,
'video-live-ending': processVideoLiveEnding,
'actor-keys': processActorKeys,
Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz <me@florianbigard.com>
2021-08-17 01:26:20 -05:00
'video-redundancy': processVideoRedundancy,
2022-02-11 03:51:33 -06:00
'move-to-object-storage': processMoveToObjectStorage,
'manage-video-torrent': processManageVideoTorrent,
2022-03-22 10:58:49 -05:00
'video-studio-edition': processVideoStudioEdition
}
const errorHandlers: { [id in JobType]?: (job: Job, err: any) => Promise<any> } = {
'move-to-object-storage': onMoveToObjectStorageFailure
}
2018-07-10 10:02:20 -05:00
const jobTypes: JobType[] = [
'activitypub-follow',
2018-05-09 02:08:22 -05:00
'activitypub-http-broadcast',
2022-06-17 07:08:13 -05:00
'activitypub-http-broadcast-parallel',
2018-05-09 02:08:22 -05:00
'activitypub-http-fetcher',
2018-07-10 10:02:20 -05:00
'activitypub-http-unicast',
'activitypub-cleaner',
2018-07-10 10:02:20 -05:00
'email',
'video-transcoding',
'video-file-import',
2018-08-29 09:26:25 -05:00
'video-import',
'videos-views-stats',
2020-01-10 03:11:28 -06:00
'activitypub-refresher',
'video-redundancy',
'actor-keys',
Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz <me@florianbigard.com>
2021-08-17 01:26:20 -05:00
'video-live-ending',
2022-02-11 03:51:33 -06:00
'move-to-object-storage',
'manage-video-torrent',
2022-03-22 10:58:49 -05:00
'video-studio-edition'
2018-05-09 02:08:22 -05:00
]
2022-05-18 05:01:02 -05:00
const silentFailure = new Set<JobType>([ 'activitypub-http-unicast' ])
class JobQueue {
private static instance: JobQueue
2021-08-27 07:32:44 -05:00
private queues: { [id in JobType]?: Queue } = {}
private initialized = false
private jobRedisPrefix: string
2020-01-31 09:56:52 -06:00
private constructor () {
}
init (produceOnly = false) {
// Already initialized
if (this.initialized === true) return
this.initialized = true
2019-04-11 04:33:44 -05:00
this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST
2022-01-14 08:06:33 -06:00
const queueOptions: Bull.QueueOptions = {
prefix: this.jobRedisPrefix,
2022-01-14 08:06:33 -06:00
redis: {
password: CONFIG.REDIS.AUTH,
db: CONFIG.REDIS.DB,
host: CONFIG.REDIS.HOSTNAME,
port: CONFIG.REDIS.PORT,
path: CONFIG.REDIS.SOCKET
},
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
for (const handlerName of (Object.keys(handlers) as JobType[])) {
2018-07-10 10:02:20 -05:00
const queue = new Bull(handlerName, queueOptions)
if (produceOnly) {
queue.pause(true)
.catch(err => logger.error('Cannot pause queue %s in produced only job queue', handlerName, { err }))
}
2018-07-10 10:02:20 -05:00
const handler = handlers[handlerName]
2022-08-02 08:29:00 -05:00
queue.process(this.getJobConcurrency(handlerName), async (jobArg: Job<any>) => {
const job = await Hooks.wrapObject(jobArg, 'filter:job-queue.process.params', { type: handlerName })
return Hooks.wrapPromiseFun(handler, job, 'filter:job-queue.process.result')
}).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) => {
2022-05-18 05:01:02 -05:00
const logLevel = silentFailure.has(handlerName)
? 'debug'
: 'error'
logger.log(logLevel, 'Cannot execute job %d in queue %s.', job.id, handlerName, { payload: job.data, err })
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-08-03 02:27:30 -05:00
})
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-07-10 10:02:20 -05:00
this.queues[handlerName] = queue
}
2018-08-29 09:26:25 -05:00
this.addRepeatableJobs()
}
2018-07-30 11:49:54 -05:00
terminate () {
for (const queueName of Object.keys(this.queues)) {
const queue = this.queues[queueName]
queue.close()
}
}
async pause () {
for (const handler of Object.keys(this.queues)) {
await this.queues[handler].pause(true)
}
}
async resume () {
for (const handler of Object.keys(this.queues)) {
await this.queues[handler].resume(true)
}
}
createJob (obj: CreateJobArgument, options: CreateJobOptions = {}): void {
this.createJobWithPromise(obj, options)
.catch(err => logger.error('Cannot create job.', { err, obj }))
2020-01-31 09:56:52 -06:00
}
createJobWithPromise (obj: CreateJobArgument, options: CreateJobOptions = {}) {
const queue: Queue = this.queues[obj.type]
2018-07-10 10:02:20 -05:00
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
}
2021-08-27 07:32:44 -05:00
const jobArgs: JobOptions = {
2018-07-10 10:02:20 -05:00
backoff: { delay: 60 * 1000, type: 'exponential' },
2018-08-03 03:19:51 -05:00
attempts: JOB_ATTEMPTS[obj.type],
timeout: JOB_TTL[obj.type],
priority: options.priority,
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)
}
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
2021-08-27 07:32:44 -05:00
}): Promise<Job[]> {
2020-12-14 05:00:35 -06:00
const { state, start, count, asc, jobType } = options
const states = state ? [ state ] : jobStates
2021-08-27 07:32:44 -05:00
let results: Job[] = []
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
}
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-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-07-10 10:02:20 -05:00
return 1
})
2018-07-10 10:02:20 -05:00
if (asc === false) results.reverse()
2018-07-10 10:02:20 -05:00
return results.slice(start, start + count)
}
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
}
2022-07-05 08:43:21 -05:00
async getStats () {
const promises = jobTypes.map(async t => ({ jobType: t, counts: await this.queues[t].getJobCounts() }))
return Promise.all(promises)
}
async removeOldJobs () {
2018-07-10 10:02:20 -05:00
for (const key of Object.keys(this.queues)) {
const queue = this.queues[key]
await queue.clean(JOB_COMPLETED_LIFETIME, 'completed')
2018-07-10 10:02:20 -05:00
}
}
2018-08-29 09:26:25 -05:00
private addRepeatableJobs () {
this.queues['videos-views-stats'].add({}, {
repeat: REPEAT_JOBS['videos-views-stats']
2020-01-31 09:56:52 -06: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 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)
}
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 07:49:59 -06:00
jobTypes,
JobQueue
}