with-job-queue.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Controller, Get, OnModuleInit } from '@nestjs/common';
  2. import { JobQueue, JobQueueService, PluginCommonModule, VendurePlugin } from '@vendure/core';
  3. import { Subject } from 'rxjs';
  4. import { take } from 'rxjs/operators';
  5. @Controller('run-job')
  6. class TestController implements OnModuleInit {
  7. private queue: JobQueue;
  8. constructor(private jobQueueService: JobQueueService) {}
  9. async onModuleInit(): Promise<void> {
  10. this.queue = await this.jobQueueService.createQueue({
  11. name: 'test',
  12. process: job => {
  13. return PluginWithJobQueue.jobSubject
  14. .pipe(take(1))
  15. .toPromise()
  16. .then(() => {
  17. PluginWithJobQueue.jobHasDoneWork = true;
  18. });
  19. },
  20. });
  21. }
  22. @Get()
  23. async runJob() {
  24. await this.queue.add({});
  25. return true;
  26. }
  27. }
  28. @VendurePlugin({
  29. imports: [PluginCommonModule],
  30. controllers: [TestController],
  31. })
  32. export class PluginWithJobQueue {
  33. static jobHasDoneWork = false;
  34. static jobSubject = new Subject();
  35. }