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