Skip to content

Commit 4595b40

Browse files
docs: Improve job queue docs
1 parent 519415e commit 4595b40

File tree

1 file changed

+54
-1
lines changed
  • docs/docs/guides/developer-guide/worker-job-queue

1 file changed

+54
-1
lines changed

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ export class ProductVideoPlugin {}
324324

325325
### Passing the RequestContext
326326

327-
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
327+
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
328328
service methods that you may be using inside your `process` function. However, the `RequestContext` object itself is not serializable,
329329
so it cannot be passed directly to the `JobQueue.add()` method. Instead, you can serialize the `RequestContext` using the [`RequestContext.serialize()`
330330
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 {
363363
}
364364
```
365365

366+
:::warning
367+
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.
368+
369+
In cases where the job is created in large quantities (hundreds or thousands of jobs per day), this can lead to performance issues. Especially
370+
when using the [BullMQJobQueuePlugin](/reference/core-plugins/job-queue-plugin/bull-mqjob-queue-plugin/), which stores the job data in Redis, the
371+
size of the job data can lead to too much memory usage which can cause the Redis server to crash.
372+
:::
373+
374+
Instead of serializing the entire RequestContext, consider passing only the necessary data you need and then reconstructing the RequestContext in the `process` function:
375+
376+
```ts
377+
import { Injectable, OnModuleInit } from '@nestjs/common';
378+
import { JobQueue, JobQueueService,
379+
RequestContext, ID, LanguageCode, RequestContextService } from '@vendure/core';
380+
381+
@Injectable()
382+
class ProductExportService implements OnModuleInit {
383+
384+
// highlight-next-line
385+
private jobQueue: JobQueue<{ channelToken: string; languageCode: LanguageCode; }>;
386+
387+
constructor(private jobQueueService: JobQueueService,
388+
private requestContextService: RequestContextService) {
389+
}
390+
391+
async onModuleInit() {
392+
this.jobQueue = await this.jobQueueService.createQueue({
393+
name: 'export-products',
394+
process: async job => {
395+
// highlight-start
396+
// Reconstruct the RequestContext from the passed data
397+
const ctx = await this.requestContextService.create({
398+
channelOrToken: job.data.channelToken,
399+
languageCode: job.data.languageCode,
400+
})
401+
// highlight-end
402+
// ... logic to export the product omitted for brevity
403+
},
404+
});
405+
}
406+
407+
exportAllProducts(ctx: RequestContext) {
408+
// highlight-start
409+
// Pass only the necessary data
410+
return this.jobQueue.add({
411+
channelId: ctx.channel.token,
412+
languageCode: ctx.languageCode
413+
});
414+
// highlight-end
415+
}
416+
}
417+
```
418+
366419
### Handling job cancellation
367420

368421
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

0 commit comments

Comments
 (0)