From 9fda858b1c987330237a35a9ac0f841c2405767f Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Tue, 5 Oct 2021 10:01:42 +0200 Subject: [PATCH] feat(job-queue-plugin): Allow config of retries/backoff for BullMQ Relates to #1111 --- .../src/bullmq/bullmq-job-queue-strategy.ts | 12 +++-- packages/job-queue-plugin/src/bullmq/types.ts | 47 +++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/packages/job-queue-plugin/src/bullmq/bullmq-job-queue-strategy.ts b/packages/job-queue-plugin/src/bullmq/bullmq-job-queue-strategy.ts index dc97e718f1..04259609bf 100644 --- a/packages/job-queue-plugin/src/bullmq/bullmq-job-queue-strategy.ts +++ b/packages/job-queue-plugin/src/bullmq/bullmq-job-queue-strategy.ts @@ -85,12 +85,14 @@ export class BullMQJobQueueStrategy implements InspectableJobQueueStrategy { } async add = {}>(job: Job): Promise> { + const retries = this.options.setRetries?.(job.queueName, job) ?? job.retries; + const backoff = this.options.setBackoff?.(job.queueName, job) ?? { + delay: 1000, + type: 'exponential', + }; const bullJob = await this.queue.add(job.queueName, job.data, { - attempts: job.retries + 1, - backoff: { - delay: 1000, - type: 'exponential', - }, + attempts: retries + 1, + backoff, }); return this.createVendureJob(bullJob); } diff --git a/packages/job-queue-plugin/src/bullmq/types.ts b/packages/job-queue-plugin/src/bullmq/types.ts index 052a63e3e9..295f488e27 100644 --- a/packages/job-queue-plugin/src/bullmq/types.ts +++ b/packages/job-queue-plugin/src/bullmq/types.ts @@ -1,3 +1,4 @@ +import { Job } from '@vendure/core'; import { ConnectionOptions, QueueSchedulerOptions, WorkerOptions } from 'bullmq'; import { QueueOptions } from 'bullmq'; @@ -7,6 +8,8 @@ import { QueueOptions } from 'bullmq'; * * @since 1.2.0 * @docsCategory job-queue-plugin + * @docsPage BullMQPluginOptions + * @docsWeight 0 */ export interface BullMQPluginOptions { /** @@ -38,4 +41,48 @@ export interface BullMQPluginOptions { * See the [BullMQ QueueSchedulerOptions docs](https://github.com/taskforcesh/bullmq/blob/master/docs/gitbook/api/bullmq.queuescheduleroptions.md) */ schedulerOptions?: Exclude; + /** + * @description + * When a job is added to the JobQueue using `JobQueue.add()`, the calling + * code may specify the number of retries in case of failure. This option allows + * you to override that number and specify your own number of retries based on + * the job being added. + * + * @since 1.3.0 + */ + setRetries?: (queueName: string, job: Job) => number; + /** + * @description + * This allows you to specify the backoff settings when a failed job gets retried. + * In other words, this determines how much time should pass before attempting to + * process the failed job again. If the function returns `undefined`, the default + * value of exponential/1000ms will be used. + * + * @example + * ```TypeScript + * setBackoff: (queueName, job) => { + * return { + * type: 'exponential', // or 'fixed' + * delay: 10000 // first retry after 10s, second retry after 20s, 40s,... + * }; + * } + * ``` + * @since 1.3.0 + * @default 'exponential', 1000 + */ + setBackoff?: (queueName: string, job: Job) => BackoffOptions | undefined; +} + +/** + * @description + * Configuration for the backoff function when retrying failed jobs. + * + * @since 1.3.0 + * @docsCategory job-queue-plugin + * @docsPage BullMQPluginOptions + * @docsWeight 1 + */ +export interface BackoffOptions { + type: 'exponential' | 'fixed'; + delay: number; }