]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import Bull, { Job, JobOptions, Queue } from 'bull' |
402145b8 | 2 | import { jobStates } from '@server/helpers/custom-validators/jobs' |
9129b769 | 3 | import { CONFIG } from '@server/initializers/config' |
402145b8 | 4 | import { processVideoRedundancy } from '@server/lib/job-queue/handlers/video-redundancy' |
8dc8a34e C |
5 | import { |
6 | ActivitypubFollowPayload, | |
7 | ActivitypubHttpBroadcastPayload, | |
e1c55031 C |
8 | ActivitypubHttpFetcherPayload, |
9 | ActivitypubHttpUnicastPayload, | |
8795d6f2 | 10 | ActorKeysPayload, |
276250f0 | 11 | DeleteResumableUploadMetaFilePayload, |
e1c55031 | 12 | EmailPayload, |
8dc8a34e | 13 | JobState, |
e1c55031 | 14 | JobType, |
0305db28 | 15 | MoveObjectStoragePayload, |
e1c55031 | 16 | RefreshPayload, |
c729caf6 | 17 | VideoEditionPayload, |
e1c55031 C |
18 | VideoFileImportPayload, |
19 | VideoImportPayload, | |
a5cf76af | 20 | VideoLiveEndingPayload, |
e1c55031 C |
21 | VideoRedundancyPayload, |
22 | VideoTranscodingPayload | |
8dc8a34e | 23 | } from '../../../shared/models' |
94a5ff8a | 24 | import { logger } from '../../helpers/logger' |
74dc3bca | 25 | import { JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_JOBS, WEBSERVER } from '../../initializers/constants' |
74d249bc | 26 | import { processActivityPubCleaner } from './handlers/activitypub-cleaner' |
402145b8 | 27 | import { processActivityPubFollow } from './handlers/activitypub-follow' |
8dc8a34e C |
28 | import { processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast' |
29 | import { processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher' | |
30 | import { processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast' | |
e1c55031 | 31 | import { refreshAPObject } from './handlers/activitypub-refresher' |
8795d6f2 | 32 | import { processActorKeys } from './handlers/actor-keys' |
402145b8 | 33 | import { processEmail } from './handlers/email' |
41fb13c3 | 34 | import { processMoveToObjectStorage } from './handlers/move-to-object-storage' |
c729caf6 | 35 | import { processVideoEdition } from './handlers/video-edition' |
e1c55031 | 36 | import { processVideoFileImport } from './handlers/video-file-import' |
402145b8 | 37 | import { processVideoImport } from './handlers/video-import' |
a5cf76af | 38 | import { processVideoLiveEnding } from './handlers/video-live-ending' |
402145b8 | 39 | import { processVideoTranscoding } from './handlers/video-transcoding' |
51353d9a | 40 | import { processVideosViewsStats } from './handlers/video-views-stats' |
94a5ff8a C |
41 | |
42 | type CreateJobArgument = | |
43 | { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } | | |
44 | { type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload } | | |
45 | { type: 'activitypub-http-fetcher', payload: ActivitypubHttpFetcherPayload } | | |
74d249bc | 46 | { type: 'activitypub-http-cleaner', payload: {} } | |
5350fd8e | 47 | { type: 'activitypub-follow', payload: ActivitypubFollowPayload } | |
28be8916 | 48 | { type: 'video-file-import', payload: VideoFileImportPayload } | |
a0327eed | 49 | { type: 'video-transcoding', payload: VideoTranscodingPayload } | |
fbad87b0 | 50 | { type: 'email', payload: EmailPayload } | |
6b616860 | 51 | { type: 'video-import', payload: VideoImportPayload } | |
04b8c3fb | 52 | { type: 'activitypub-refresher', payload: RefreshPayload } | |
51353d9a | 53 | { type: 'videos-views-stats', payload: {} } | |
a5cf76af | 54 | { type: 'video-live-ending', payload: VideoLiveEndingPayload } | |
8795d6f2 | 55 | { type: 'actor-keys', payload: ActorKeysPayload } | |
0305db28 | 56 | { type: 'video-redundancy', payload: VideoRedundancyPayload } | |
276250f0 | 57 | { type: 'delete-resumable-upload-meta-file', payload: DeleteResumableUploadMetaFilePayload } | |
c729caf6 | 58 | { type: 'video-edition', payload: VideoEditionPayload } | |
0305db28 | 59 | { type: 'move-to-object-storage', payload: MoveObjectStoragePayload } |
94a5ff8a | 60 | |
0305db28 | 61 | export type CreateJobOptions = { |
a5cf76af | 62 | delay?: number |
77d7e851 | 63 | priority?: number |
a5cf76af C |
64 | } |
65 | ||
41fb13c3 | 66 | const handlers: { [id in JobType]: (job: Job) => Promise<any> } = { |
94a5ff8a C |
67 | 'activitypub-http-broadcast': processActivityPubHttpBroadcast, |
68 | 'activitypub-http-unicast': processActivityPubHttpUnicast, | |
69 | 'activitypub-http-fetcher': processActivityPubHttpFetcher, | |
74d249bc | 70 | 'activitypub-cleaner': processActivityPubCleaner, |
5350fd8e | 71 | 'activitypub-follow': processActivityPubFollow, |
28be8916 | 72 | 'video-file-import': processVideoFileImport, |
a0327eed | 73 | 'video-transcoding': processVideoTranscoding, |
fbad87b0 | 74 | 'email': processEmail, |
6b616860 | 75 | 'video-import': processVideoImport, |
51353d9a | 76 | 'videos-views-stats': processVideosViewsStats, |
b764380a | 77 | 'activitypub-refresher': refreshAPObject, |
a5cf76af | 78 | 'video-live-ending': processVideoLiveEnding, |
8795d6f2 | 79 | 'actor-keys': processActorKeys, |
0305db28 | 80 | 'video-redundancy': processVideoRedundancy, |
c729caf6 C |
81 | 'move-to-object-storage': processMoveToObjectStorage, |
82 | 'video-edition': processVideoEdition | |
94a5ff8a C |
83 | } |
84 | ||
94831479 C |
85 | const jobTypes: JobType[] = [ |
86 | 'activitypub-follow', | |
71e3dfda | 87 | 'activitypub-http-broadcast', |
71e3dfda | 88 | 'activitypub-http-fetcher', |
94831479 | 89 | 'activitypub-http-unicast', |
74d249bc | 90 | 'activitypub-cleaner', |
94831479 | 91 | 'email', |
a0327eed | 92 | 'video-transcoding', |
fbad87b0 | 93 | 'video-file-import', |
6b616860 | 94 | 'video-import', |
51353d9a | 95 | 'videos-views-stats', |
b764380a | 96 | 'activitypub-refresher', |
a5cf76af | 97 | 'video-redundancy', |
8795d6f2 | 98 | 'actor-keys', |
0305db28 | 99 | 'video-live-ending', |
c729caf6 C |
100 | 'move-to-object-storage', |
101 | 'video-edition' | |
71e3dfda C |
102 | ] |
103 | ||
94a5ff8a C |
104 | class JobQueue { |
105 | ||
106 | private static instance: JobQueue | |
107 | ||
41fb13c3 | 108 | private queues: { [id in JobType]?: Queue } = {} |
94a5ff8a | 109 | private initialized = false |
2c29ad4f | 110 | private jobRedisPrefix: string |
94a5ff8a | 111 | |
a1587156 C |
112 | private constructor () { |
113 | } | |
94a5ff8a | 114 | |
e1ab52d7 | 115 | init (produceOnly = false) { |
94a5ff8a C |
116 | // Already initialized |
117 | if (this.initialized === true) return | |
118 | this.initialized = true | |
119 | ||
6dd9de95 | 120 | this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST |
ff4d2c73 C |
121 | |
122 | const queueOptions: Bull.QueueOptions = { | |
2c29ad4f | 123 | prefix: this.jobRedisPrefix, |
ff4d2c73 C |
124 | redis: { |
125 | password: CONFIG.REDIS.AUTH, | |
126 | db: CONFIG.REDIS.DB, | |
127 | host: CONFIG.REDIS.HOSTNAME, | |
128 | port: CONFIG.REDIS.PORT, | |
129 | path: CONFIG.REDIS.SOCKET | |
130 | }, | |
4a9e71c2 C |
131 | settings: { |
132 | maxStalledCount: 10 // transcoding could be long, so jobs can often be interrupted by restarts | |
133 | } | |
94831479 | 134 | } |
ecb4e35f | 135 | |
9129b769 | 136 | for (const handlerName of (Object.keys(handlers) as JobType[])) { |
94831479 | 137 | const queue = new Bull(handlerName, queueOptions) |
e1ab52d7 | 138 | |
139 | if (produceOnly) { | |
140 | queue.pause(true) | |
141 | .catch(err => logger.error('Cannot pause queue %s in produced only job queue', handlerName, { err })) | |
142 | } | |
143 | ||
94831479 | 144 | const handler = handlers[handlerName] |
94a5ff8a | 145 | |
9129b769 | 146 | queue.process(this.getJobConcurrency(handlerName), handler) |
2b86fe72 | 147 | .catch(err => logger.error('Error in job queue processor %s.', handlerName, { err })) |
d7f83948 C |
148 | |
149 | queue.on('failed', (job, err) => { | |
150 | logger.error('Cannot execute job %d in queue %s.', job.id, handlerName, { payload: job.data, err }) | |
151 | }) | |
3df45638 | 152 | |
94831479 C |
153 | queue.on('error', err => { |
154 | logger.error('Error in job queue %s.', handlerName, { err }) | |
94a5ff8a | 155 | }) |
94831479 C |
156 | |
157 | this.queues[handlerName] = queue | |
94a5ff8a | 158 | } |
6b616860 C |
159 | |
160 | this.addRepeatableJobs() | |
94a5ff8a C |
161 | } |
162 | ||
14f2b3ad C |
163 | terminate () { |
164 | for (const queueName of Object.keys(this.queues)) { | |
165 | const queue = this.queues[queueName] | |
166 | queue.close() | |
167 | } | |
168 | } | |
169 | ||
419b520c C |
170 | async pause () { |
171 | for (const handler of Object.keys(this.queues)) { | |
172 | await this.queues[handler].pause(true) | |
173 | } | |
174 | } | |
175 | ||
176 | async resume () { | |
177 | for (const handler of Object.keys(this.queues)) { | |
178 | await this.queues[handler].resume(true) | |
179 | } | |
180 | } | |
181 | ||
a5cf76af C |
182 | createJob (obj: CreateJobArgument, options: CreateJobOptions = {}): void { |
183 | this.createJobWithPromise(obj, options) | |
e1c55031 | 184 | .catch(err => logger.error('Cannot create job.', { err, obj })) |
a1587156 C |
185 | } |
186 | ||
a5cf76af | 187 | createJobWithPromise (obj: CreateJobArgument, options: CreateJobOptions = {}) { |
94831479 C |
188 | const queue = this.queues[obj.type] |
189 | if (queue === undefined) { | |
190 | logger.error('Unknown queue %s: cannot create job.', obj.type) | |
a1587156 | 191 | return |
94831479 | 192 | } |
94a5ff8a | 193 | |
41fb13c3 | 194 | const jobArgs: JobOptions = { |
94831479 | 195 | backoff: { delay: 60 * 1000, type: 'exponential' }, |
2b86fe72 | 196 | attempts: JOB_ATTEMPTS[obj.type], |
a5cf76af | 197 | timeout: JOB_TTL[obj.type], |
77d7e851 | 198 | priority: options.priority, |
a5cf76af | 199 | delay: options.delay |
94831479 | 200 | } |
71e3dfda | 201 | |
94831479 | 202 | return queue.add(obj.payload, jobArgs) |
94a5ff8a C |
203 | } |
204 | ||
1061c73f | 205 | async listForApi (options: { |
402145b8 | 206 | state?: JobState |
a1587156 C |
207 | start: number |
208 | count: number | |
209 | asc?: boolean | |
1061c73f | 210 | jobType: JobType |
41fb13c3 | 211 | }): Promise<Job[]> { |
402145b8 C |
212 | const { state, start, count, asc, jobType } = options |
213 | ||
214 | const states = state ? [ state ] : jobStates | |
41fb13c3 | 215 | let results: Job[] = [] |
94a5ff8a | 216 | |
1061c73f C |
217 | const filteredJobTypes = this.filterJobTypes(jobType) |
218 | ||
1061c73f | 219 | for (const jobType of filteredJobTypes) { |
a1587156 | 220 | const queue = this.queues[jobType] |
94831479 C |
221 | if (queue === undefined) { |
222 | logger.error('Unknown queue %s to list jobs.', jobType) | |
223 | continue | |
224 | } | |
2c29ad4f | 225 | |
402145b8 | 226 | const jobs = await queue.getJobs(states, 0, start + count, asc) |
94831479 C |
227 | results = results.concat(jobs) |
228 | } | |
94a5ff8a | 229 | |
94831479 C |
230 | results.sort((j1: any, j2: any) => { |
231 | if (j1.timestamp < j2.timestamp) return -1 | |
232 | else if (j1.timestamp === j2.timestamp) return 0 | |
94a5ff8a | 233 | |
94831479 | 234 | return 1 |
94a5ff8a | 235 | }) |
94a5ff8a | 236 | |
94831479 | 237 | if (asc === false) results.reverse() |
94a5ff8a | 238 | |
94831479 | 239 | return results.slice(start, start + count) |
94a5ff8a C |
240 | } |
241 | ||
402145b8 C |
242 | async count (state: JobState, jobType?: JobType): Promise<number> { |
243 | const states = state ? [ state ] : jobStates | |
94831479 | 244 | let total = 0 |
3df45638 | 245 | |
1061c73f C |
246 | const filteredJobTypes = this.filterJobTypes(jobType) |
247 | ||
248 | for (const type of filteredJobTypes) { | |
a1587156 | 249 | const queue = this.queues[type] |
94831479 C |
250 | if (queue === undefined) { |
251 | logger.error('Unknown queue %s to count jobs.', type) | |
252 | continue | |
253 | } | |
3df45638 | 254 | |
94831479 | 255 | const counts = await queue.getJobCounts() |
3df45638 | 256 | |
040d6896 RK |
257 | for (const s of states) { |
258 | total += counts[s] | |
259 | } | |
94831479 | 260 | } |
3df45638 | 261 | |
94831479 | 262 | return total |
3df45638 C |
263 | } |
264 | ||
2f5c6b2f | 265 | async removeOldJobs () { |
94831479 C |
266 | for (const key of Object.keys(this.queues)) { |
267 | const queue = this.queues[key] | |
2f5c6b2f | 268 | await queue.clean(JOB_COMPLETED_LIFETIME, 'completed') |
94831479 | 269 | } |
2c29ad4f C |
270 | } |
271 | ||
6b616860 | 272 | private addRepeatableJobs () { |
51353d9a C |
273 | this.queues['videos-views-stats'].add({}, { |
274 | repeat: REPEAT_JOBS['videos-views-stats'] | |
a1587156 | 275 | }).catch(err => logger.error('Cannot add repeatable job.', { err })) |
74d249bc C |
276 | |
277 | if (CONFIG.FEDERATION.VIDEOS.CLEANUP_REMOTE_INTERACTIONS) { | |
278 | this.queues['activitypub-cleaner'].add({}, { | |
279 | repeat: REPEAT_JOBS['activitypub-cleaner'] | |
280 | }).catch(err => logger.error('Cannot add repeatable job.', { err })) | |
281 | } | |
6b616860 C |
282 | } |
283 | ||
1061c73f C |
284 | private filterJobTypes (jobType?: JobType) { |
285 | if (!jobType) return jobTypes | |
286 | ||
287 | return jobTypes.filter(t => t === jobType) | |
288 | } | |
289 | ||
9129b769 C |
290 | private getJobConcurrency (jobType: JobType) { |
291 | if (jobType === 'video-transcoding') return CONFIG.TRANSCODING.CONCURRENCY | |
292 | if (jobType === 'video-import') return CONFIG.IMPORT.VIDEOS.CONCURRENCY | |
293 | ||
294 | return JOB_CONCURRENCY[jobType] | |
295 | } | |
296 | ||
94a5ff8a C |
297 | static get Instance () { |
298 | return this.instance || (this.instance = new this()) | |
299 | } | |
300 | } | |
301 | ||
302 | // --------------------------------------------------------------------------- | |
303 | ||
304 | export { | |
1061c73f | 305 | jobTypes, |
94a5ff8a C |
306 | JobQueue |
307 | } |