ソースを参照

docs: Improve job queue docs

Michael Bromley 11 ヶ月 前
コミット
4595b40f90
1 ファイル変更54 行追加1 行削除
  1. 54 1
      docs/docs/guides/developer-guide/worker-job-queue/index.mdx

+ 54 - 1
docs/docs/guides/developer-guide/worker-job-queue/index.mdx

@@ -324,7 +324,7 @@ export class ProductVideoPlugin {}
 
 ### Passing the RequestContext
 
-It is common to need to pass the [RequestContext object](/reference/typescript-api/request/request-context) to the `process` function of a job, since `ctx` is required by many Vendure
+Sometimes you need to pass the [RequestContext object](/reference/typescript-api/request/request-context) to the `process` function of a job, since `ctx` is required by many Vendure
 service methods that you may be using inside your `process` function. However, the `RequestContext` object itself is not serializable,
 so it cannot be passed directly to the `JobQueue.add()` method. Instead, you can serialize the `RequestContext` using the [`RequestContext.serialize()`
 method](/reference/typescript-api/request/request-context/#serialize), and then deserialize it in the `process` function using the static `deserialize` method:
@@ -363,6 +363,59 @@ class ProductExportService implements OnModuleInit {
 }
 ```
 
+:::warning
+Serializing the RequestContext should be done with caution, since it is a relatively large object and will significantly increase the size of the job data.
+
+In cases where the job is created in large quantities (hundreds or thousands of jobs per day), this can lead to performance issues. Especially
+when using the [BullMQJobQueuePlugin](/reference/core-plugins/job-queue-plugin/bull-mqjob-queue-plugin/), which stores the job data in Redis, the
+size of the job data can lead to too much memory usage which can cause the Redis server to crash.
+:::
+
+Instead of serializing the entire RequestContext, consider passing only the necessary data you need and then reconstructing the RequestContext in the `process` function:
+
+```ts
+import { Injectable, OnModuleInit } from '@nestjs/common';
+import { JobQueue, JobQueueService,
+    RequestContext, ID, LanguageCode, RequestContextService } from '@vendure/core';
+
+@Injectable()
+class ProductExportService implements OnModuleInit {
+
+    // highlight-next-line
+    private jobQueue: JobQueue<{ channelToken: string; languageCode: LanguageCode; }>;
+
+    constructor(private jobQueueService: JobQueueService,
+                private requestContextService: RequestContextService) {
+    }
+
+    async onModuleInit() {
+        this.jobQueue = await this.jobQueueService.createQueue({
+            name: 'export-products',
+            process: async job => {
+                // highlight-start
+                // Reconstruct the RequestContext from the passed data
+                const ctx = await this.requestContextService.create({
+                    channelOrToken: job.data.channelToken,
+                    languageCode: job.data.languageCode,
+                })
+                // highlight-end
+                // ... logic to export the product omitted for brevity
+            },
+        });
+    }
+
+    exportAllProducts(ctx: RequestContext) {
+        // highlight-start
+        // Pass only the necessary data
+        return this.jobQueue.add({
+            channelId: ctx.channel.token,
+            languageCode: ctx.languageCode
+        });
+        // highlight-end
+    }
+}
+```
+
 ### Handling job cancellation
 
 It is possible for an administrator to cancel a running job. Doing so will cause the configured job queue strategy to mark the job as cancelled, but