]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/job-queue.ts
Add timeout and TTL to request jobs
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / job-queue.ts
CommitLineData
94a5ff8a 1import * as kue from 'kue'
628d28e8 2import { JobState, JobType } from '../../../shared/models'
94a5ff8a 3import { logger } from '../../helpers/logger'
71e3dfda 4import { CONFIG, JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_REQUEST_TTL } from '../../initializers'
2c29ad4f 5import { Redis } from '../redis'
94a5ff8a
C
6import { ActivitypubHttpBroadcastPayload, processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast'
7import { ActivitypubHttpFetcherPayload, processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher'
8import { ActivitypubHttpUnicastPayload, processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast'
ecb4e35f 9import { EmailPayload, processEmail } from './handlers/email'
94a5ff8a 10import { processVideoFile, VideoFilePayload } from './handlers/video-file'
5350fd8e 11import { ActivitypubFollowPayload, processActivityPubFollow } from './handlers/activitypub-follow'
94a5ff8a
C
12
13type 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 } |
ecb4e35f
C
18 { type: 'video-file', payload: VideoFilePayload } |
19 { type: 'email', payload: EmailPayload }
94a5ff8a
C
20
21const handlers: { [ id in JobType ]: (job: kue.Job) => Promise<any>} = {
22 'activitypub-http-broadcast': processActivityPubHttpBroadcast,
23 'activitypub-http-unicast': processActivityPubHttpUnicast,
24 'activitypub-http-fetcher': processActivityPubHttpFetcher,
5350fd8e 25 'activitypub-follow': processActivityPubFollow,
ecb4e35f
C
26 'video-file': processVideoFile,
27 'email': processEmail
94a5ff8a
C
28}
29
71e3dfda
C
30const jobsWithTLL: JobType[] = [
31 'activitypub-http-broadcast',
32 'activitypub-http-unicast',
33 'activitypub-http-fetcher',
34 'activitypub-follow'
35]
36
94a5ff8a
C
37class JobQueue {
38
39 private static instance: JobQueue
40
41 private jobQueue: kue.Queue
42 private initialized = false
2c29ad4f 43 private jobRedisPrefix: string
94a5ff8a
C
44
45 private constructor () {}
46
3df45638 47 async init () {
94a5ff8a
C
48 // Already initialized
49 if (this.initialized === true) return
50 this.initialized = true
51
2c29ad4f
C
52 this.jobRedisPrefix = 'q-' + CONFIG.WEBSERVER.HOST
53
94a5ff8a 54 this.jobQueue = kue.createQueue({
2c29ad4f 55 prefix: this.jobRedisPrefix,
94a5ff8a
C
56 redis: {
57 host: CONFIG.REDIS.HOSTNAME,
58 port: CONFIG.REDIS.PORT,
59 auth: CONFIG.REDIS.AUTH
60 }
61 })
62
5350fd8e 63 this.jobQueue.setMaxListeners(20)
ecb4e35f 64
94a5ff8a 65 this.jobQueue.on('error', err => {
d5b7d911 66 logger.error('Error in job queue.', { err })
94a5ff8a
C
67 process.exit(-1)
68 })
69 this.jobQueue.watchStuckJobs(5000)
70
3df45638
C
71 await this.reactiveStuckJobs()
72
94a5ff8a
C
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) => {
71e3dfda 87 let job = this.jobQueue
94a5ff8a
C
88 .create(obj.type, obj.payload)
89 .priority(priority)
90 .attempts(JOB_ATTEMPTS[obj.type])
802dbc32 91 .backoff({ delay: 60 * 1000, type: 'exponential' })
94a5ff8a 92
71e3dfda
C
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 })
94a5ff8a
C
102 })
103 }
104
628d28e8 105 async listForApi (state: JobState, start: number, count: number, sort: 'ASC' | 'DESC'): Promise<kue.Job[]> {
2c29ad4f 106 const jobStrings = await Redis.Instance.listJobs(this.jobRedisPrefix, state, 'alpha', sort, start, count)
94a5ff8a 107
2c29ad4f
C
108 const jobPromises = jobStrings
109 .map(s => s.split('|'))
110 .map(([ , jobId ]) => this.getJob(parseInt(jobId, 10)))
111
112 return Promise.all(jobPromises)
94a5ff8a
C
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) {
d5b7d911 129 logger.error('Cannot get jobs when removing old jobs.', { err })
94a5ff8a
C
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
3df45638
C
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
2c29ad4f 166 private getJob (id: number) {
628d28e8 167 return new Promise<kue.Job>((res, rej) => {
2c29ad4f
C
168 kue.Job.get(id, (err, job) => {
169 if (err) return rej(err)
170
171 return res(job)
172 })
173 })
174 }
175
94a5ff8a
C
176 static get Instance () {
177 return this.instance || (this.instance = new this())
178 }
179}
180
181// ---------------------------------------------------------------------------
182
183export {
184 JobQueue
185}