queue-events.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { JobProgress } from '../types';
  2. import { IoredisListener, QueueEventsOptions } from '../interfaces';
  3. import { QueueBase } from './queue-base';
  4. import { RedisConnection } from './redis-connection';
  5. export interface QueueEventsListener extends IoredisListener {
  6. /**
  7. * Listen to 'active' event.
  8. *
  9. * This event is triggered when a job enters the 'active' state, meaning it is being processed.
  10. *
  11. * @param args - An object containing details about the job that became active.
  12. * - `jobId`: The unique identifier of the job that entered the active state.
  13. * - `prev`: The previous state of the job before it became active (e.g., 'waiting'), if applicable.
  14. *
  15. * @param id - The identifier of the event.
  16. */
  17. active: (args: {
  18. jobId: string;
  19. prev?: string;
  20. }, id: string) => void;
  21. /**
  22. * Listen to 'added' event.
  23. *
  24. * This event is triggered when a job is created and added to the queue.
  25. *
  26. * @param args - An object containing details about the newly added job.
  27. * - `jobId` - The unique identifier of the job that was added.
  28. * - `name` - The name of the job, typically indicating its type or purpose.
  29. * @param id - The identifier of the event.
  30. */
  31. added: (args: {
  32. jobId: string;
  33. name: string;
  34. }, id: string) => void;
  35. /**
  36. * Listen to 'cleaned' event.
  37. *
  38. * This event is triggered when jobs are cleaned (e.g., removed) from the queue, typically via a cleanup method.
  39. *
  40. * @param args - An object containing the count of cleaned jobs.
  41. * - `count` - The number of jobs that were cleaned, represented as a string due to Redis serialization.
  42. * @param id - The identifier of the event.
  43. */
  44. cleaned: (args: {
  45. count: string;
  46. }, id: string) => void;
  47. /**
  48. * Listen to 'completed' event.
  49. *
  50. * This event is triggered when a job has successfully completed its execution.
  51. *
  52. * @param args - An object containing details about the completed job.
  53. * - `jobId` - The unique identifier of the job that completed.
  54. * - `returnvalue` - The return value of the job, serialized as a string.
  55. * - `prev` - The previous state of the job before completion (e.g., 'active'), if applicable.
  56. * @param id - The identifier of the event.
  57. */
  58. completed: (args: {
  59. jobId: string;
  60. returnvalue: string;
  61. prev?: string;
  62. }, id: string) => void;
  63. /**
  64. * Listen to 'debounced' event.
  65. *
  66. * @deprecated Use the 'deduplicated' event instead.
  67. *
  68. * This event is triggered when a job is debounced because a job with the same debounceId still exists.
  69. *
  70. * @param args - An object containing details about the debounced job.
  71. * - `jobId` - The unique identifier of the job that was debounced.
  72. * - `debounceId` - The identifier used to debounce the job, preventing duplicate processing.
  73. * @param id - The identifier of the event.
  74. */
  75. debounced: (args: {
  76. jobId: string;
  77. debounceId: string;
  78. }, id: string) => void;
  79. /**
  80. * Listen to 'deduplicated' event.
  81. *
  82. * This event is triggered when a job is not added to the queue because a job with the same deduplicationId
  83. * already exists.
  84. *
  85. * @param args - An object containing details about the deduplicated job.
  86. * - `jobId` - The unique identifier of the job that was attempted to be added.
  87. * - `deduplicationId` - The deduplication identifier that caused the job to be deduplicated.
  88. * - `deduplicatedJobId` - The unique identifier of the existing job that caused the deduplication.
  89. * @param id - The identifier of the event.
  90. */
  91. deduplicated: (args: {
  92. jobId: string;
  93. deduplicationId: string;
  94. deduplicatedJobId: string;
  95. }, id: string) => void;
  96. /**
  97. * Listen to 'delayed' event.
  98. *
  99. * This event is triggered when a job is scheduled with a delay before it becomes active.
  100. *
  101. * @param args - An object containing details about the delayed job.
  102. * - `jobId` - The unique identifier of the job that was delayed.
  103. * - `delay` - The delay duration in milliseconds before the job becomes active.
  104. * @param id - The identifier of the event.
  105. */
  106. delayed: (args: {
  107. jobId: string;
  108. delay: number;
  109. }, id: string) => void;
  110. /**
  111. * Listen to 'drained' event.
  112. *
  113. * This event is triggered when the queue has drained its waiting list, meaning there are no jobs
  114. * in the 'waiting' state.
  115. * Note that there could still be delayed jobs waiting their timers to expire
  116. * and this event will still be triggered as long as the waiting list has emptied.
  117. *
  118. * @param id - The identifier of the event.
  119. */
  120. drained: (id: string) => void;
  121. /**
  122. * Listen to 'duplicated' event.
  123. *
  124. * This event is triggered when a job is not created because a job with the same identifier already exists.
  125. *
  126. * @param args - An object containing the job identifier.
  127. * - `jobId` - The unique identifier of the job that was attempted to be added.
  128. * @param id - The identifier of the event.
  129. */
  130. duplicated: (args: {
  131. jobId: string;
  132. }, id: string) => void;
  133. /**
  134. * Listen to 'error' event.
  135. *
  136. * This event is triggered when an error in the Redis backend is thrown.
  137. */
  138. error: (args: Error) => void;
  139. /**
  140. * Listen to 'failed' event.
  141. *
  142. * This event is triggered when a job fails by throwing an exception during execution.
  143. *
  144. * @param args - An object containing details about the failed job.
  145. * - `jobId` - The unique identifier of the job that failed.
  146. * - `failedReason` - The reason or message describing why the job failed.
  147. * - `prev` - The previous state of the job before failure (e.g., 'active'), if applicable.
  148. * @param id - The identifier of the event.
  149. */
  150. failed: (args: {
  151. jobId: string;
  152. failedReason: string;
  153. prev?: string;
  154. }, id: string) => void;
  155. /**
  156. * Listen to 'paused' event.
  157. *
  158. * This event is triggered when the queue is paused, halting the processing of new jobs.
  159. *
  160. * @param args - An empty object (no additional data provided).
  161. * @param id - The identifier of the event.
  162. */
  163. paused: (args: object, id: string) => void;
  164. /**
  165. * Listen to 'progress' event.
  166. *
  167. * This event is triggered when a job updates its progress via the `Job#updateProgress()` method, allowing
  168. * progress or custom data to be communicated externally.
  169. *
  170. * @param args - An object containing the job identifier and progress data.
  171. * - `jobId` - The unique identifier of the job reporting progress.
  172. * - `data` - The progress data, which can be a number (e.g., percentage) or an object with custom data.
  173. * @param id - The identifier of the event.
  174. */
  175. progress: (args: {
  176. jobId: string;
  177. data: JobProgress;
  178. }, id: string) => void;
  179. /**
  180. * Listen to 'removed' event.
  181. *
  182. * This event is triggered when a job is manually removed from the queue.
  183. *
  184. * @param args - An object containing details about the removed job.
  185. * - `jobId` - The unique identifier of the job that was removed.
  186. * - `prev` - The previous state of the job before removal (e.g., 'active' or 'waiting').
  187. * @param id - The identifier of the event.
  188. */
  189. removed: (args: {
  190. jobId: string;
  191. prev: string;
  192. }, id: string) => void;
  193. /**
  194. * Listen to 'resumed' event.
  195. *
  196. * This event is triggered when the queue is resumed, allowing job processing to continue.
  197. *
  198. * @param args - An empty object (no additional data provided).
  199. * @param id - The identifier of the event.
  200. */
  201. resumed: (args: object, id: string) => void;
  202. /**
  203. * Listen to 'retries-exhausted' event.
  204. *
  205. * This event is triggered when a job has exhausted its maximum retry attempts after repeated failures.
  206. *
  207. * @param args - An object containing details about the job that exhausted retries.
  208. * - `jobId` - The unique identifier of the job that exhausted its retries.
  209. * - `attemptsMade` - The number of retry attempts made, represented as a string
  210. * (due to Redis serialization).
  211. * @param id - The identifier of the event.
  212. */
  213. 'retries-exhausted': (args: {
  214. jobId: string;
  215. attemptsMade: string;
  216. }, id: string) => void;
  217. /**
  218. * Listen to 'stalled' event.
  219. *
  220. * This event is triggered when a job moves from 'active' back to 'waiting' or
  221. * 'failed' because the processor could not renew its lock, indicating a
  222. * potential processing issue.
  223. *
  224. * @param args - An object containing the job identifier.
  225. * - `jobId` - The unique identifier of the job that stalled.
  226. * @param id - The identifier of the event.
  227. */
  228. stalled: (args: {
  229. jobId: string;
  230. }, id: string) => void;
  231. /**
  232. * Listen to 'waiting' event.
  233. *
  234. * This event is triggered when a job enters the 'waiting' state, indicating it is queued and
  235. * awaiting processing.
  236. *
  237. * @param args - An object containing details about the job in the waiting state.
  238. * - `jobId` - The unique identifier of the job that is waiting.
  239. * - `prev` - The previous state of the job before entering 'waiting' (e.g., 'stalled'),
  240. * if applicable.
  241. * @param id - The identifier of the event.
  242. */
  243. waiting: (args: {
  244. jobId: string;
  245. prev?: string;
  246. }, id: string) => void;
  247. /**
  248. * Listen to 'waiting-children' event.
  249. *
  250. * This event is triggered when a job enters the 'waiting-children' state, indicating it is
  251. * waiting for its child jobs to complete.
  252. *
  253. * @param args - An object containing the job identifier.
  254. * - `jobId` - The unique identifier of the job waiting for its children.
  255. * @param id - The identifier of the event.
  256. */
  257. 'waiting-children': (args: {
  258. jobId: string;
  259. }, id: string) => void;
  260. }
  261. type CustomParameters<T> = T extends (...args: infer Args) => void ? Args : never;
  262. type KeyOf<T extends object> = Extract<keyof T, string>;
  263. /**
  264. * The QueueEvents class is used for listening to the global events
  265. * emitted by a given queue.
  266. *
  267. * This class requires a dedicated redis connection.
  268. *
  269. */
  270. export declare class QueueEvents extends QueueBase {
  271. private running;
  272. private blocking;
  273. constructor(name: string, { connection, autorun, ...opts }?: QueueEventsOptions, Connection?: typeof RedisConnection);
  274. emit<QEL extends QueueEventsListener = QueueEventsListener, U extends KeyOf<QEL> = KeyOf<QEL>>(event: U, ...args: CustomParameters<QEL[U]>): boolean;
  275. off<QEL extends QueueEventsListener = QueueEventsListener, U extends KeyOf<QEL> = KeyOf<QEL>>(eventName: U, listener: QEL[U]): this;
  276. on<QEL extends QueueEventsListener = QueueEventsListener, U extends KeyOf<QEL> = KeyOf<QEL>>(event: U, listener: QEL[U]): this;
  277. once<QEL extends QueueEventsListener = QueueEventsListener, U extends KeyOf<QEL> = KeyOf<QEL>>(event: U, listener: QEL[U]): this;
  278. /**
  279. * Manually starts running the event consumming loop. This shall be used if you do not
  280. * use the default "autorun" option on the constructor.
  281. */
  282. run(): Promise<void>;
  283. private consumeEvents;
  284. /**
  285. * Stops consuming events and close the underlying Redis connection if necessary.
  286. *
  287. * @returns
  288. */
  289. close(): Promise<void>;
  290. }
  291. export {};