]>
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 | 38 | import { processVideoTranscoding } from './handlers/video-transcoding' |
51353d9a | 39 | import { processVideosViewsStats } from './handlers/video-views-stats' |
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 } | |
51353d9a | 52 | { type: 'videos-views-stats', 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, |
51353d9a | 74 | 'videos-views-stats': processVideosViewsStats, |
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', |
51353d9a | 92 | 'videos-views-stats', |
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 | |
e1ab52d7 | 111 | init (produceOnly = false) { |
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 | 126 | const queue = new Bull(handlerName, queueOptions) |
e1ab52d7 | 127 | |
128 | if (produceOnly) { | |
129 | queue.pause(true) | |
130 | .catch(err => logger.error('Cannot pause queue %s in produced only job queue', handlerName, { err })) | |
131 | } | |
132 | ||
94831479 | 133 | const handler = handlers[handlerName] |
94a5ff8a | 134 | |
9129b769 | 135 | queue.process(this.getJobConcurrency(handlerName), handler) |
2b86fe72 | 136 | .catch(err => logger.error('Error in job queue processor %s.', handlerName, { err })) |
d7f83948 C |
137 | |
138 | queue.on('failed', (job, err) => { | |
139 | logger.error('Cannot execute job %d in queue %s.', job.id, handlerName, { payload: job.data, err }) | |
140 | }) | |
3df45638 | 141 | |
94831479 C |
142 | queue.on('error', err => { |
143 | logger.error('Error in job queue %s.', handlerName, { err }) | |
94a5ff8a | 144 | }) |
94831479 C |
145 | |
146 | this.queues[handlerName] = queue | |
94a5ff8a | 147 | } |
6b616860 C |
148 | |
149 | this.addRepeatableJobs() | |
94a5ff8a C |
150 | } |
151 | ||
14f2b3ad C |
152 | terminate () { |
153 | for (const queueName of Object.keys(this.queues)) { | |
154 | const queue = this.queues[queueName] | |
155 | queue.close() | |
156 | } | |
157 | } | |
158 | ||
a5cf76af C |
159 | createJob (obj: CreateJobArgument, options: CreateJobOptions = {}): void { |
160 | this.createJobWithPromise(obj, options) | |
e1c55031 | 161 | .catch(err => logger.error('Cannot create job.', { err, obj })) |
a1587156 C |
162 | } |
163 | ||
a5cf76af | 164 | createJobWithPromise (obj: CreateJobArgument, options: CreateJobOptions = {}) { |
94831479 C |
165 | const queue = this.queues[obj.type] |
166 | if (queue === undefined) { | |
167 | logger.error('Unknown queue %s: cannot create job.', obj.type) | |
a1587156 | 168 | return |
94831479 | 169 | } |
94a5ff8a | 170 | |
41fb13c3 | 171 | const jobArgs: JobOptions = { |
94831479 | 172 | backoff: { delay: 60 * 1000, type: 'exponential' }, |
2b86fe72 | 173 | attempts: JOB_ATTEMPTS[obj.type], |
a5cf76af | 174 | timeout: JOB_TTL[obj.type], |
77d7e851 | 175 | priority: options.priority, |
a5cf76af | 176 | delay: options.delay |
94831479 | 177 | } |
71e3dfda | 178 | |
94831479 | 179 | return queue.add(obj.payload, jobArgs) |
94a5ff8a C |
180 | } |
181 | ||
1061c73f | 182 | async listForApi (options: { |
402145b8 | 183 | state?: JobState |
a1587156 C |
184 | start: number |
185 | count: number | |
186 | asc?: boolean | |
1061c73f | 187 | jobType: JobType |
41fb13c3 | 188 | }): Promise<Job[]> { |
402145b8 C |
189 | const { state, start, count, asc, jobType } = options |
190 | ||
191 | const states = state ? [ state ] : jobStates | |
41fb13c3 | 192 | let results: Job[] = [] |
94a5ff8a | 193 | |
1061c73f C |
194 | const filteredJobTypes = this.filterJobTypes(jobType) |
195 | ||
1061c73f | 196 | for (const jobType of filteredJobTypes) { |
a1587156 | 197 | const queue = this.queues[jobType] |
94831479 C |
198 | if (queue === undefined) { |
199 | logger.error('Unknown queue %s to list jobs.', jobType) | |
200 | continue | |
201 | } | |
2c29ad4f | 202 | |
402145b8 | 203 | const jobs = await queue.getJobs(states, 0, start + count, asc) |
94831479 C |
204 | results = results.concat(jobs) |
205 | } | |
94a5ff8a | 206 | |
94831479 C |
207 | results.sort((j1: any, j2: any) => { |
208 | if (j1.timestamp < j2.timestamp) return -1 | |
209 | else if (j1.timestamp === j2.timestamp) return 0 | |
94a5ff8a | 210 | |
94831479 | 211 | return 1 |
94a5ff8a | 212 | }) |
94a5ff8a | 213 | |
94831479 | 214 | if (asc === false) results.reverse() |
94a5ff8a | 215 | |
94831479 | 216 | return results.slice(start, start + count) |
94a5ff8a C |
217 | } |
218 | ||
402145b8 C |
219 | async count (state: JobState, jobType?: JobType): Promise<number> { |
220 | const states = state ? [ state ] : jobStates | |
94831479 | 221 | let total = 0 |
3df45638 | 222 | |
1061c73f C |
223 | const filteredJobTypes = this.filterJobTypes(jobType) |
224 | ||
225 | for (const type of filteredJobTypes) { | |
a1587156 | 226 | const queue = this.queues[type] |
94831479 C |
227 | if (queue === undefined) { |
228 | logger.error('Unknown queue %s to count jobs.', type) | |
229 | continue | |
230 | } | |
3df45638 | 231 | |
94831479 | 232 | const counts = await queue.getJobCounts() |
3df45638 | 233 | |
040d6896 RK |
234 | for (const s of states) { |
235 | total += counts[s] | |
236 | } | |
94831479 | 237 | } |
3df45638 | 238 | |
94831479 | 239 | return total |
3df45638 C |
240 | } |
241 | ||
2f5c6b2f | 242 | async removeOldJobs () { |
94831479 C |
243 | for (const key of Object.keys(this.queues)) { |
244 | const queue = this.queues[key] | |
2f5c6b2f | 245 | await queue.clean(JOB_COMPLETED_LIFETIME, 'completed') |
94831479 | 246 | } |
2c29ad4f C |
247 | } |
248 | ||
6b616860 | 249 | private addRepeatableJobs () { |
51353d9a C |
250 | this.queues['videos-views-stats'].add({}, { |
251 | repeat: REPEAT_JOBS['videos-views-stats'] | |
a1587156 | 252 | }).catch(err => logger.error('Cannot add repeatable job.', { err })) |
74d249bc C |
253 | |
254 | if (CONFIG.FEDERATION.VIDEOS.CLEANUP_REMOTE_INTERACTIONS) { | |
255 | this.queues['activitypub-cleaner'].add({}, { | |
256 | repeat: REPEAT_JOBS['activitypub-cleaner'] | |
257 | }).catch(err => logger.error('Cannot add repeatable job.', { err })) | |
258 | } | |
6b616860 C |
259 | } |
260 | ||
1061c73f C |
261 | private filterJobTypes (jobType?: JobType) { |
262 | if (!jobType) return jobTypes | |
263 | ||
264 | return jobTypes.filter(t => t === jobType) | |
265 | } | |
266 | ||
9129b769 C |
267 | private getJobConcurrency (jobType: JobType) { |
268 | if (jobType === 'video-transcoding') return CONFIG.TRANSCODING.CONCURRENCY | |
269 | if (jobType === 'video-import') return CONFIG.IMPORT.VIDEOS.CONCURRENCY | |
270 | ||
271 | return JOB_CONCURRENCY[jobType] | |
272 | } | |
273 | ||
94a5ff8a C |
274 | static get Instance () { |
275 | return this.instance || (this.instance = new this()) | |
276 | } | |
277 | } | |
278 | ||
279 | // --------------------------------------------------------------------------- | |
280 | ||
281 | export { | |
1061c73f | 282 | jobTypes, |
94a5ff8a C |
283 | JobQueue |
284 | } |