]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/job-queue.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / job-queue.ts
index a1c623b2575d0997d4987f003f21278c73c05df6..14acace7da80a35f0d10338a6f73aa4c9c961e47 100644 (file)
@@ -28,7 +28,7 @@ type CreateJobArgument =
   { type: 'videos-views', payload: {} } |
   { type: 'video-redundancy', payload: VideoRedundancyPayload }
 
-const handlers: { [ id in JobType ]: (job: Bull.Job) => Promise<any>} = {
+const handlers: { [id in JobType]: (job: Bull.Job) => Promise<any> } = {
   'activitypub-http-broadcast': processActivityPubHttpBroadcast,
   'activitypub-http-unicast': processActivityPubHttpUnicast,
   'activitypub-http-fetcher': processActivityPubHttpFetcher,
@@ -60,13 +60,14 @@ class JobQueue {
 
   private static instance: JobQueue
 
-  private queues: { [ id in JobType ]?: Bull.Queue } = {}
+  private queues: { [id in JobType]?: Bull.Queue } = {}
   private initialized = false
   private jobRedisPrefix: string
 
-  private constructor () {}
+  private constructor () {
+  }
 
-  async init () {
+  init () {
     // Already initialized
     if (this.initialized === true) return
     this.initialized = true
@@ -108,11 +109,16 @@ class JobQueue {
     }
   }
 
-  createJob (obj: CreateJobArgument) {
+  createJob (obj: CreateJobArgument): void {
+    this.createJobWithPromise(obj)
+         .catch(err => logger.error('Cannot create job.', { err, obj }))
+  }
+
+  createJobWithPromise (obj: CreateJobArgument) {
     const queue = this.queues[obj.type]
     if (queue === undefined) {
       logger.error('Unknown queue %s: cannot create job.', obj.type)
-      throw Error('Unknown queue, cannot create job')
+      return
     }
 
     const jobArgs: Bull.JobOptions = {
@@ -125,10 +131,10 @@ class JobQueue {
   }
 
   async listForApi (options: {
-    state: JobState,
-    start: number,
-    count: number,
-    asc?: boolean,
+    state: JobState
+    start: number
+    count: number
+    asc?: boolean
     jobType: JobType
   }): Promise<Bull.Job[]> {
     const { state, start, count, asc, jobType } = options
@@ -136,9 +142,8 @@ class JobQueue {
 
     const filteredJobTypes = this.filterJobTypes(jobType)
 
-    // TODO: optimize
     for (const jobType of filteredJobTypes) {
-      const queue = this.queues[ jobType ]
+      const queue = this.queues[jobType]
       if (queue === undefined) {
         logger.error('Unknown queue %s to list jobs.', jobType)
         continue
@@ -166,7 +171,7 @@ class JobQueue {
     const filteredJobTypes = this.filterJobTypes(jobType)
 
     for (const type of filteredJobTypes) {
-      const queue = this.queues[ type ]
+      const queue = this.queues[type]
       if (queue === undefined) {
         logger.error('Unknown queue %s to count jobs.', type)
         continue
@@ -174,7 +179,7 @@ class JobQueue {
 
       const counts = await queue.getJobCounts()
 
-      total += counts[ state ]
+      total += counts[state]
     }
 
     return total
@@ -190,7 +195,7 @@ class JobQueue {
   private addRepeatableJobs () {
     this.queues['videos-views'].add({}, {
       repeat: REPEAT_JOBS['videos-views']
-    })
+    }).catch(err => logger.error('Cannot add repeatable job.', { err }))
   }
 
   private filterJobTypes (jobType?: JobType) {