]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/job-queue.ts
Don't inject untrusted input
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / job-queue.ts
index e54d12acd2f82274fcfda7735fa4133c4610d78b..6bc59732f2d7260e96d32168568884ced0d4572e 100644 (file)
@@ -63,6 +63,7 @@ import { processVideoLiveEnding } from './handlers/video-live-ending'
 import { processVideoStudioEdition } from './handlers/video-studio-edition'
 import { processVideoTranscoding } from './handlers/video-transcoding'
 import { processVideosViewsStats } from './handlers/video-views-stats'
+import { Redis } from '../redis'
 
 export type CreateJobArgument =
   { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } |
@@ -168,7 +169,7 @@ class JobQueue {
   private constructor () {
   }
 
-  init (produceOnly = false) {
+  init () {
     // Already initialized
     if (this.initialized === true) return
     this.initialized = true
@@ -176,14 +177,14 @@ class JobQueue {
     this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST
 
     for (const handlerName of (Object.keys(handlers) as JobType[])) {
-      this.buildWorker(handlerName, produceOnly)
+      this.buildWorker(handlerName)
       this.buildQueue(handlerName)
-      this.buildQueueScheduler(handlerName, produceOnly)
-      this.buildQueueEvent(handlerName, produceOnly)
+      this.buildQueueScheduler(handlerName)
+      this.buildQueueEvent(handlerName)
     }
 
     this.flowProducer = new FlowProducer({
-      connection: this.getRedisConnection(),
+      connection: Redis.getRedisClientOptions('FlowProducer'),
       prefix: this.jobRedisPrefix
     })
     this.flowProducer.on('error', err => { logger.error('Error in flow producer', { err }) })
@@ -191,12 +192,12 @@ class JobQueue {
     this.addRepeatableJobs()
   }
 
-  private buildWorker (handlerName: JobType, produceOnly: boolean) {
+  private buildWorker (handlerName: JobType) {
     const workerOptions: WorkerOptions = {
-      autorun: !produceOnly,
+      autorun: false,
       concurrency: this.getJobConcurrency(handlerName),
       prefix: this.jobRedisPrefix,
-      connection: this.getRedisConnection()
+      connection: Redis.getRedisClientOptions('Worker')
     }
 
     const handler = function (job: Job) {
@@ -236,7 +237,7 @@ class JobQueue {
 
   private buildQueue (handlerName: JobType) {
     const queueOptions: QueueOptions = {
-      connection: this.getRedisConnection(),
+      connection: Redis.getRedisClientOptions('Queue'),
       prefix: this.jobRedisPrefix
     }
 
@@ -246,10 +247,10 @@ class JobQueue {
     this.queues[handlerName] = queue
   }
 
-  private buildQueueScheduler (handlerName: JobType, produceOnly: boolean) {
+  private buildQueueScheduler (handlerName: JobType) {
     const queueSchedulerOptions: QueueSchedulerOptions = {
-      autorun: !produceOnly,
-      connection: this.getRedisConnection(),
+      autorun: false,
+      connection: Redis.getRedisClientOptions('QueueScheduler'),
       prefix: this.jobRedisPrefix,
       maxStalledCount: 10
     }
@@ -260,10 +261,10 @@ class JobQueue {
     this.queueSchedulers[handlerName] = queueScheduler
   }
 
-  private buildQueueEvent (handlerName: JobType, produceOnly: boolean) {
+  private buildQueueEvent (handlerName: JobType) {
     const queueEventsOptions: QueueEventsOptions = {
-      autorun: !produceOnly,
-      connection: this.getRedisConnection(),
+      autorun: false,
+      connection: Redis.getRedisClientOptions('QueueEvent'),
       prefix: this.jobRedisPrefix
     }
 
@@ -273,16 +274,6 @@ class JobQueue {
     this.queueEvents[handlerName] = queueEvents
   }
 
-  private getRedisConnection () {
-    return {
-      password: CONFIG.REDIS.AUTH,
-      db: CONFIG.REDIS.DB,
-      host: CONFIG.REDIS.HOSTNAME,
-      port: CONFIG.REDIS.PORT,
-      path: CONFIG.REDIS.SOCKET
-    }
-  }
-
   // ---------------------------------------------------------------------------
 
   async terminate () {
@@ -304,6 +295,23 @@ class JobQueue {
     return Promise.all(promises)
   }
 
+  start () {
+    const promises = Object.keys(this.workers)
+      .map(handlerName => {
+        const worker: Worker = this.workers[handlerName]
+        const queueScheduler: QueueScheduler = this.queueSchedulers[handlerName]
+        const queueEvent: QueueEvents = this.queueEvents[handlerName]
+
+        return Promise.all([
+          worker.run(),
+          queueScheduler.run(),
+          queueEvent.run()
+        ])
+      })
+
+    return Promise.all(promises)
+  }
+
   async pause () {
     for (const handlerName of Object.keys(this.workers)) {
       const worker: Worker = this.workers[handlerName]