Browse Source

feat(job-queue-plugin): Implement default cleanup of old BullMQ jobs

Relates to #1425
Michael Bromley 2 years ago
parent
commit
6c1d7bb17d

+ 13 - 1
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<void> {
     async init(injector: Injector): Promise<void> {
         const options = injector.get<BullMQPluginOptions>(BULLMQ_PLUGIN_OPTIONS);
         const options = injector.get<BullMQPluginOptions>(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 =
         this.connectionOptions =
             options.connection ??
             options.connection ??
             ({
             ({

+ 31 - 0
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
  * @docsCategory core plugins/JobQueuePlugin
  */
  */
 @VendurePlugin({
 @VendurePlugin({