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