scripts.d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * Includes all the scripts needed by the queue and jobs.
  3. */
  4. import { JobJson, JobJsonRaw, MinimalJob, MoveToWaitingChildrenOpts, ParentKeyOpts, RedisClient, MoveToDelayedOpts, RepeatableOptions, RetryJobOpts, RetryOptions, ScriptQueueContext } from '../interfaces';
  5. import { JobsOptions, JobState, JobType, FinishedStatus, FinishedPropValAttribute, KeepJobs, RedisJobOptions, JobProgress } from '../types';
  6. import { ChainableCommander } from 'ioredis';
  7. export type JobData = [JobJsonRaw | number, string?];
  8. export declare class Scripts {
  9. protected queue: ScriptQueueContext;
  10. protected version: string;
  11. moveToFinishedKeys: (string | undefined)[];
  12. constructor(queue: ScriptQueueContext);
  13. execCommand(client: RedisClient | ChainableCommander, commandName: string, args: any[]): any;
  14. isJobInList(listKey: string, jobId: string): Promise<boolean>;
  15. protected addDelayedJobArgs(job: JobJson, encodedOpts: any, args: (string | number | Record<string, any>)[]): (string | Buffer)[];
  16. protected addDelayedJob(client: RedisClient, job: JobJson, encodedOpts: any, args: (string | number | Record<string, any>)[]): Promise<string | number>;
  17. protected addPrioritizedJobArgs(job: JobJson, encodedOpts: any, args: (string | number | Record<string, any>)[]): (string | Buffer)[];
  18. protected addPrioritizedJob(client: RedisClient, job: JobJson, encodedOpts: any, args: (string | number | Record<string, any>)[]): Promise<string | number>;
  19. protected addParentJobArgs(job: JobJson, encodedOpts: any, args: (string | number | Record<string, any>)[]): (string | Buffer)[];
  20. protected addParentJob(client: RedisClient, job: JobJson, encodedOpts: any, args: (string | number | Record<string, any>)[]): Promise<string | number>;
  21. protected addStandardJobArgs(job: JobJson, encodedOpts: any, args: (string | number | Record<string, any>)[]): (string | Buffer)[];
  22. protected addStandardJob(client: RedisClient, job: JobJson, encodedOpts: any, args: (string | number | Record<string, any>)[]): Promise<string | number>;
  23. addJob(client: RedisClient, job: JobJson, opts: RedisJobOptions, jobId: string, parentKeyOpts?: ParentKeyOpts): Promise<string>;
  24. protected pauseArgs(pause: boolean): (string | number)[];
  25. pause(pause: boolean): Promise<void>;
  26. protected addRepeatableJobArgs(customKey: string, nextMillis: number, opts: RepeatableOptions, legacyCustomKey: string): (string | number | Buffer)[];
  27. addRepeatableJob(customKey: string, nextMillis: number, opts: RepeatableOptions, legacyCustomKey: string): Promise<string>;
  28. removeDeduplicationKey(deduplicationId: string, jobId: string): Promise<number>;
  29. addJobScheduler(jobSchedulerId: string, nextMillis: number, templateData: string, templateOpts: RedisJobOptions, opts: RepeatableOptions, delayedJobOpts: JobsOptions, producerId?: string): Promise<[string, number]>;
  30. updateRepeatableJobMillis(client: RedisClient, customKey: string, nextMillis: number, legacyCustomKey: string): Promise<string>;
  31. updateJobSchedulerNextMillis(jobSchedulerId: string, nextMillis: number, templateData: string, delayedJobOpts: JobsOptions, producerId?: string): Promise<string | null>;
  32. private removeRepeatableArgs;
  33. getRepeatConcatOptions(repeatConcatOptions: string, repeatJobKey: string): string;
  34. removeRepeatable(legacyRepeatJobId: string, repeatConcatOptions: string, repeatJobKey: string): Promise<number>;
  35. removeJobScheduler(jobSchedulerId: string): Promise<number>;
  36. protected removeArgs(jobId: string, removeChildren: boolean): (string | number)[];
  37. remove(jobId: string, removeChildren: boolean): Promise<number>;
  38. removeUnprocessedChildren(jobId: string): Promise<void>;
  39. extendLock(jobId: string, token: string, duration: number, client?: RedisClient | ChainableCommander): Promise<number>;
  40. extendLocks(jobIds: string[], tokens: string[], duration: number): Promise<string[]>;
  41. updateData<T = any, R = any, N extends string = string>(job: MinimalJob<T, R, N>, data: T): Promise<void>;
  42. updateProgress(jobId: string, progress: JobProgress): Promise<void>;
  43. addLog(jobId: string, logRow: string, keepLogs?: number): Promise<number>;
  44. protected moveToFinishedArgs<T = any, R = any, N extends string = string>(job: MinimalJob<T, R, N>, val: any, propVal: FinishedPropValAttribute, shouldRemove: undefined | boolean | number | KeepJobs, target: FinishedStatus, token: string, timestamp: number, fetchNext?: boolean, fieldsToUpdate?: Record<string, any>): (string | number | boolean | Buffer)[];
  45. protected getKeepJobs(shouldRemove: undefined | boolean | number | KeepJobs, workerKeepJobs: undefined | KeepJobs): KeepJobs;
  46. moveToFinished(jobId: string, args: (string | number | boolean | Buffer)[]): Promise<any[]>;
  47. private drainArgs;
  48. drain(delayed: boolean): Promise<void>;
  49. private removeChildDependencyArgs;
  50. removeChildDependency(jobId: string, parentKey: string): Promise<boolean>;
  51. private getRangesArgs;
  52. getRanges(types: JobType[], start?: number, end?: number, asc?: boolean): Promise<[string][]>;
  53. private getCountsArgs;
  54. getCounts(types: JobType[]): Promise<number[]>;
  55. protected getCountsPerPriorityArgs(priorities: number[]): (string | number)[];
  56. getCountsPerPriority(priorities: number[]): Promise<number[]>;
  57. protected getDependencyCountsArgs(jobId: string, types: string[]): (string | number)[];
  58. getDependencyCounts(jobId: string, types: string[]): Promise<number[]>;
  59. moveToCompletedArgs<T = any, R = any, N extends string = string>(job: MinimalJob<T, R, N>, returnvalue: R, removeOnComplete: boolean | number | KeepJobs, token: string, fetchNext?: boolean): (string | number | boolean | Buffer)[];
  60. moveToFailedArgs<T = any, R = any, N extends string = string>(job: MinimalJob<T, R, N>, failedReason: string, removeOnFailed: boolean | number | KeepJobs, token: string, fetchNext?: boolean, fieldsToUpdate?: Record<string, any>): (string | number | boolean | Buffer)[];
  61. isFinished(jobId: string, returnValue?: boolean): Promise<number | [number, string]>;
  62. getState(jobId: string): Promise<JobState | 'unknown'>;
  63. /**
  64. * Change delay of a delayed job.
  65. *
  66. * Reschedules a delayed job by setting a new delay from the current time.
  67. * For example, calling changeDelay(5000) will reschedule the job to execute
  68. * 5000 milliseconds (5 seconds) from now, regardless of the original delay.
  69. *
  70. * @param jobId - the ID of the job to change the delay for.
  71. * @param delay - milliseconds from now when the job should be processed.
  72. * @returns delay in milliseconds.
  73. * @throws JobNotExist
  74. * This exception is thrown if jobId is missing.
  75. * @throws JobNotInState
  76. * This exception is thrown if job is not in delayed state.
  77. */
  78. changeDelay(jobId: string, delay: number): Promise<void>;
  79. private changeDelayArgs;
  80. changePriority(jobId: string, priority?: number, lifo?: boolean): Promise<void>;
  81. protected changePriorityArgs(jobId: string, priority?: number, lifo?: boolean): (string | number)[];
  82. moveToDelayedArgs(jobId: string, timestamp: number, token: string, delay: number, opts?: MoveToDelayedOpts): (string | number | Buffer)[];
  83. moveToWaitingChildrenArgs(jobId: string, token: string, opts?: MoveToWaitingChildrenOpts): (string | number)[];
  84. isMaxedArgs(): string[];
  85. isMaxed(): Promise<boolean>;
  86. moveToDelayed(jobId: string, timestamp: number, delay: number, token?: string, opts?: MoveToDelayedOpts): Promise<void | any[]>;
  87. /**
  88. * Move parent job to waiting-children state.
  89. *
  90. * @returns true if job is successfully moved, false if there are pending dependencies.
  91. * @throws JobNotExist
  92. * This exception is thrown if jobId is missing.
  93. * @throws JobLockNotExist
  94. * This exception is thrown if job lock is missing.
  95. * @throws JobNotInState
  96. * This exception is thrown if job is not in active state.
  97. */
  98. moveToWaitingChildren(jobId: string, token: string, opts?: MoveToWaitingChildrenOpts): Promise<boolean>;
  99. getRateLimitTtlArgs(maxJobs?: number): (string | number)[];
  100. getRateLimitTtl(maxJobs?: number): Promise<number>;
  101. /**
  102. * Remove jobs in a specific state.
  103. *
  104. * @returns Id jobs from the deleted records.
  105. */
  106. cleanJobsInSet(set: string, timestamp: number, limit?: number): Promise<string[]>;
  107. getJobSchedulerArgs(id: string): string[];
  108. getJobScheduler(id: string): Promise<[any, string | null]>;
  109. retryJobArgs(jobId: string, lifo: boolean, token: string, opts?: MoveToDelayedOpts): (string | number | Buffer)[];
  110. retryJob(jobId: string, lifo: boolean, token?: string, opts?: RetryJobOpts): Promise<void>;
  111. protected moveJobsToWaitArgs(state: FinishedStatus | 'delayed', count: number, timestamp: number): (string | number)[];
  112. retryJobs(state?: FinishedStatus, count?: number, timestamp?: number): Promise<number>;
  113. promoteJobs(count?: number): Promise<number>;
  114. /**
  115. * Attempts to reprocess a job
  116. *
  117. * @param job - The job to reprocess
  118. * @param state - The expected job state. If the job is not found
  119. * on the provided state, then it's not reprocessed. Supported states: 'failed', 'completed'
  120. *
  121. * @returns A promise that resolves when the job has been successfully moved to the wait queue.
  122. * @throws Will throw an error with a code property indicating the failure reason:
  123. * - code 0: Job does not exist
  124. * - code -1: Job is currently locked and can't be retried
  125. * - code -2: Job was not found in the expected set
  126. */
  127. reprocessJob<T = any, R = any, N extends string = string>(job: MinimalJob<T, R, N>, state: 'failed' | 'completed', opts?: RetryOptions): Promise<void>;
  128. getMetrics(type: 'completed' | 'failed', start?: number, end?: number): Promise<[string[], string[], number]>;
  129. moveToActive(client: RedisClient, token: string, name?: string): Promise<any[]>;
  130. promote(jobId: string): Promise<void>;
  131. protected moveStalledJobsToWaitArgs(): (string | number)[];
  132. /**
  133. * Looks for unlocked jobs in the active queue.
  134. *
  135. * The job was being worked on, but the worker process died and it failed to renew the lock.
  136. * We call these jobs 'stalled'. This is the most common case. We resolve these by moving them
  137. * back to wait to be re-processed. To prevent jobs from cycling endlessly between active and wait,
  138. * (e.g. if the job handler keeps crashing),
  139. * we limit the number stalled job recoveries to settings.maxStalledCount.
  140. */
  141. moveStalledJobsToWait(): Promise<string[]>;
  142. /**
  143. * Moves a job back from Active to Wait.
  144. * This script is used when a job has been manually rate limited and needs
  145. * to be moved back to wait from active status.
  146. *
  147. * @param client - Redis client
  148. * @param jobId - Job id
  149. * @returns
  150. */
  151. moveJobFromActiveToWait(jobId: string, token?: string): Promise<any>;
  152. obliterate(opts: {
  153. force: boolean;
  154. count: number;
  155. }): Promise<number>;
  156. /**
  157. * Paginate a set or hash keys.
  158. * @param opts - options to define the pagination behaviour
  159. *
  160. */
  161. paginate(key: string, opts: {
  162. start: number;
  163. end: number;
  164. fetchJobs?: boolean;
  165. }): Promise<{
  166. cursor: string;
  167. items: {
  168. id: string;
  169. v?: any;
  170. err?: string;
  171. }[];
  172. total: number;
  173. jobs?: JobJsonRaw[];
  174. }>;
  175. finishedErrors({ code, jobId, parentKey, command, state, }: {
  176. code: number;
  177. jobId?: string;
  178. parentKey?: string;
  179. command: string;
  180. state?: string;
  181. }): Error;
  182. removeOrphanedJobs(candidateJobIds: string[], stateKeySuffixes: string[], jobSubKeySuffixes: string[]): Promise<number>;
  183. }
  184. export declare function raw2NextJobData(raw: any[]): any[];