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