]>
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' |
41fb13c3 | 36 | import { 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 | ||
94831479 C |
89 | const jobTypes: JobType[] = [ |
90 | 'activitypub-follow', | |
71e3dfda | 91 | 'activitypub-http-broadcast', |
71e3dfda | 92 | 'activitypub-http-fetcher', |
94831479 | 93 | 'activitypub-http-unicast', |
74d249bc | 94 | 'activitypub-cleaner', |
94831479 | 95 | 'email', |
a0327eed | 96 | 'video-transcoding', |
fbad87b0 | 97 | 'video-file-import', |
6b616860 | 98 | 'video-import', |
51353d9a | 99 | 'videos-views-stats', |
b764380a | 100 | 'activitypub-refresher', |
a5cf76af | 101 | 'video-redundancy', |
8795d6f2 | 102 | 'actor-keys', |
0305db28 | 103 | 'video-live-ending', |
c729caf6 | 104 | 'move-to-object-storage', |
f012319a | 105 | 'manage-video-torrent', |
92e66e04 | 106 | 'video-studio-edition' |
71e3dfda C |
107 | ] |
108 | ||
94a5ff8a C |
109 | class JobQueue { |
110 | ||
111 | private static instance: JobQueue | |
112 | ||
41fb13c3 | 113 | private queues: { [id in JobType]?: Queue } = {} |
94a5ff8a | 114 | private initialized = false |
2c29ad4f | 115 | private jobRedisPrefix: string |
94a5ff8a | 116 | |
a1587156 C |
117 | private constructor () { |
118 | } | |
94a5ff8a | 119 | |
e1ab52d7 | 120 | init (produceOnly = false) { |
94a5ff8a C |
121 | // Already initialized |
122 | if (this.initialized === true) return | |
123 | this.initialized = true | |
124 | ||
6dd9de95 | 125 | this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST |
ff4d2c73 C |
126 | |
127 | const queueOptions: Bull.QueueOptions = { | |
2c29ad4f | 128 | prefix: this.jobRedisPrefix, |
ff4d2c73 C |
129 | redis: { |
130 | password: CONFIG.REDIS.AUTH, | |
131 | db: CONFIG.REDIS.DB, | |
132 | host: CONFIG.REDIS.HOSTNAME, | |
133 | port: CONFIG.REDIS.PORT, | |
134 | path: CONFIG.REDIS.SOCKET | |
135 | }, | |
4a9e71c2 C |
136 | settings: { |
137 | maxStalledCount: 10 // transcoding could be long, so jobs can often be interrupted by restarts | |
138 | } | |
94831479 | 139 | } |
ecb4e35f | 140 | |
9129b769 | 141 | for (const handlerName of (Object.keys(handlers) as JobType[])) { |
94831479 | 142 | const queue = new Bull(handlerName, queueOptions) |
e1ab52d7 | 143 | |
144 | if (produceOnly) { | |
145 | queue.pause(true) | |
146 | .catch(err => logger.error('Cannot pause queue %s in produced only job queue', handlerName, { err })) | |
147 | } | |
148 | ||
94831479 | 149 | const handler = handlers[handlerName] |
94a5ff8a | 150 | |
9129b769 | 151 | queue.process(this.getJobConcurrency(handlerName), handler) |
2b86fe72 | 152 | .catch(err => logger.error('Error in job queue processor %s.', handlerName, { err })) |
d7f83948 C |
153 | |
154 | queue.on('failed', (job, err) => { | |
155 | logger.error('Cannot execute job %d in queue %s.', job.id, handlerName, { payload: job.data, err }) | |
156 | }) | |
3df45638 | 157 | |
94831479 C |
158 | queue.on('error', err => { |
159 | logger.error('Error in job queue %s.', handlerName, { err }) | |
94a5ff8a | 160 | }) |
94831479 C |
161 | |
162 | this.queues[handlerName] = queue | |
94a5ff8a | 163 | } |
6b616860 C |
164 | |
165 | this.addRepeatableJobs() | |
94a5ff8a C |
166 | } |
167 | ||
14f2b3ad C |
168 | terminate () { |
169 | for (const queueName of Object.keys(this.queues)) { | |
170 | const queue = this.queues[queueName] | |
171 | queue.close() | |
172 | } | |
173 | } | |
174 | ||
419b520c C |
175 | async pause () { |
176 | for (const handler of Object.keys(this.queues)) { | |
177 | await this.queues[handler].pause(true) | |
178 | } | |
179 | } | |
180 | ||
181 | async resume () { | |
182 | for (const handler of Object.keys(this.queues)) { | |
183 | await this.queues[handler].resume(true) | |
184 | } | |
185 | } | |
186 | ||
a5cf76af C |
187 | createJob (obj: CreateJobArgument, options: CreateJobOptions = {}): void { |
188 | this.createJobWithPromise(obj, options) | |
e1c55031 | 189 | .catch(err => logger.error('Cannot create job.', { err, obj })) |
a1587156 C |
190 | } |
191 | ||
a5cf76af | 192 | createJobWithPromise (obj: CreateJobArgument, options: CreateJobOptions = {}) { |
f012319a | 193 | const queue: Queue = this.queues[obj.type] |
94831479 C |
194 | if (queue === undefined) { |
195 | logger.error('Unknown queue %s: cannot create job.', obj.type) | |
a1587156 | 196 | return |
94831479 | 197 | } |
94a5ff8a | 198 | |
41fb13c3 | 199 | const jobArgs: JobOptions = { |
94831479 | 200 | backoff: { delay: 60 * 1000, type: 'exponential' }, |
2b86fe72 | 201 | attempts: JOB_ATTEMPTS[obj.type], |
a5cf76af | 202 | timeout: JOB_TTL[obj.type], |
77d7e851 | 203 | priority: options.priority, |
a5cf76af | 204 | delay: options.delay |
94831479 | 205 | } |
71e3dfda | 206 | |
94831479 | 207 | return queue.add(obj.payload, jobArgs) |
94a5ff8a C |
208 | } |
209 | ||
1061c73f | 210 | async listForApi (options: { |
402145b8 | 211 | state?: JobState |
a1587156 C |
212 | start: number |
213 | count: number | |
214 | asc?: boolean | |
1061c73f | 215 | jobType: JobType |
41fb13c3 | 216 | }): Promise<Job[]> { |
402145b8 C |
217 | const { state, start, count, asc, jobType } = options |
218 | ||
219 | const states = state ? [ state ] : jobStates | |
41fb13c3 | 220 | let results: Job[] = [] |
94a5ff8a | 221 | |
1061c73f C |
222 | const filteredJobTypes = this.filterJobTypes(jobType) |
223 | ||
1061c73f | 224 | for (const jobType of filteredJobTypes) { |
a1587156 | 225 | const queue = this.queues[jobType] |
94831479 C |
226 | if (queue === undefined) { |
227 | logger.error('Unknown queue %s to list jobs.', jobType) | |
228 | continue | |
229 | } | |
2c29ad4f | 230 | |
402145b8 | 231 | const jobs = await queue.getJobs(states, 0, start + count, asc) |
94831479 C |
232 | results = results.concat(jobs) |
233 | } | |
94a5ff8a | 234 | |
94831479 C |
235 | results.sort((j1: any, j2: any) => { |
236 | if (j1.timestamp < j2.timestamp) return -1 | |
237 | else if (j1.timestamp === j2.timestamp) return 0 | |
94a5ff8a | 238 | |
94831479 | 239 | return 1 |
94a5ff8a | 240 | }) |
94a5ff8a | 241 | |
94831479 | 242 | if (asc === false) results.reverse() |
94a5ff8a | 243 | |
94831479 | 244 | return results.slice(start, start + count) |
94a5ff8a C |
245 | } |
246 | ||
402145b8 C |
247 | async count (state: JobState, jobType?: JobType): Promise<number> { |
248 | const states = state ? [ state ] : jobStates | |
94831479 | 249 | let total = 0 |
3df45638 | 250 | |
1061c73f C |
251 | const filteredJobTypes = this.filterJobTypes(jobType) |
252 | ||
253 | for (const type of filteredJobTypes) { | |
a1587156 | 254 | const queue = this.queues[type] |
94831479 C |
255 | if (queue === undefined) { |
256 | logger.error('Unknown queue %s to count jobs.', type) | |
257 | continue | |
258 | } | |
3df45638 | 259 | |
94831479 | 260 | const counts = await queue.getJobCounts() |
3df45638 | 261 | |
040d6896 RK |
262 | for (const s of states) { |
263 | total += counts[s] | |
264 | } | |
94831479 | 265 | } |
3df45638 | 266 | |
94831479 | 267 | return total |
3df45638 C |
268 | } |
269 | ||
2f5c6b2f | 270 | async removeOldJobs () { |
94831479 C |
271 | for (const key of Object.keys(this.queues)) { |
272 | const queue = this.queues[key] | |
2f5c6b2f | 273 | await queue.clean(JOB_COMPLETED_LIFETIME, 'completed') |
94831479 | 274 | } |
2c29ad4f C |
275 | } |
276 | ||
6b616860 | 277 | private addRepeatableJobs () { |
51353d9a C |
278 | this.queues['videos-views-stats'].add({}, { |
279 | repeat: REPEAT_JOBS['videos-views-stats'] | |
a1587156 | 280 | }).catch(err => logger.error('Cannot add repeatable job.', { err })) |
74d249bc C |
281 | |
282 | if (CONFIG.FEDERATION.VIDEOS.CLEANUP_REMOTE_INTERACTIONS) { | |
283 | this.queues['activitypub-cleaner'].add({}, { | |
284 | repeat: REPEAT_JOBS['activitypub-cleaner'] | |
285 | }).catch(err => logger.error('Cannot add repeatable job.', { err })) | |
286 | } | |
6b616860 C |
287 | } |
288 | ||
1061c73f C |
289 | private filterJobTypes (jobType?: JobType) { |
290 | if (!jobType) return jobTypes | |
291 | ||
292 | return jobTypes.filter(t => t === jobType) | |
293 | } | |
294 | ||
9129b769 C |
295 | private getJobConcurrency (jobType: JobType) { |
296 | if (jobType === 'video-transcoding') return CONFIG.TRANSCODING.CONCURRENCY | |
297 | if (jobType === 'video-import') return CONFIG.IMPORT.VIDEOS.CONCURRENCY | |
298 | ||
299 | return JOB_CONCURRENCY[jobType] | |
300 | } | |
301 | ||
94a5ff8a C |
302 | static get Instance () { |
303 | return this.instance || (this.instance = new this()) | |
304 | } | |
305 | } | |
306 | ||
307 | // --------------------------------------------------------------------------- | |
308 | ||
309 | export { | |
1061c73f | 310 | jobTypes, |
94a5ff8a C |
311 | JobQueue |
312 | } |