job.d.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. import { BulkJobOptions, DependenciesOpts, JobJson, JobJsonRaw, MinimalJob, MinimalQueue, MoveToWaitingChildrenOpts, ParentKeys, ParentKeyOpts, RedisClient, RetryOptions } from '../interfaces';
  2. import { FinishedStatus, JobsOptions, JobState, JobJsonSandbox, RedisJobOptions, JobProgress } from '../types';
  3. import { Scripts } from './scripts';
  4. import type { QueueEvents } from './queue-events';
  5. export declare const PRIORITY_LIMIT: number;
  6. /**
  7. * Job
  8. *
  9. * This class represents a Job in the queue. Normally job are implicitly created when
  10. * you add a job to the queue with methods such as Queue.addJob( ... )
  11. *
  12. * A Job instance is also passed to the Worker's process function.
  13. *
  14. */
  15. export declare class Job<DataType = any, ReturnType = any, NameType extends string = string> implements MinimalJob<DataType, ReturnType, NameType> {
  16. protected queue: MinimalQueue;
  17. /**
  18. * The name of the Job
  19. */
  20. name: NameType;
  21. /**
  22. * The payload for this job.
  23. */
  24. data: DataType;
  25. /**
  26. * The options object for this job.
  27. */
  28. opts: JobsOptions;
  29. id?: string;
  30. /**
  31. * It includes the prefix, the namespace separator :, and queue name.
  32. * @see {@link https://www.gnu.org/software/gawk/manual/html_node/Qualified-Names.html}
  33. */
  34. readonly queueQualifiedName: string;
  35. /**
  36. * The progress a job has performed so far.
  37. * @defaultValue 0
  38. */
  39. progress: JobProgress;
  40. /**
  41. * The value returned by the processor when processing this job.
  42. * @defaultValue null
  43. */
  44. returnvalue: ReturnType;
  45. /**
  46. * Stacktrace for the error (for failed jobs).
  47. * @defaultValue null
  48. */
  49. stacktrace: string[];
  50. /**
  51. * An amount of milliseconds to wait until this job can be processed.
  52. * @defaultValue 0
  53. */
  54. delay: number;
  55. /**
  56. * Ranges from 0 (highest priority) to 2 097 152 (lowest priority). Note that
  57. * using priorities has a slight impact on performance,
  58. * so do not use it if not required.
  59. * @defaultValue 0
  60. */
  61. priority: number;
  62. /**
  63. * Timestamp when the job was created (unless overridden with job options).
  64. */
  65. timestamp: number;
  66. /**
  67. * Number of attempts when job is moved to active.
  68. * @defaultValue 0
  69. */
  70. attemptsStarted: number;
  71. /**
  72. * Number of attempts after the job has failed.
  73. * @defaultValue 0
  74. */
  75. attemptsMade: number;
  76. /**
  77. * Number of times where job has stalled.
  78. * @defaultValue 0
  79. */
  80. stalledCounter: number;
  81. /**
  82. * Reason for failing.
  83. */
  84. failedReason: string;
  85. /**
  86. * Deferred failure. Stores a failed message and marks this job to be failed directly
  87. * as soon as the job is picked up by a worker, and using this string as the failed reason.
  88. */
  89. deferredFailure: string;
  90. /**
  91. * Timestamp for when the job finished (completed or failed).
  92. */
  93. finishedOn?: number;
  94. /**
  95. * Timestamp for when the job was processed.
  96. */
  97. processedOn?: number;
  98. /**
  99. * Fully qualified key (including the queue prefix) pointing to the parent of this job.
  100. */
  101. parentKey?: string;
  102. /**
  103. * Object that contains parentId (id) and parent queueKey.
  104. */
  105. parent?: ParentKeys;
  106. /**
  107. * Debounce identifier.
  108. * @deprecated use deduplicationId
  109. */
  110. debounceId?: string;
  111. /**
  112. * Deduplication identifier.
  113. */
  114. deduplicationId?: string;
  115. /**
  116. * Base repeat job key.
  117. */
  118. repeatJobKey?: string;
  119. /**
  120. * Produced next repetable job Id.
  121. *
  122. */
  123. nextRepeatableJobId?: string;
  124. /**
  125. * The token used for locking this job.
  126. */
  127. token?: string;
  128. /**
  129. * The worker name that is processing or processed this job.
  130. */
  131. processedBy?: string;
  132. protected toKey: (type: string) => string;
  133. /**
  134. * @deprecated use UnrecoverableError
  135. */
  136. protected discarded: boolean;
  137. protected scripts: Scripts;
  138. constructor(queue: MinimalQueue,
  139. /**
  140. * The name of the Job
  141. */
  142. name: NameType,
  143. /**
  144. * The payload for this job.
  145. */
  146. data: DataType,
  147. /**
  148. * The options object for this job.
  149. */
  150. opts?: JobsOptions, id?: string);
  151. /**
  152. * Creates a new job and adds it to the queue.
  153. *
  154. * @param queue - the queue where to add the job.
  155. * @param name - the name of the job.
  156. * @param data - the payload of the job.
  157. * @param opts - the options bag for this job.
  158. * @returns The created Job instance
  159. */
  160. static create<T = any, R = any, N extends string = string>(queue: MinimalQueue, name: N, data: T, opts?: JobsOptions): Promise<Job<T, R, N>>;
  161. /**
  162. * Creates a bulk of jobs and adds them atomically to the given queue.
  163. *
  164. * @param queue - the queue where to add the jobs.
  165. * @param jobs - an array of jobs to be added to the queue.
  166. * @returns The created Job instances
  167. */
  168. static createBulk<T = any, R = any, N extends string = string>(queue: MinimalQueue, jobs: {
  169. name: N;
  170. data: T;
  171. opts?: BulkJobOptions;
  172. }[]): Promise<Job<T, R, N>[]>;
  173. /**
  174. * Instantiates a Job from a JobJsonRaw object (coming from a deserialized JSON object)
  175. *
  176. * @param queue - the queue where the job belongs to.
  177. * @param json - the plain object containing the job.
  178. * @param jobId - an optional job id (overrides the id coming from the JSON object)
  179. * @returns A Job instance reconstructed from the JSON data
  180. */
  181. static fromJSON<T = any, R = any, N extends string = string>(queue: MinimalQueue, json: JobJsonRaw, jobId?: string): Job<T, R, N>;
  182. protected createScripts(): void;
  183. static optsFromJSON(rawOpts?: string, optsDecode?: Record<string, string>): JobsOptions;
  184. /**
  185. * Fetches a Job from the queue given the passed job id.
  186. *
  187. * @param queue - the queue where the job belongs to.
  188. * @param jobId - the job id.
  189. * @returns
  190. */
  191. static fromId<T = any, R = any, N extends string = string>(queue: MinimalQueue, jobId: string): Promise<Job<T, R, N> | undefined>;
  192. /**
  193. * addJobLog
  194. *
  195. * @param queue - A minimal queue instance
  196. * @param jobId - Job id
  197. * @param logRow - String with a row of log data to be logged
  198. * @param keepLogs - The optional amount of log entries to preserve
  199. *
  200. * @returns The total number of log entries for this job so far.
  201. */
  202. static addJobLog(queue: MinimalQueue, jobId: string, logRow: string, keepLogs?: number): Promise<number>;
  203. toJSON(): Omit<this, "queue" | "scripts" | "toJSON" | "asJSON" | "asJSONSandbox" | "updateData" | "updateProgress" | "log" | "removeChildDependency" | "clearLogs" | "remove" | "removeUnprocessedChildren" | "extendLock" | "moveToCompleted" | "moveToWait" | "moveToFailed" | "isCompleted" | "isFailed" | "isDelayed" | "isWaitingChildren" | "isActive" | "isWaiting" | "queueName" | "prefix" | "getState" | "changeDelay" | "changePriority" | "getChildrenValues" | "getIgnoredChildrenFailures" | "getFailedChildrenValues" | "getDependencies" | "getDependenciesCount" | "waitUntilFinished" | "moveToDelayed" | "moveToWaitingChildren" | "promote" | "retry" | "discard" | "addJob" | "removeDeduplicationKey">;
  204. /**
  205. * Prepares a job to be serialized for storage in Redis.
  206. * @returns
  207. */
  208. asJSON(): JobJson;
  209. static optsAsJSON(opts?: JobsOptions, optsEncode?: Record<string, string>): RedisJobOptions;
  210. /**
  211. * Prepares a job to be passed to Sandbox.
  212. * @returns
  213. */
  214. asJSONSandbox(): JobJsonSandbox;
  215. /**
  216. * Updates a job's data
  217. *
  218. * @param data - the data that will replace the current jobs data.
  219. */
  220. updateData(data: DataType): Promise<void>;
  221. /**
  222. * Updates a job's progress
  223. *
  224. * @param progress - number or object to be saved as progress.
  225. */
  226. updateProgress(progress: JobProgress): Promise<void>;
  227. /**
  228. * Logs one row of log data.
  229. *
  230. * @param logRow - string with log data to be logged.
  231. * @returns The total number of log entries for this job so far.
  232. */
  233. log(logRow: string): Promise<number>;
  234. /**
  235. * Removes child dependency from parent when child is not yet finished
  236. *
  237. * @returns True if the relationship existed and if it was removed.
  238. */
  239. removeChildDependency(): Promise<boolean>;
  240. /**
  241. * Clears job's logs
  242. *
  243. * @param keepLogs - the amount of log entries to preserve
  244. */
  245. clearLogs(keepLogs?: number): Promise<void>;
  246. /**
  247. * Completely remove the job from the queue.
  248. * Note, this call will throw an exception if the job
  249. * is being processed when the call is performed.
  250. *
  251. * @param opts - Options to remove a job
  252. */
  253. remove({ removeChildren }?: {
  254. removeChildren?: boolean;
  255. }): Promise<void>;
  256. /**
  257. * Remove all children from this job that are not yet processed,
  258. * in other words that are in any other state than completed, failed or active.
  259. *
  260. * @remarks
  261. * - Jobs with locks (most likely active) are ignored.
  262. * - This method can be slow if the number of children is large (\> 1000).
  263. */
  264. removeUnprocessedChildren(): Promise<void>;
  265. /**
  266. * Extend the lock for this job.
  267. *
  268. * @param token - unique token for the lock
  269. * @param duration - lock duration in milliseconds
  270. */
  271. extendLock(token: string, duration: number): Promise<number>;
  272. /**
  273. * Moves a job to the completed queue.
  274. * Returned job to be used with Queue.prototype.nextJobFromJobData.
  275. *
  276. * @param returnValue - The jobs success message.
  277. * @param token - Worker token used to acquire completed job.
  278. * @param fetchNext - True when wanting to fetch the next job.
  279. * @returns Returns the jobData of the next job in the waiting queue or void.
  280. */
  281. moveToCompleted(returnValue: ReturnType, token: string, fetchNext?: boolean): Promise<void | any[]>;
  282. /**
  283. * Moves a job to the wait or prioritized state.
  284. *
  285. * @param token - Worker token used to acquire completed job.
  286. * @returns Returns pttl.
  287. */
  288. moveToWait(token?: string): Promise<number>;
  289. private shouldRetryJob;
  290. /**
  291. * Moves a job to the failed queue.
  292. *
  293. * @param err - the jobs error message.
  294. * @param token - token to check job is locked by current worker
  295. * @param fetchNext - true when wanting to fetch the next job
  296. * @returns Returns the jobData of the next job in the waiting queue or void.
  297. */
  298. moveToFailed<E extends Error>(err: E, token: string, fetchNext?: boolean): Promise<void | any[]>;
  299. private getSpanOperation;
  300. /**
  301. * Records job metrics if a meter is configured in telemetry options.
  302. *
  303. * @param status - The job status
  304. */
  305. private recordJobMetrics;
  306. /**
  307. * @returns true if the job has completed.
  308. */
  309. isCompleted(): Promise<boolean>;
  310. /**
  311. * @returns true if the job has failed.
  312. */
  313. isFailed(): Promise<boolean>;
  314. /**
  315. * @returns true if the job is delayed.
  316. */
  317. isDelayed(): Promise<boolean>;
  318. /**
  319. * @returns true if the job is waiting for children.
  320. */
  321. isWaitingChildren(): Promise<boolean>;
  322. /**
  323. * @returns true of the job is active.
  324. */
  325. isActive(): Promise<boolean>;
  326. /**
  327. * @returns true if the job is waiting.
  328. */
  329. isWaiting(): Promise<boolean>;
  330. /**
  331. * @returns the queue name this job belongs to.
  332. */
  333. get queueName(): string;
  334. /**
  335. * @returns the prefix that is used.
  336. */
  337. get prefix(): string;
  338. /**
  339. * Get current state.
  340. *
  341. * @returns Returns one of these values:
  342. * 'completed', 'failed', 'delayed', 'active', 'waiting', 'waiting-children', 'unknown'.
  343. */
  344. getState(): Promise<JobState | 'unknown'>;
  345. /**
  346. * Change delay of a delayed job.
  347. *
  348. * Reschedules a delayed job by setting a new delay from the current time.
  349. * For example, calling changeDelay(5000) will reschedule the job to execute
  350. * 5000 milliseconds (5 seconds) from now, regardless of the original delay.
  351. *
  352. * @param delay - milliseconds from now when the job should be processed.
  353. * @returns void
  354. * @throws JobNotExist
  355. * This exception is thrown if jobId is missing.
  356. * @throws JobNotInState
  357. * This exception is thrown if job is not in delayed state.
  358. */
  359. changeDelay(delay: number): Promise<void>;
  360. /**
  361. * Change job priority.
  362. *
  363. * @param opts - options containing priority and lifo values.
  364. * @returns void
  365. */
  366. changePriority(opts: {
  367. priority?: number;
  368. lifo?: boolean;
  369. }): Promise<void>;
  370. /**
  371. * Get this jobs children result values if any.
  372. *
  373. * @returns Object mapping children job keys with their values.
  374. */
  375. getChildrenValues<CT = any>(): Promise<{
  376. [jobKey: string]: CT;
  377. }>;
  378. /**
  379. * Retrieves the failures of child jobs that were explicitly ignored while using ignoreDependencyOnFailure option.
  380. * This method is useful for inspecting which child jobs were intentionally ignored when an error occurred.
  381. * @see {@link https://docs.bullmq.io/guide/flows/ignore-dependency}
  382. *
  383. * @returns Object mapping children job keys with their failure values.
  384. */
  385. getIgnoredChildrenFailures(): Promise<{
  386. [jobKey: string]: string;
  387. }>;
  388. /**
  389. * Get job's children failure values that were ignored if any.
  390. *
  391. * @deprecated This method is deprecated and will be removed in v6. Use getIgnoredChildrenFailures instead.
  392. *
  393. * @returns Object mapping children job keys with their failure values.
  394. */
  395. getFailedChildrenValues(): Promise<{
  396. [jobKey: string]: string;
  397. }>;
  398. /**
  399. * Get children job keys if this job is a parent and has children.
  400. * @remarks
  401. * Count options before Redis v7.2 works as expected with any quantity of entries
  402. * on processed/unprocessed dependencies, since v7.2 you must consider that count
  403. * won't have any effect until processed/unprocessed dependencies have a length
  404. * greater than 127
  405. * @see {@link https://redis.io/docs/management/optimization/memory-optimization/#redis--72}
  406. * @see {@link https://docs.bullmq.io/guide/flows#getters}
  407. * @returns dependencies separated by processed, unprocessed, ignored and failed.
  408. */
  409. getDependencies(opts?: DependenciesOpts): Promise<{
  410. nextFailedCursor?: number;
  411. failed?: string[];
  412. nextIgnoredCursor?: number;
  413. ignored?: Record<string, any>;
  414. nextProcessedCursor?: number;
  415. processed?: Record<string, any>;
  416. nextUnprocessedCursor?: number;
  417. unprocessed?: string[];
  418. }>;
  419. /**
  420. * Get children job counts if this job is a parent and has children.
  421. *
  422. * @returns dependencies count separated by processed, unprocessed, ignored and failed.
  423. */
  424. getDependenciesCount(opts?: {
  425. failed?: boolean;
  426. ignored?: boolean;
  427. processed?: boolean;
  428. unprocessed?: boolean;
  429. }): Promise<{
  430. failed?: number;
  431. ignored?: number;
  432. processed?: number;
  433. unprocessed?: number;
  434. }>;
  435. /**
  436. * Returns a promise the resolves when the job has completed (containing the return value of the job),
  437. * or rejects when the job has failed (containing the failedReason).
  438. *
  439. * @param queueEvents - Instance of QueueEvents.
  440. * @param ttl - Time in milliseconds to wait for job to finish before timing out.
  441. */
  442. waitUntilFinished(queueEvents: QueueEvents, ttl?: number): Promise<ReturnType>;
  443. /**
  444. * Moves the job to the delay set.
  445. *
  446. * @param timestamp - timestamp when the job should be moved back to "wait"
  447. * @param token - token to check job is locked by current worker
  448. * @returns
  449. */
  450. moveToDelayed(timestamp: number, token?: string): Promise<void>;
  451. /**
  452. * Moves the job to the waiting-children set.
  453. *
  454. * @param token - Token to check job is locked by current worker
  455. * @param opts - The options bag for moving a job to waiting-children.
  456. * @returns true if the job was moved
  457. */
  458. moveToWaitingChildren(token: string, opts?: MoveToWaitingChildrenOpts): Promise<boolean>;
  459. /**
  460. * Promotes a delayed job so that it starts to be processed as soon as possible.
  461. */
  462. promote(): Promise<void>;
  463. /**
  464. * Attempts to retry the job. Only a job that has failed or completed can be retried.
  465. *
  466. * @param state - completed / failed
  467. * @param opts - options to retry a job
  468. * @returns A promise that resolves when the job has been successfully moved to the wait queue.
  469. * The queue emits a waiting event when the job is successfully moved.
  470. * @throws Will throw an error if the job does not exist, is locked, or is not in the expected state.
  471. */
  472. retry(state?: FinishedStatus, opts?: RetryOptions): Promise<void>;
  473. /**
  474. * Marks a job to not be retried if it fails (even if attempts has been configured)
  475. * @deprecated use UnrecoverableError
  476. */
  477. discard(): void;
  478. private isInZSet;
  479. private isInList;
  480. /**
  481. * Adds the job to Redis.
  482. *
  483. * @param client - The Redis client to use for adding the job.
  484. * @param parentOpts - Options for the parent-child relationship.
  485. * @returns The job ID
  486. */
  487. addJob(client: RedisClient, parentOpts?: ParentKeyOpts): Promise<string>;
  488. /**
  489. * Removes a deduplication key if job is still the cause of deduplication.
  490. * @returns true if the deduplication key was removed.
  491. */
  492. removeDeduplicationKey(): Promise<boolean>;
  493. protected validateOptions(jobData: JobJson): void;
  494. protected updateStacktrace(err: Error): void;
  495. private setSpanJobAttributes;
  496. }