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