From 6c1d7bb17da89d129d2b42181723fca41e085abf Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Tue, 26 Sep 2023 10:05:06 +0200 Subject: [PATCH] feat(job-queue-plugin): Implement default cleanup of old BullMQ jobs Relates to #1425 --- .../src/bullmq/bullmq-job-queue-strategy.ts | 14 ++++++++- .../job-queue-plugin/src/bullmq/plugin.ts | 31 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) 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 715cf4ba8b..9400c62284 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 @@ -48,7 +48,19 @@ export class BullMQJobQueueStrategy implements InspectableJobQueueStrategy { async init(injector: Injector): Promise { const options = injector.get(BULLMQ_PLUGIN_OPTIONS); - this.options = options; + this.options = { + ...options, + workerOptions: { + removeOnComplete: options.workerOptions?.removeOnComplete ?? { + age: 60 * 60 * 24 * 30, + count: 5000, + }, + removeOnFail: options.workerOptions?.removeOnFail ?? { + age: 60 * 60 * 24 * 30, + count: 5000, + }, + }, + }; this.connectionOptions = options.connection ?? ({ diff --git a/packages/job-queue-plugin/src/bullmq/plugin.ts b/packages/job-queue-plugin/src/bullmq/plugin.ts index f48730ac6c..c82d533af9 100644 --- a/packages/job-queue-plugin/src/bullmq/plugin.ts +++ b/packages/job-queue-plugin/src/bullmq/plugin.ts @@ -98,6 +98,37 @@ import { BullMQPluginOptions } from './types'; * }; * ``` * + * ## Removing old jobs + * + * By default, BullMQ will keep completed jobs in the `completed` set and failed jobs in the `failed` set. Over time, + * these sets can grow very large. Since Vendure v2.1, the default behaviour is to remove jobs from these sets after + * 30 days or after a maximum of 5,000 completed or failed jobs. + * + * This can be configured using the `removeOnComplete` and `removeOnFail` options: + * + * @example + * ```ts + * const config: VendureConfig = { + * plugins: [ + * BullMQJobQueuePlugin.init({ + * workerOptions: { + * removeOnComplete: { + * count: 500, + * }, + * removeOnFail: { + * age: 60 * 60 * 24 * 7, // 7 days + * count: 1000, + * }, + * } + * }), + * ], + * }; + * ``` + * + * The `count` option specifies the maximum number of jobs to keep in the set, while the `age` option specifies the + * maximum age of a job in seconds. If both options are specified, then the jobs kept will be the ones that satisfy + * both properties. + * * @docsCategory core plugins/JobQueuePlugin */ @VendurePlugin({