]>
Commit | Line | Data |
---|---|---|
94831479 | 1 | import * as Bull from 'bull' |
628d28e8 | 2 | import { JobState, JobType } from '../../../shared/models' |
94a5ff8a | 3 | import { logger } from '../../helpers/logger' |
19f7b248 | 4 | import { Redis } from '../redis' |
71e3dfda | 5 | import { CONFIG, JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_REQUEST_TTL } from '../../initializers' |
94a5ff8a C |
6 | import { ActivitypubHttpBroadcastPayload, processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast' |
7 | import { ActivitypubHttpFetcherPayload, processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher' | |
8 | import { ActivitypubHttpUnicastPayload, processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast' | |
ecb4e35f | 9 | import { EmailPayload, processEmail } from './handlers/email' |
94831479 | 10 | import { processVideoFile, processVideoFileImport, VideoFileImportPayload, VideoFilePayload } from './handlers/video-file' |
5350fd8e | 11 | import { ActivitypubFollowPayload, processActivityPubFollow } from './handlers/activitypub-follow' |
94a5ff8a C |
12 | |
13 | type CreateJobArgument = | |
14 | { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } | | |
15 | { type: 'activitypub-http-unicast', payload: ActivitypubHttpUnicastPayload } | | |
16 | { type: 'activitypub-http-fetcher', payload: ActivitypubHttpFetcherPayload } | | |
5350fd8e | 17 | { type: 'activitypub-follow', payload: ActivitypubFollowPayload } | |
28be8916 | 18 | { type: 'video-file-import', payload: VideoFileImportPayload } | |
ecb4e35f C |
19 | { type: 'video-file', payload: VideoFilePayload } | |
20 | { type: 'email', payload: EmailPayload } | |
94a5ff8a | 21 | |
94831479 | 22 | const handlers: { [ id in JobType ]: (job: Bull.Job) => Promise<any>} = { |
94a5ff8a C |
23 | 'activitypub-http-broadcast': processActivityPubHttpBroadcast, |
24 | 'activitypub-http-unicast': processActivityPubHttpUnicast, | |
25 | 'activitypub-http-fetcher': processActivityPubHttpFetcher, | |
5350fd8e | 26 | 'activitypub-follow': processActivityPubFollow, |
28be8916 | 27 | 'video-file-import': processVideoFileImport, |
ecb4e35f C |
28 | 'video-file': processVideoFile, |
29 | 'email': processEmail | |
94a5ff8a C |
30 | } |
31 | ||
94831479 C |
32 | const jobsWithRequestTimeout: { [ id in JobType ]?: boolean } = { |
33 | 'activitypub-http-broadcast': true, | |
34 | 'activitypub-http-unicast': true, | |
35 | 'activitypub-http-fetcher': true, | |
36 | 'activitypub-follow': true | |
37 | } | |
38 | ||
39 | const jobTypes: JobType[] = [ | |
40 | 'activitypub-follow', | |
71e3dfda | 41 | 'activitypub-http-broadcast', |
71e3dfda | 42 | 'activitypub-http-fetcher', |
94831479 C |
43 | 'activitypub-http-unicast', |
44 | 'email', | |
45 | 'video-file', | |
46 | 'video-file-import' | |
71e3dfda C |
47 | ] |
48 | ||
94a5ff8a C |
49 | class JobQueue { |
50 | ||
51 | private static instance: JobQueue | |
52 | ||
94831479 | 53 | private queues: { [ id in JobType ]?: Bull.Queue } = {} |
94a5ff8a | 54 | private initialized = false |
2c29ad4f | 55 | private jobRedisPrefix: string |
94a5ff8a C |
56 | |
57 | private constructor () {} | |
58 | ||
3df45638 | 59 | async init () { |
94a5ff8a C |
60 | // Already initialized |
61 | if (this.initialized === true) return | |
62 | this.initialized = true | |
63 | ||
94831479 C |
64 | this.jobRedisPrefix = 'bull-' + CONFIG.WEBSERVER.HOST |
65 | const queueOptions = { | |
2c29ad4f | 66 | prefix: this.jobRedisPrefix, |
4a9e71c2 C |
67 | redis: Redis.getRedisClient(), |
68 | settings: { | |
69 | maxStalledCount: 10 // transcoding could be long, so jobs can often be interrupted by restarts | |
70 | } | |
94831479 | 71 | } |
ecb4e35f | 72 | |
94831479 C |
73 | for (const handlerName of Object.keys(handlers)) { |
74 | const queue = new Bull(handlerName, queueOptions) | |
75 | const handler = handlers[handlerName] | |
94a5ff8a | 76 | |
94831479 C |
77 | queue.process(JOB_CONCURRENCY[handlerName], handler) |
78 | .catch(err => logger.error('Cannot execute job queue %s.', handlerName, { err })) | |
3df45638 | 79 | |
94831479 C |
80 | queue.on('error', err => { |
81 | logger.error('Error in job queue %s.', handlerName, { err }) | |
82 | process.exit(-1) | |
94a5ff8a | 83 | }) |
94831479 C |
84 | |
85 | this.queues[handlerName] = queue | |
94a5ff8a C |
86 | } |
87 | } | |
88 | ||
14f2b3ad C |
89 | terminate () { |
90 | for (const queueName of Object.keys(this.queues)) { | |
91 | const queue = this.queues[queueName] | |
92 | queue.close() | |
93 | } | |
94 | } | |
95 | ||
94831479 C |
96 | createJob (obj: CreateJobArgument) { |
97 | const queue = this.queues[obj.type] | |
98 | if (queue === undefined) { | |
99 | logger.error('Unknown queue %s: cannot create job.', obj.type) | |
c1e791ba | 100 | throw Error('Unknown queue, cannot create job') |
94831479 | 101 | } |
94a5ff8a | 102 | |
94831479 C |
103 | const jobArgs: Bull.JobOptions = { |
104 | backoff: { delay: 60 * 1000, type: 'exponential' }, | |
105 | attempts: JOB_ATTEMPTS[obj.type] | |
106 | } | |
71e3dfda | 107 | |
94831479 C |
108 | if (jobsWithRequestTimeout[obj.type] === true) { |
109 | jobArgs.timeout = JOB_REQUEST_TTL | |
110 | } | |
71e3dfda | 111 | |
94831479 | 112 | return queue.add(obj.payload, jobArgs) |
94a5ff8a C |
113 | } |
114 | ||
94831479 C |
115 | async listForApi (state: JobState, start: number, count: number, asc?: boolean): Promise<Bull.Job[]> { |
116 | let results: Bull.Job[] = [] | |
94a5ff8a | 117 | |
94831479 C |
118 | // TODO: optimize |
119 | for (const jobType of jobTypes) { | |
120 | const queue = this.queues[ jobType ] | |
121 | if (queue === undefined) { | |
122 | logger.error('Unknown queue %s to list jobs.', jobType) | |
123 | continue | |
124 | } | |
2c29ad4f | 125 | |
94831479 C |
126 | // FIXME: Bull queue typings does not have getJobs method |
127 | const jobs = await (queue as any).getJobs(state, 0, start + count, asc) | |
128 | results = results.concat(jobs) | |
129 | } | |
94a5ff8a | 130 | |
94831479 C |
131 | results.sort((j1: any, j2: any) => { |
132 | if (j1.timestamp < j2.timestamp) return -1 | |
133 | else if (j1.timestamp === j2.timestamp) return 0 | |
94a5ff8a | 134 | |
94831479 | 135 | return 1 |
94a5ff8a | 136 | }) |
94a5ff8a | 137 | |
94831479 | 138 | if (asc === false) results.reverse() |
94a5ff8a | 139 | |
94831479 | 140 | return results.slice(start, start + count) |
94a5ff8a C |
141 | } |
142 | ||
94831479 C |
143 | async count (state: JobState): Promise<number> { |
144 | let total = 0 | |
3df45638 | 145 | |
94831479 C |
146 | for (const type of jobTypes) { |
147 | const queue = this.queues[ type ] | |
148 | if (queue === undefined) { | |
149 | logger.error('Unknown queue %s to count jobs.', type) | |
150 | continue | |
151 | } | |
3df45638 | 152 | |
94831479 | 153 | const counts = await queue.getJobCounts() |
3df45638 | 154 | |
94831479 C |
155 | total += counts[ state ] |
156 | } | |
3df45638 | 157 | |
94831479 | 158 | return total |
3df45638 C |
159 | } |
160 | ||
94831479 C |
161 | removeOldJobs () { |
162 | for (const key of Object.keys(this.queues)) { | |
163 | const queue = this.queues[key] | |
164 | queue.clean(JOB_COMPLETED_LIFETIME, 'completed') | |
165 | } | |
2c29ad4f C |
166 | } |
167 | ||
94a5ff8a C |
168 | static get Instance () { |
169 | return this.instance || (this.instance = new this()) | |
170 | } | |
171 | } | |
172 | ||
173 | // --------------------------------------------------------------------------- | |
174 | ||
175 | export { | |
176 | JobQueue | |
177 | } |