default-job-queue-plugin.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Type } from '@vendure/common/lib/shared-types';
  2. import { BackoffStrategy } from '../../job-queue/polling-job-queue-strategy';
  3. import { PluginCommonModule } from '../plugin-common.module';
  4. import { VendurePlugin } from '../vendure-plugin';
  5. import { JobRecord } from './job-record.entity';
  6. import { SqlJobQueueStrategy } from './sql-job-queue-strategy';
  7. /**
  8. * @description
  9. * A plugin which configures Vendure to use the SQL database to persist the JobQueue jobs using the {@link SqlJobQueueStrategy}. If you add this
  10. * plugin to an existing Vendure installation, you'll need to run a [database migration](/docs/developer-guide/migrations), since this
  11. * plugin will add a new "job_record" table to the database.
  12. *
  13. * @example
  14. * ```TypeScript
  15. * import { DefaultJobQueuePlugin, VendureConfig } from '\@vendure/core';
  16. *
  17. * export const config: VendureConfig = {
  18. * // Add an instance of the plugin to the plugins array
  19. * plugins: [
  20. * DefaultJobQueuePlugin,
  21. * ],
  22. * };
  23. * ```
  24. *
  25. * ## Configuration
  26. *
  27. * It is possible to configure the behaviour of the {@link SqlJobQueueStrategy} by passing options to the static `init()` function:
  28. *
  29. * ### pollInterval
  30. * The interval in ms between polling for new jobs. The default is 200ms.
  31. * Using a longer interval reduces load on the database but results in a slight
  32. * delay in processing jobs.
  33. *
  34. * ### concurrency
  35. * The number of jobs to process concurrently per worker. Defaults to 1.
  36. *
  37. * ### backoffStrategy
  38. * Defines the backoff strategy used when retrying failed jobs. In other words, if a job fails
  39. * and is configured to be re-tried, how long should we wait before the next attempt?
  40. *
  41. * By default a job will be retried as soon as possible, but in some cases this is not desirable. For example,
  42. * a job may interact with an unreliable 3rd-party API which is sensitive to too many requests. In this case, an
  43. * exponential backoff may be used which progressively increases the delay between each subsequent retry.
  44. *
  45. * @example
  46. * ```TypeScript
  47. * export const config: VendureConfig = {
  48. * plugins: [
  49. * DefaultJobQueuePlugin.init({
  50. * pollInterval: 5000,
  51. * concurrency: 2
  52. * backoffStrategy: (queueName, attemptsMade, job) => {
  53. * if (queueName === 'transcode-video') {
  54. * // exponential backoff example
  55. * return (attemptsMade ** 2) * 1000;
  56. * }
  57. *
  58. * // A default delay for all other queues
  59. * return 1000;
  60. * },
  61. * }),
  62. * ],
  63. * };
  64. * ```
  65. *
  66. * @docsCategory JobQueue
  67. */
  68. @VendurePlugin({
  69. imports: [PluginCommonModule],
  70. entities: [JobRecord],
  71. configuration: config => {
  72. const { pollInterval, concurrency, backoffStrategy } = DefaultJobQueuePlugin.options ?? {};
  73. config.jobQueueOptions.jobQueueStrategy = new SqlJobQueueStrategy({
  74. concurrency,
  75. pollInterval,
  76. backoffStrategy,
  77. });
  78. return config;
  79. },
  80. })
  81. export class DefaultJobQueuePlugin {
  82. /** @internal */
  83. static options: { pollInterval?: number; concurrency?: number; backoffStrategy?: BackoffStrategy };
  84. static init(options: {
  85. pollInterval?: number;
  86. concurrency?: number;
  87. backoffStrategy?: BackoffStrategy;
  88. }): Type<DefaultJobQueuePlugin> {
  89. DefaultJobQueuePlugin.options = options;
  90. return DefaultJobQueuePlugin;
  91. }
  92. }