]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/job-queue.ts
feature/ability to disable video history by default (#5728)
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / job-queue.ts
index 6bc59732f2d7260e96d32168568884ced0d4572e..cc6be0bd8d1fb3b48f9765d326d31f727e294ea7 100644 (file)
@@ -7,11 +7,10 @@ import {
   QueueEvents,
   QueueEventsOptions,
   QueueOptions,
-  QueueScheduler,
-  QueueSchedulerOptions,
   Worker,
   WorkerOptions
 } from 'bullmq'
+import { parseDurationToMs } from '@server/helpers/core-utils'
 import { jobStates } from '@server/helpers/custom-validators/jobs'
 import { CONFIG } from '@server/initializers/config'
 import { processVideoRedundancy } from '@server/lib/job-queue/handlers/video-redundancy'
@@ -41,8 +40,9 @@ import {
   VideoTranscodingPayload
 } from '../../../shared/models'
 import { logger } from '../../helpers/logger'
-import { JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_JOBS, WEBSERVER } from '../../initializers/constants'
+import { JOB_ATTEMPTS, JOB_CONCURRENCY, JOB_REMOVAL_OPTIONS, JOB_TTL, REPEAT_JOBS, WEBSERVER } from '../../initializers/constants'
 import { Hooks } from '../plugins/hooks'
+import { Redis } from '../redis'
 import { processActivityPubCleaner } from './handlers/activitypub-cleaner'
 import { processActivityPubFollow } from './handlers/activitypub-follow'
 import { processActivityPubHttpSequentialBroadcast, processActivityPubParallelHttpBroadcast } from './handlers/activitypub-http-broadcast'
@@ -63,7 +63,6 @@ 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 } |
@@ -158,7 +157,6 @@ class JobQueue {
 
   private workers: { [id in JobType]?: Worker } = {}
   private queues: { [id in JobType]?: Queue } = {}
-  private queueSchedulers: { [id in JobType]?: QueueScheduler } = {}
   private queueEvents: { [id in JobType]?: QueueEvents } = {}
 
   private flowProducer: FlowProducer
@@ -176,10 +174,9 @@ class JobQueue {
 
     this.jobRedisPrefix = 'bull-' + WEBSERVER.HOST
 
-    for (const handlerName of (Object.keys(handlers) as JobType[])) {
+    for (const handlerName of Object.keys(handlers)) {
       this.buildWorker(handlerName)
       this.buildQueue(handlerName)
-      this.buildQueueScheduler(handlerName)
       this.buildQueueEvent(handlerName)
     }
 
@@ -197,7 +194,8 @@ class JobQueue {
       autorun: false,
       concurrency: this.getJobConcurrency(handlerName),
       prefix: this.jobRedisPrefix,
-      connection: Redis.getRedisClientOptions('Worker')
+      connection: Redis.getRedisClientOptions('Worker'),
+      maxStalledCount: 10
     }
 
     const handler = function (job: Job) {
@@ -247,20 +245,6 @@ class JobQueue {
     this.queues[handlerName] = queue
   }
 
-  private buildQueueScheduler (handlerName: JobType) {
-    const queueSchedulerOptions: QueueSchedulerOptions = {
-      autorun: false,
-      connection: Redis.getRedisClientOptions('QueueScheduler'),
-      prefix: this.jobRedisPrefix,
-      maxStalledCount: 10
-    }
-
-    const queueScheduler = new QueueScheduler(handlerName, queueSchedulerOptions)
-    queueScheduler.on('error', err => { logger.error('Error in job queue scheduler %s.', handlerName, { err }) })
-
-    this.queueSchedulers[handlerName] = queueScheduler
-  }
-
   private buildQueueEvent (handlerName: JobType) {
     const queueEventsOptions: QueueEventsOptions = {
       autorun: false,
@@ -281,13 +265,11 @@ class JobQueue {
       .map(handlerName => {
         const worker: Worker = this.workers[handlerName]
         const queue: Queue = this.queues[handlerName]
-        const queueScheduler: QueueScheduler = this.queueSchedulers[handlerName]
         const queueEvent: QueueEvents = this.queueEvents[handlerName]
 
         return Promise.all([
           worker.close(false),
           queue.close(),
-          queueScheduler.close(),
           queueEvent.close()
         ])
       })
@@ -299,12 +281,10 @@ class JobQueue {
     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()
         ])
       })
@@ -373,7 +353,7 @@ class JobQueue {
     })
   }
 
-  private buildJobFlowOption (job: CreateJobArgument & CreateJobOptions) {
+  private buildJobFlowOption (job: CreateJobArgument & CreateJobOptions): FlowJob {
     return {
       name: 'job',
       data: job.payload,
@@ -387,7 +367,9 @@ class JobQueue {
       backoff: { delay: 60 * 1000, type: 'exponential' },
       attempts: JOB_ATTEMPTS[type],
       priority: options.priority,
-      delay: options.delay
+      delay: options.delay,
+
+      ...this.buildJobRemovalOptions(type)
     }
   }
 
@@ -482,18 +464,23 @@ class JobQueue {
   async removeOldJobs () {
     for (const key of Object.keys(this.queues)) {
       const queue: Queue = this.queues[key]
-      await queue.clean(JOB_COMPLETED_LIFETIME, 100, 'completed')
+      await queue.clean(parseDurationToMs('7 days'), 1000, 'completed')
+      await queue.clean(parseDurationToMs('7 days'), 1000, 'failed')
     }
   }
 
   private addRepeatableJobs () {
     this.queues['videos-views-stats'].add('job', {}, {
-      repeat: REPEAT_JOBS['videos-views-stats']
+      repeat: REPEAT_JOBS['videos-views-stats'],
+
+      ...this.buildJobRemovalOptions('videos-views-stats')
     }).catch(err => logger.error('Cannot add repeatable job.', { err }))
 
     if (CONFIG.FEDERATION.VIDEOS.CLEANUP_REMOTE_INTERACTIONS) {
       this.queues['activitypub-cleaner'].add('job', {}, {
-        repeat: REPEAT_JOBS['activitypub-cleaner']
+        repeat: REPEAT_JOBS['activitypub-cleaner'],
+
+        ...this.buildJobRemovalOptions('activitypub-cleaner')
       }).catch(err => logger.error('Cannot add repeatable job.', { err }))
     }
   }
@@ -505,6 +492,23 @@ class JobQueue {
     return JOB_CONCURRENCY[jobType]
   }
 
+  private buildJobRemovalOptions (queueName: string) {
+    return {
+      removeOnComplete: {
+        // Wants seconds
+        age: (JOB_REMOVAL_OPTIONS.SUCCESS[queueName] || JOB_REMOVAL_OPTIONS.SUCCESS.DEFAULT) / 1000,
+
+        count: JOB_REMOVAL_OPTIONS.COUNT
+      },
+      removeOnFail: {
+        // Wants seconds
+        age: (JOB_REMOVAL_OPTIONS.FAILURE[queueName] || JOB_REMOVAL_OPTIONS.FAILURE.DEFAULT) / 1000,
+
+        count: JOB_REMOVAL_OPTIONS.COUNT / 1000
+      }
+    }
+  }
+
   static get Instance () {
     return this.instance || (this.instance = new this())
   }