with-job-queue.ts 1.1 KB

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