default-job-queue-plugin.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /**
  20. * @description
  21. * The interval in ms between polling the database for new jobs. If many job queues
  22. * are active, the polling may cause undue load on the database, in which case this value
  23. * should be increased to e.g. 1000.
  24. *
  25. * @default 200
  26. */
  27. pollInterval?: number | ((queueName: string) => number);
  28. /**
  29. * @description
  30. * How many jobs from a given queue to process concurrently.
  31. *
  32. * @default 1
  33. */
  34. concurrency?: number;
  35. /**
  36. * @description
  37. * The strategy used to decide how long to wait before retrying a failed job.
  38. *
  39. * @default () => 1000
  40. */
  41. backoffStrategy?: BackoffStrategy;
  42. /**
  43. * @description
  44. * When a job is added to the JobQueue using `JobQueue.add()`, the calling
  45. * code may specify the number of retries in case of failure. This option allows
  46. * you to override that number and specify your own number of retries based on
  47. * the job being added.
  48. *
  49. * @example
  50. * ```TypeScript
  51. * setRetries: (queueName, job) => {
  52. * if (queueName === 'send-email') {
  53. * // Override the default number of retries
  54. * // for the 'send-email' job because we have
  55. * // a very unreliable email service.
  56. * return 10;
  57. * }
  58. * return job.retries;
  59. * }
  60. * ```
  61. * @param queueName
  62. * @param job
  63. */
  64. setRetries?: (queueName: string, job: Job) => number;
  65. /**
  66. * @description
  67. * If set to `true`, the database will be used to store buffered jobs. This is
  68. * recommended for production.
  69. *
  70. * When enabled, a new `JobRecordBuffer` database entity will be defined which will
  71. * require a migration when first enabling this option.
  72. *
  73. * @since 1.3.0
  74. */
  75. useDatabaseForBuffer?: boolean;
  76. }
  77. /**
  78. * @description
  79. * A plugin which configures Vendure to use the SQL database to persist the JobQueue jobs using the {@link SqlJobQueueStrategy}. If you add this
  80. * plugin to an existing Vendure installation, you'll need to run a [database migration](/docs/developer-guide/migrations), since this
  81. * plugin will add a new "job_record" table to the database.
  82. *
  83. * @example
  84. * ```TypeScript
  85. * import { DefaultJobQueuePlugin, VendureConfig } from '\@vendure/core';
  86. *
  87. * export const config: VendureConfig = {
  88. * // Add an instance of the plugin to the plugins array
  89. * plugins: [
  90. * DefaultJobQueuePlugin,
  91. * ],
  92. * };
  93. * ```
  94. *
  95. * ## Configuration
  96. *
  97. * It is possible to configure the behaviour of the {@link SqlJobQueueStrategy} by passing options to the static `init()` function:
  98. *
  99. * ### pollInterval
  100. * The interval in ms between polling for new jobs. The default is 200ms.
  101. * Using a longer interval reduces load on the database but results in a slight
  102. * delay in processing jobs. For more control, it is possible to supply a function which can specify
  103. * a pollInterval based on the queue name:
  104. *
  105. * @example
  106. * ```TypeScript
  107. * export const config: VendureConfig = {
  108. * plugins: [
  109. * DefaultJobQueuePlugin.init({
  110. * pollInterval: queueName => {
  111. * if (queueName === 'cart-recovery-email') {
  112. * // This queue does not need to be polled so frequently,
  113. * // so we set a longer interval in order to reduce load
  114. * // on the database.
  115. * return 10000;
  116. * }
  117. * return 200;
  118. * },
  119. * }),
  120. * ],
  121. * };
  122. * ```
  123. * ### concurrency
  124. * The number of jobs to process concurrently per worker. Defaults to 1.
  125. *
  126. * ### backoffStrategy
  127. * Defines the backoff strategy used when retrying failed jobs. In other words, if a job fails
  128. * and is configured to be re-tried, how long should we wait before the next attempt?
  129. *
  130. * By default, a job will be retried as soon as possible, but in some cases this is not desirable. For example,
  131. * a job may interact with an unreliable 3rd-party API which is sensitive to too many requests. In this case, an
  132. * exponential backoff may be used which progressively increases the delay between each subsequent retry.
  133. *
  134. * @example
  135. * ```TypeScript
  136. * export const config: VendureConfig = {
  137. * plugins: [
  138. * DefaultJobQueuePlugin.init({
  139. * pollInterval: 5000,
  140. * concurrency: 2
  141. * backoffStrategy: (queueName, attemptsMade, job) => {
  142. * if (queueName === 'transcode-video') {
  143. * // exponential backoff example
  144. * return (attemptsMade ** 2) * 1000;
  145. * }
  146. *
  147. * // A default delay for all other queues
  148. * return 1000;
  149. * },
  150. * setRetries: (queueName, job) => {
  151. * if (queueName === 'send-email') {
  152. * // Override the default number of retries
  153. * // for the 'send-email' job because we have
  154. * // a very unreliable email service.
  155. * return 10;
  156. * }
  157. * return job.retries;
  158. * }
  159. * }),
  160. * ],
  161. * };
  162. * ```
  163. *
  164. * @docsCategory JobQueue
  165. * @docsWeight 0
  166. */
  167. @VendurePlugin({
  168. imports: [PluginCommonModule],
  169. entities: () =>
  170. DefaultJobQueuePlugin.options.useDatabaseForBuffer === true
  171. ? [JobRecord, JobRecordBuffer]
  172. : [JobRecord],
  173. configuration: config => {
  174. const { pollInterval, concurrency, backoffStrategy, setRetries } =
  175. DefaultJobQueuePlugin.options ?? {};
  176. config.jobQueueOptions.jobQueueStrategy = new SqlJobQueueStrategy({
  177. concurrency,
  178. pollInterval,
  179. backoffStrategy,
  180. setRetries,
  181. });
  182. if (DefaultJobQueuePlugin.options.useDatabaseForBuffer === true) {
  183. config.jobQueueOptions.jobBufferStorageStrategy = new SqlJobBufferStorageStrategy();
  184. }
  185. return config;
  186. },
  187. })
  188. export class DefaultJobQueuePlugin {
  189. /** @internal */
  190. static options: DefaultJobQueueOptions = {};
  191. static init(options: DefaultJobQueueOptions): Type<DefaultJobQueuePlugin> {
  192. DefaultJobQueuePlugin.options = options;
  193. return DefaultJobQueuePlugin;
  194. }
  195. }