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