Browse Source

feat(job-queue-plugin): Set default concurrency

Michael Bromley 4 years ago
parent
commit
0e971e7348

+ 7 - 2
packages/job-queue-plugin/src/bullmq/bullmq-job-queue-strategy.ts

@@ -10,13 +10,14 @@ import {
     Logger,
     PaginatedList,
 } from '@vendure/core';
-import Bull, { Processor, Queue, QueueScheduler, Worker } from 'bullmq';
+import Bull, { Processor, Queue, QueueScheduler, Worker, WorkerOptions } from 'bullmq';
 
 import { ALL_JOB_TYPES, BULLMQ_PLUGIN_OPTIONS, loggerCtx } from './constants';
 import { RedisHealthIndicator } from './redis-health-indicator';
 import { BullMQPluginOptions } from './types';
 
 const QUEUE_NAME = 'vendure-job-queue';
+const DEFAULT_CONCURRENCY = 3;
 
 /**
  * @description
@@ -181,7 +182,11 @@ export class BullMQJobQueueStrategy implements InspectableJobQueueStrategy {
     ): Promise<void> {
         this.queueNameProcessFnMap.set(queueName, process);
         if (!this.worker) {
-            this.worker = new Worker(QUEUE_NAME, this.workerProcessor).on('error', (e: any) =>
+            const options: WorkerOptions = {
+                concurrency: DEFAULT_CONCURRENCY,
+                ...this.options.workerOptions,
+            };
+            this.worker = new Worker(QUEUE_NAME, this.workerProcessor, options).on('error', (e: any) =>
                 Logger.error(`BullMQ Worker error: ${e.message}`, loggerCtx, e.stack),
             );
         }

+ 18 - 0
packages/job-queue-plugin/src/bullmq/plugin.ts

@@ -52,6 +52,24 @@ import { BullMQPluginOptions } from './types';
  * };
  * ```
  *
+ * ## Concurrency
+ *
+ * The default concurrency of a single worker is 3, i.e. up to 3 jobs will be processed at the same time.
+ * You can change the concurrency in the `workerOptions` passed to the `init()` method:
+ *
+ * @example
+ * ```TypeScript
+ * const config: VendureConfig = {
+ *   plugins: [
+ *     BullMQJobQueuePlugin.init({
+ *       workerOptions: {
+ *         concurrency: 10,
+ *       },
+ *     }),
+ *   ],
+ * };
+ * ```
+ *
  * @docsCategory job-queue-plugin
  */
 @VendurePlugin({