]>
Commit | Line | Data |
---|---|---|
94831479 | 1 | import * as Bull from 'bull' |
628d28e8 | 2 | import { JobState, JobType } from '../../../shared/models' |
94a5ff8a | 3 | import { logger } from '../../helpers/logger' |
19f7b248 | 4 | import { Redis } from '../redis' |
74dc3bca | 5 | import { JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_JOBS, WEBSERVER } from '../../initializers/constants' |
94a5ff8a C |
6 | import { ActivitypubHttpBroadcastPayload, processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast' |
7 | import { ActivitypubHttpFetcherPayload, processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher' | |
8 | import { ActivitypubHttpUnicastPayload, processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast' | |
ecb4e35f | 9 | import { EmailPayload, processEmail } from './handlers/email' |
30842128 | 10 | import { processVideoTranscoding, VideoTranscodingPayload } from './handlers/video-transcoding' |
5350fd8e | 11 | import { ActivitypubFollowPayload, processActivityPubFollow } from './handlers/activitypub-follow' |
fbad87b0 | 12 | import { processVideoImport, VideoImportPayload } from './handlers/video-import' |
030177d2 | 13 | import { processVideosViews } from './handlers/video-views' |
04b8c3fb | 14 | import { refreshAPObject, RefreshPayload } from './handlers/activitypub-refresher' |
30842128 | 15 | import { processVideoFileImport, VideoFileImportPayload } from './handlers/video-file-import' |
b764380a | 16 | import { processVideoRedundancy, VideoRedundancyPayload } from '@server/lib/job-queue/handlers/video-redundancy' |
94a5ff8a C |
17 | |
18 | type CreateJobArgument = | |
19 | { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } | | |
20 | { type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload } | | |
21 | { type: 'activitypub-http-fetcher', payload: ActivitypubHttpFetcherPayload } | | |
5350fd8e | 22 | { type: 'activitypub-follow', payload: ActivitypubFollowPayload } | |
28be8916 | 23 | { type: 'video-file-import', payload: VideoFileImportPayload } | |
a0327eed | 24 | { type: 'video-transcoding', payload: VideoTranscodingPayload } | |
fbad87b0 | 25 | { type: 'email', payload: EmailPayload } | |
6b616860 | 26 | { type: 'video-import', payload: VideoImportPayload } | |
04b8c3fb | 27 | { type: 'activitypub-refresher', payload: RefreshPayload } | |
b764380a C |
28 | { type: 'videos-views', payload: {} } | |
29 | { type: 'video-redundancy', payload: VideoRedundancyPayload } | |
94a5ff8a | 30 | |
a1587156 | 31 | const handlers: { [id in JobType]: (job: Bull.Job) => Promise<any> } = { |
94a5ff8a C |
32 | 'activitypub-http-broadcast': processActivityPubHttpBroadcast, |
33 | 'activitypub-http-unicast': processActivityPubHttpUnicast, | |
34 | 'activitypub-http-fetcher': processActivityPubHttpFetcher, | |
5350fd8e | 35 | 'activitypub-follow': processActivityPubFollow, |
28be8916 | 36 | 'video-file-import': processVideoFileImport, |
a0327eed | 37 | 'video-transcoding': processVideoTranscoding, |
fbad87b0 | 38 | 'email': processEmail, |
6b616860 | 39 | 'video-import': processVideoImport, |
04b8c3fb | 40 | 'videos-views': processVideosViews, |
b764380a C |
41 | 'activitypub-refresher': refreshAPObject, |
42 | 'video-redundancy': processVideoRedundancy | |
94a5ff8a C |
43 | } |
44 | ||
94831479 C |
45 | const jobTypes: JobType[] = [ |
46 | 'activitypub-follow', | |
71e3dfda | 47 | 'activitypub-http-broadcast', |
71e3dfda | 48 | 'activitypub-http-fetcher', |
94831479 C |
49 | 'activitypub-http-unicast', |
50 | 'email', | |
a0327eed | 51 | 'video-transcoding', |
fbad87b0 | 52 | 'video-file-import', |
6b616860 | 53 | 'video-import', |
04b8c3fb | 54 | 'videos-views', |
b764380a C |
55 | 'activitypub-refresher', |
56 | 'video-redundancy' | |
71e3dfda C |
57 | ] |
58 | ||
94a5ff8a C |
59 | class JobQueue { |
60 | ||
61 | private static instance: JobQueue | |
62 | ||
a1587156 | 63 | private queues: { [id in JobType]?: Bull.Queue } = {} |
94a5ff8a | 64 | private initialized = false |
2c29ad4f | 65 | private jobRedisPrefix: string |
94a5ff8a | 66 | |
a1587156 C |
67 | private constructor () { |
68 | } | |
94a5ff8a | 69 | |
a1587156 | 70 | init () { |
94a5ff8a C |
71 | // Already initialized |
72 | if (this.initialized === true) return | |
73 | this.initialized = true | |
74 | ||
6dd9de95 | 75 | this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST |
94831479 | 76 | const queueOptions = { |
2c29ad4f | 77 | prefix: this.jobRedisPrefix, |
47f6409b | 78 | redis: Redis.getRedisClientOptions(), |
4a9e71c2 C |
79 | settings: { |
80 | maxStalledCount: 10 // transcoding could be long, so jobs can often be interrupted by restarts | |
81 | } | |
94831479 | 82 | } |
ecb4e35f | 83 | |
94831479 C |
84 | for (const handlerName of Object.keys(handlers)) { |
85 | const queue = new Bull(handlerName, queueOptions) | |
86 | const handler = handlers[handlerName] | |
94a5ff8a | 87 | |
94831479 | 88 | queue.process(JOB_CONCURRENCY[handlerName], handler) |
2b86fe72 | 89 | .catch(err => logger.error('Error in job queue processor %s.', handlerName, { err })) |
d7f83948 C |
90 | |
91 | queue.on('failed', (job, err) => { | |
92 | logger.error('Cannot execute job %d in queue %s.', job.id, handlerName, { payload: job.data, err }) | |
93 | }) | |
3df45638 | 94 | |
94831479 C |
95 | queue.on('error', err => { |
96 | logger.error('Error in job queue %s.', handlerName, { err }) | |
94a5ff8a | 97 | }) |
94831479 C |
98 | |
99 | this.queues[handlerName] = queue | |
94a5ff8a | 100 | } |
6b616860 C |
101 | |
102 | this.addRepeatableJobs() | |
94a5ff8a C |
103 | } |
104 | ||
14f2b3ad C |
105 | terminate () { |
106 | for (const queueName of Object.keys(this.queues)) { | |
107 | const queue = this.queues[queueName] | |
108 | queue.close() | |
109 | } | |
110 | } | |
111 | ||
a1587156 C |
112 | createJob (obj: CreateJobArgument): void { |
113 | this.createJobWithPromise(obj) | |
114 | .catch(err => logger.error('Cannot create job.', { err, obj })) | |
115 | } | |
116 | ||
117 | createJobWithPromise (obj: CreateJobArgument) { | |
94831479 C |
118 | const queue = this.queues[obj.type] |
119 | if (queue === undefined) { | |
120 | logger.error('Unknown queue %s: cannot create job.', obj.type) | |
a1587156 | 121 | return |
94831479 | 122 | } |
94a5ff8a | 123 | |
94831479 C |
124 | const jobArgs: Bull.JobOptions = { |
125 | backoff: { delay: 60 * 1000, type: 'exponential' }, | |
2b86fe72 C |
126 | attempts: JOB_ATTEMPTS[obj.type], |
127 | timeout: JOB_TTL[obj.type] | |
94831479 | 128 | } |
71e3dfda | 129 | |
94831479 | 130 | return queue.add(obj.payload, jobArgs) |
94a5ff8a C |
131 | } |
132 | ||
1061c73f | 133 | async listForApi (options: { |
a1587156 C |
134 | state: JobState |
135 | start: number | |
136 | count: number | |
137 | asc?: boolean | |
1061c73f C |
138 | jobType: JobType |
139 | }): Promise<Bull.Job[]> { | |
140 | const { state, start, count, asc, jobType } = options | |
94831479 | 141 | let results: Bull.Job[] = [] |
94a5ff8a | 142 | |
1061c73f C |
143 | const filteredJobTypes = this.filterJobTypes(jobType) |
144 | ||
1061c73f | 145 | for (const jobType of filteredJobTypes) { |
a1587156 | 146 | const queue = this.queues[jobType] |
94831479 C |
147 | if (queue === undefined) { |
148 | logger.error('Unknown queue %s to list jobs.', jobType) | |
149 | continue | |
150 | } | |
2c29ad4f | 151 | |
0374b6b5 | 152 | const jobs = await queue.getJobs([ state ], 0, start + count, asc) |
94831479 C |
153 | results = results.concat(jobs) |
154 | } | |
94a5ff8a | 155 | |
94831479 C |
156 | results.sort((j1: any, j2: any) => { |
157 | if (j1.timestamp < j2.timestamp) return -1 | |
158 | else if (j1.timestamp === j2.timestamp) return 0 | |
94a5ff8a | 159 | |
94831479 | 160 | return 1 |
94a5ff8a | 161 | }) |
94a5ff8a | 162 | |
94831479 | 163 | if (asc === false) results.reverse() |
94a5ff8a | 164 | |
94831479 | 165 | return results.slice(start, start + count) |
94a5ff8a C |
166 | } |
167 | ||
1061c73f | 168 | async count (state: JobState, jobType?: JobType): Promise<number> { |
94831479 | 169 | let total = 0 |
3df45638 | 170 | |
1061c73f C |
171 | const filteredJobTypes = this.filterJobTypes(jobType) |
172 | ||
173 | for (const type of filteredJobTypes) { | |
a1587156 | 174 | const queue = this.queues[type] |
94831479 C |
175 | if (queue === undefined) { |
176 | logger.error('Unknown queue %s to count jobs.', type) | |
177 | continue | |
178 | } | |
3df45638 | 179 | |
94831479 | 180 | const counts = await queue.getJobCounts() |
3df45638 | 181 | |
a1587156 | 182 | total += counts[state] |
94831479 | 183 | } |
3df45638 | 184 | |
94831479 | 185 | return total |
3df45638 C |
186 | } |
187 | ||
2f5c6b2f | 188 | async removeOldJobs () { |
94831479 C |
189 | for (const key of Object.keys(this.queues)) { |
190 | const queue = this.queues[key] | |
2f5c6b2f | 191 | await queue.clean(JOB_COMPLETED_LIFETIME, 'completed') |
94831479 | 192 | } |
2c29ad4f C |
193 | } |
194 | ||
6b616860 C |
195 | private addRepeatableJobs () { |
196 | this.queues['videos-views'].add({}, { | |
197 | repeat: REPEAT_JOBS['videos-views'] | |
a1587156 | 198 | }).catch(err => logger.error('Cannot add repeatable job.', { err })) |
6b616860 C |
199 | } |
200 | ||
1061c73f C |
201 | private filterJobTypes (jobType?: JobType) { |
202 | if (!jobType) return jobTypes | |
203 | ||
204 | return jobTypes.filter(t => t === jobType) | |
205 | } | |
206 | ||
94a5ff8a C |
207 | static get Instance () { |
208 | return this.instance || (this.instance = new this()) | |
209 | } | |
210 | } | |
211 | ||
212 | // --------------------------------------------------------------------------- | |
213 | ||
214 | export { | |
1061c73f | 215 | jobTypes, |
94a5ff8a C |
216 | JobQueue |
217 | } |