default-job-queue-plugin.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { Type } from '@vendure/common/lib/shared-types';
  2. import { Job } from '../../job-queue/job';
  3. import { BackoffStrategy } from '../../job-queue/polling-job-queue-strategy';
  4. import { PluginCommonModule } from '../plugin-common.module';
  5. import { VendurePlugin } from '../vendure-plugin';
  6. import { JobRecordBuffer } from './job-record-buffer.entity';
  7. import { JobRecord } from './job-record.entity';
  8. import { SqlJobBufferStorageStrategy } from './sql-job-buffer-storage-strategy';
  9. import { SqlJobQueueStrategy } from './sql-job-queue-strategy';
  10. /**
  11. * @description
  12. * Configuration options for the DefaultJobQueuePlugin. These values get passed into the
  13. * {@link SqlJobQueueStrategy}.
  14. *
  15. * @docsCategory JobQueue
  16. * @docsPage DefaultJobQueuePlugin
  17. */
  18. export interface DefaultJobQueueOptions {
  19. pollInterval?: number | ((queueName: string) => number);
  20. concurrency?: number;
  21. backoffStrategy?: BackoffStrategy;
  22. setRetries?: (queueName: string, job: Job) => number;
  23. /**
  24. * @description
  25. * If set to `true`, the database will be used to store buffered jobs. This is
  26. * recommended for production.
  27. *
  28. * When enabled, a new `JobRecordBuffer` database entity will be defined which will
  29. * require a migration when first enabling this option.
  30. *
  31. * @since 1.3.0
  32. */
  33. useDatabaseForBuffer?: boolean;
  34. }
  35. /**
  36. * @description
  37. * A plugin which configures Vendure to use the SQL database to persist the JobQueue jobs using the {@link SqlJobQueueStrategy}. If you add this
  38. * plugin to an existing Vendure installation, you'll need to run a [database migration](/docs/developer-guide/migrations), since this
  39. * plugin will add a new "job_record" table to the database.
  40. *
  41. * @example
  42. * ```TypeScript
  43. * import { DefaultJobQueuePlugin, VendureConfig } from '\@vendure/core';
  44. *
  45. * export const config: VendureConfig = {
  46. * // Add an instance of the plugin to the plugins array
  47. * plugins: [
  48. * DefaultJobQueuePlugin,
  49. * ],
  50. * };
  51. * ```
  52. *
  53. * ## Configuration
  54. *
  55. * It is possible to configure the behaviour of the {@link SqlJobQueueStrategy} by passing options to the static `init()` function:
  56. *
  57. * ### pollInterval
  58. * The interval in ms between polling for new jobs. The default is 200ms.
  59. * Using a longer interval reduces load on the database but results in a slight
  60. * delay in processing jobs. For more control, it is possible to supply a function which can specify
  61. * a pollInterval based on the queue name:
  62. *
  63. * @example
  64. * ```TypeScript
  65. * export const config: VendureConfig = {
  66. * plugins: [
  67. * DefaultJobQueuePlugin.init({
  68. * pollInterval: queueName => {
  69. * if (queueName === 'cart-recovery-email') {
  70. * // This queue does not need to be polled so frequently,
  71. * // so we set a longer interval in order to reduce load
  72. * // on the database.
  73. * return 10000;
  74. * }
  75. * return 200;
  76. * },
  77. * }),
  78. * ],
  79. * };
  80. * ```
  81. * ### concurrency
  82. * The number of jobs to process concurrently per worker. Defaults to 1.
  83. *
  84. * ### backoffStrategy
  85. * Defines the backoff strategy used when retrying failed jobs. In other words, if a job fails
  86. * and is configured to be re-tried, how long should we wait before the next attempt?
  87. *
  88. * By default, a job will be retried as soon as possible, but in some cases this is not desirable. For example,
  89. * a job may interact with an unreliable 3rd-party API which is sensitive to too many requests. In this case, an
  90. * exponential backoff may be used which progressively increases the delay between each subsequent retry.
  91. *
  92. * @example
  93. * ```TypeScript
  94. * export const config: VendureConfig = {
  95. * plugins: [
  96. * DefaultJobQueuePlugin.init({
  97. * pollInterval: 5000,
  98. * concurrency: 2
  99. * backoffStrategy: (queueName, attemptsMade, job) => {
  100. * if (queueName === 'transcode-video') {
  101. * // exponential backoff example
  102. * return (attemptsMade ** 2) * 1000;
  103. * }
  104. *
  105. * // A default delay for all other queues
  106. * return 1000;
  107. * },
  108. * retries: (queueName, job) => {
  109. * if (queueName === 'send-email') {
  110. * // Override the default number of retries
  111. * // for the 'send-email' job because we have
  112. * // a very unreliable email service.
  113. * return 10;
  114. * }
  115. * return job.retries;
  116. * }
  117. * }),
  118. * ],
  119. * };
  120. * ```
  121. *
  122. * @docsCategory JobQueue
  123. * @docsWeight 0
  124. */
  125. @VendurePlugin({
  126. imports: [PluginCommonModule],
  127. entities: () =>
  128. DefaultJobQueuePlugin.options.useDatabaseForBuffer === true
  129. ? [JobRecord, JobRecordBuffer]
  130. : [JobRecord],
  131. configuration: config => {
  132. const { pollInterval, concurrency, backoffStrategy, setRetries } =
  133. DefaultJobQueuePlugin.options ?? {};
  134. config.jobQueueOptions.jobQueueStrategy = new SqlJobQueueStrategy({
  135. concurrency,
  136. pollInterval,
  137. backoffStrategy,
  138. setRetries,
  139. });
  140. if (DefaultJobQueuePlugin.options.useDatabaseForBuffer === true) {
  141. config.jobQueueOptions.jobBufferStorageStrategy = new SqlJobBufferStorageStrategy();
  142. }
  143. return config;
  144. },
  145. })
  146. export class DefaultJobQueuePlugin {
  147. /** @internal */
  148. static options: DefaultJobQueueOptions = {};
  149. static init(options: DefaultJobQueueOptions): Type<DefaultJobQueuePlugin> {
  150. DefaultJobQueuePlugin.options = options;
  151. return DefaultJobQueuePlugin;
  152. }
  153. }