]>
Commit | Line | Data |
---|---|---|
1 | import * as kue from 'kue' | |
2 | import { JobState, JobType } from '../../../shared/models' | |
3 | import { logger } from '../../helpers/logger' | |
4 | import { CONFIG, JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_REQUEST_TTL } from '../../initializers' | |
5 | import { Redis } from '../redis' | |
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' | |
9 | import { EmailPayload, processEmail } from './handlers/email' | |
10 | import { processVideoFile, VideoFilePayload } from './handlers/video-file' | |
11 | import { ActivitypubFollowPayload, processActivityPubFollow } from './handlers/activitypub-follow' | |
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 } | | |
17 | { type: 'activitypub-follow', payload: ActivitypubFollowPayload } | | |
18 | { type: 'video-file', payload: VideoFilePayload } | | |
19 | { type: 'email', payload: EmailPayload } | |
20 | ||
21 | const handlers: { [ id in JobType ]: (job: kue.Job) => Promise<any>} = { | |
22 | 'activitypub-http-broadcast': processActivityPubHttpBroadcast, | |
23 | 'activitypub-http-unicast': processActivityPubHttpUnicast, | |
24 | 'activitypub-http-fetcher': processActivityPubHttpFetcher, | |
25 | 'activitypub-follow': processActivityPubFollow, | |
26 | 'video-file': processVideoFile, | |
27 | 'email': processEmail | |
28 | } | |
29 | ||
30 | const jobsWithTLL: JobType[] = [ | |
31 | 'activitypub-http-broadcast', | |
32 | 'activitypub-http-unicast', | |
33 | 'activitypub-http-fetcher', | |
34 | 'activitypub-follow' | |
35 | ] | |
36 | ||
37 | class JobQueue { | |
38 | ||
39 | private static instance: JobQueue | |
40 | ||
41 | private jobQueue: kue.Queue | |
42 | private initialized = false | |
43 | private jobRedisPrefix: string | |
44 | ||
45 | private constructor () {} | |
46 | ||
47 | async init () { | |
48 | // Already initialized | |
49 | if (this.initialized === true) return | |
50 | this.initialized = true | |
51 | ||
52 | this.jobRedisPrefix = 'q-' + CONFIG.WEBSERVER.HOST | |
53 | ||
54 | this.jobQueue = kue.createQueue({ | |
55 | prefix: this.jobRedisPrefix, | |
56 | redis: { | |
57 | host: CONFIG.REDIS.HOSTNAME, | |
58 | port: CONFIG.REDIS.PORT, | |
59 | auth: CONFIG.REDIS.AUTH | |
60 | } | |
61 | }) | |
62 | ||
63 | this.jobQueue.setMaxListeners(20) | |
64 | ||
65 | this.jobQueue.on('error', err => { | |
66 | logger.error('Error in job queue.', { err }) | |
67 | process.exit(-1) | |
68 | }) | |
69 | this.jobQueue.watchStuckJobs(5000) | |
70 | ||
71 | await this.reactiveStuckJobs() | |
72 | ||
73 | for (const handlerName of Object.keys(handlers)) { | |
74 | this.jobQueue.process(handlerName, JOB_CONCURRENCY[handlerName], async (job, done) => { | |
75 | try { | |
76 | const res = await handlers[ handlerName ](job) | |
77 | return done(null, res) | |
78 | } catch (err) { | |
79 | return done(err) | |
80 | } | |
81 | }) | |
82 | } | |
83 | } | |
84 | ||
85 | createJob (obj: CreateJobArgument, priority = 'normal') { | |
86 | return new Promise((res, rej) => { | |
87 | let job = this.jobQueue | |
88 | .create(obj.type, obj.payload) | |
89 | .priority(priority) | |
90 | .attempts(JOB_ATTEMPTS[obj.type]) | |
91 | .backoff({ delay: 60 * 1000, type: 'exponential' }) | |
92 | ||
93 | if (jobsWithTLL.indexOf(obj.type) !== -1) { | |
94 | job = job.ttl(JOB_REQUEST_TTL) | |
95 | } | |
96 | ||
97 | return job.save(err => { | |
98 | if (err) return rej(err) | |
99 | ||
100 | return res() | |
101 | }) | |
102 | }) | |
103 | } | |
104 | ||
105 | async listForApi (state: JobState, start: number, count: number, sort: 'ASC' | 'DESC'): Promise<kue.Job[]> { | |
106 | const jobStrings = await Redis.Instance.listJobs(this.jobRedisPrefix, state, 'alpha', sort, start, count) | |
107 | ||
108 | const jobPromises = jobStrings | |
109 | .map(s => s.split('|')) | |
110 | .map(([ , jobId ]) => this.getJob(parseInt(jobId, 10))) | |
111 | ||
112 | return Promise.all(jobPromises) | |
113 | } | |
114 | ||
115 | count (state: JobState) { | |
116 | return new Promise<number>((res, rej) => { | |
117 | this.jobQueue[state + 'Count']((err, total) => { | |
118 | if (err) return rej(err) | |
119 | ||
120 | return res(total) | |
121 | }) | |
122 | }) | |
123 | } | |
124 | ||
125 | removeOldJobs () { | |
126 | const now = new Date().getTime() | |
127 | kue.Job.rangeByState('complete', 0, -1, 'asc', (err, jobs) => { | |
128 | if (err) { | |
129 | logger.error('Cannot get jobs when removing old jobs.', { err }) | |
130 | return | |
131 | } | |
132 | ||
133 | for (const job of jobs) { | |
134 | if (now - job.created_at > JOB_COMPLETED_LIFETIME) { | |
135 | job.remove() | |
136 | } | |
137 | } | |
138 | }) | |
139 | } | |
140 | ||
141 | private reactiveStuckJobs () { | |
142 | const promises: Promise<any>[] = [] | |
143 | ||
144 | this.jobQueue.active((err, ids) => { | |
145 | if (err) throw err | |
146 | ||
147 | for (const id of ids) { | |
148 | kue.Job.get(id, (err, job) => { | |
149 | if (err) throw err | |
150 | ||
151 | const p = new Promise((res, rej) => { | |
152 | job.inactive(err => { | |
153 | if (err) return rej(err) | |
154 | return res() | |
155 | }) | |
156 | }) | |
157 | ||
158 | promises.push(p) | |
159 | }) | |
160 | } | |
161 | }) | |
162 | ||
163 | return Promise.all(promises) | |
164 | } | |
165 | ||
166 | private getJob (id: number) { | |
167 | return new Promise<kue.Job>((res, rej) => { | |
168 | kue.Job.get(id, (err, job) => { | |
169 | if (err) return rej(err) | |
170 | ||
171 | return res(job) | |
172 | }) | |
173 | }) | |
174 | } | |
175 | ||
176 | static get Instance () { | |
177 | return this.instance || (this.instance = new this()) | |
178 | } | |
179 | } | |
180 | ||
181 | // --------------------------------------------------------------------------- | |
182 | ||
183 | export { | |
184 | JobQueue | |
185 | } |