flow-producer.d.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { EventEmitter } from 'events';
  2. import { ChainableCommander } from 'ioredis';
  3. import { FlowJob, FlowQueuesOpts, FlowOpts, IoredisListener, ParentOptions, QueueBaseOptions, RedisClient, Tracer, ContextManager } from '../interfaces';
  4. import { Job } from './job';
  5. import { KeysMap, QueueKeys } from './queue-keys';
  6. import { RedisConnection } from './redis-connection';
  7. export interface AddNodeOpts {
  8. multi: ChainableCommander;
  9. node: FlowJob;
  10. parent?: {
  11. parentOpts: ParentOptions;
  12. parentDependenciesKey: string;
  13. };
  14. /**
  15. * Queues options that will be applied in each node depending on queue name presence.
  16. */
  17. queuesOpts?: FlowQueuesOpts;
  18. }
  19. export interface AddChildrenOpts {
  20. multi: ChainableCommander;
  21. nodes: FlowJob[];
  22. parent: {
  23. parentOpts: ParentOptions;
  24. parentDependenciesKey: string;
  25. };
  26. queuesOpts?: FlowQueuesOpts;
  27. }
  28. export interface NodeOpts {
  29. /**
  30. * Root job queue name.
  31. */
  32. queueName: string;
  33. /**
  34. * Prefix included in job key.
  35. */
  36. prefix?: string;
  37. /**
  38. * Root job id.
  39. */
  40. id: string;
  41. /**
  42. * Maximum depth or levels to visit in the tree.
  43. */
  44. depth?: number;
  45. /**
  46. * Maximum quantity of children per type (processed, unprocessed).
  47. */
  48. maxChildren?: number;
  49. }
  50. export interface JobNode {
  51. job: Job;
  52. children?: JobNode[];
  53. }
  54. export interface FlowProducerListener extends IoredisListener {
  55. /**
  56. * Listen to 'error' event.
  57. *
  58. * This event is triggered when an error is throw.
  59. */
  60. error: (failedReason: Error) => void;
  61. }
  62. /**
  63. * This class allows to add jobs with dependencies between them in such
  64. * a way that it is possible to build complex flows.
  65. * Note: A flow is a tree-like structure of jobs that depend on each other.
  66. * Whenever the children of a given parent are completed, the parent
  67. * will be processed, being able to access the children's result data.
  68. * All Jobs can be in different queues, either children or parents,
  69. */
  70. export declare class FlowProducer extends EventEmitter {
  71. opts: QueueBaseOptions;
  72. toKey: (name: string, type: string) => string;
  73. keys: KeysMap;
  74. closing: Promise<void> | undefined;
  75. queueKeys: QueueKeys;
  76. protected connection: RedisConnection;
  77. protected telemetry: {
  78. tracer: Tracer | undefined;
  79. contextManager: ContextManager | undefined;
  80. };
  81. constructor(opts?: QueueBaseOptions, Connection?: typeof RedisConnection);
  82. emit<U extends keyof FlowProducerListener>(event: U, ...args: Parameters<FlowProducerListener[U]>): boolean;
  83. off<U extends keyof FlowProducerListener>(eventName: U, listener: FlowProducerListener[U]): this;
  84. on<U extends keyof FlowProducerListener>(event: U, listener: FlowProducerListener[U]): this;
  85. once<U extends keyof FlowProducerListener>(event: U, listener: FlowProducerListener[U]): this;
  86. /**
  87. * Returns a promise that resolves to a redis client. Normally used only by subclasses.
  88. */
  89. get client(): Promise<RedisClient>;
  90. /**
  91. * Helper to easily extend Job class calls.
  92. */
  93. protected get Job(): typeof Job;
  94. waitUntilReady(): Promise<RedisClient>;
  95. /**
  96. * Adds a flow.
  97. *
  98. * This call would be atomic, either it fails and no jobs will
  99. * be added to the queues, or it succeeds and all jobs will be added.
  100. *
  101. * @param flow - an object with a tree-like structure where children jobs
  102. * will be processed before their parents.
  103. * @param opts - options that will be applied to the flow object.
  104. */
  105. add(flow: FlowJob, opts?: FlowOpts): Promise<JobNode>;
  106. /**
  107. * Get a flow.
  108. *
  109. * @param opts - an object with options for getting a JobNode.
  110. */
  111. getFlow(opts: NodeOpts): Promise<JobNode>;
  112. /**
  113. * Adds multiple flows.
  114. *
  115. * A flow is a tree-like structure of jobs that depend on each other.
  116. * Whenever the children of a given parent are completed, the parent
  117. * will be processed, being able to access the children's result data.
  118. *
  119. * All Jobs can be in different queues, either children or parents,
  120. * however this call would be atomic, either it fails and no jobs will
  121. * be added to the queues, or it succeeds and all jobs will be added.
  122. *
  123. * @param flows - an array of objects with a tree-like structure where children jobs
  124. * will be processed before their parents.
  125. */
  126. addBulk(flows: FlowJob[]): Promise<JobNode[]>;
  127. /**
  128. * Add a node (job) of a flow to the queue. This method will recursively
  129. * add all its children as well. Note that a given job can potentially be
  130. * a parent and a child job at the same time depending on where it is located
  131. * in the tree hierarchy.
  132. *
  133. * @param multi - ioredis ChainableCommander
  134. * @param node - the node representing a job to be added to some queue
  135. * @param parent - parent data sent to children to create the "links" to their parent
  136. * @returns
  137. */
  138. protected addNode({ multi, node, parent, queuesOpts, }: AddNodeOpts): Promise<JobNode>;
  139. /**
  140. * Adds nodes (jobs) of multiple flows to the queue. This method will recursively
  141. * add all its children as well. Note that a given job can potentially be
  142. * a parent and a child job at the same time depending on where it is located
  143. * in the tree hierarchy.
  144. *
  145. * @param multi - ioredis ChainableCommander
  146. * @param nodes - the nodes representing jobs to be added to some queue
  147. * @returns
  148. */
  149. protected addNodes(multi: ChainableCommander, nodes: FlowJob[]): Promise<JobNode[]>;
  150. private getNode;
  151. private addChildren;
  152. private getChildren;
  153. /**
  154. * Helper factory method that creates a queue-like object
  155. * required to create jobs in any queue.
  156. *
  157. * @param node -
  158. * @param queueKeys -
  159. * @returns
  160. */
  161. private queueFromNode;
  162. /**
  163. *
  164. * Closes the connection and returns a promise that resolves when the connection is closed.
  165. */
  166. close(): Promise<void>;
  167. /**
  168. *
  169. * Force disconnects a connection.
  170. */
  171. disconnect(): Promise<void>;
  172. }