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