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