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