UNPKG

35.1 kBTypeScriptView Raw
1// Type definitions for bull 3.15
2// Project: https://github.com/OptimalBits/bull
3// Definitions by: Bruno Grieder <https://github.com/bgrieder>
4// Cameron Crothers <https://github.com/JProgrammer>
5// Marshall Cottrell <https://github.com/marshall007>
6// Weeco <https://github.com/weeco>
7// Oleg Repin <https://github.com/iamolegga>
8// David Koblas <https://github.com/koblas>
9// Bond Akinmade <https://github.com/bondz>
10// Wuha Team <https://github.com/wuha-team>
11// Alec Brunelle <https://github.com/aleccool213>
12// Dan Manastireanu <https://github.com/danmana>
13// Kjell-Morten Bratsberg Thorsen <https://github.com/kjellmorten>
14// Christian D. <https://github.com/pc-jedi>
15// Silas Rech <https://github.com/lenovouser>
16// DoYoung Ha <https://github.com/hados99>
17// Borys Kupar <https://github.com/borys-kupar>
18// Remko Klein <https://github.com/remko79>
19// Levi Bostian <https://github.com/levibostian>
20// Todd Dukart <https://github.com/tdukart>
21// Mix <https://github.com/mnixry>
22// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
23// TypeScript Version: 2.8
24
25import * as Redis from 'ioredis';
26import { EventEmitter } from 'events';
27
28/**
29 * This is the Queue constructor.
30 * It creates a new Queue that is persisted in Redis.
31 * Everytime the same queue is instantiated it tries to process all the old jobs that may exist from a previous unfinished session.
32 */
33declare const Bull: {
34 /* tslint:disable:no-unnecessary-generics unified-signatures */
35 <T = any>(queueName: string, opts?: Bull.QueueOptions): Bull.Queue<T>;
36 <T = any>(
37 queueName: string,
38 url: string,
39 opts?: Bull.QueueOptions
40 ): Bull.Queue<T>;
41 new <T = any>(queueName: string, opts?: Bull.QueueOptions): Bull.Queue<T>;
42 new <T = any>(
43 queueName: string,
44 url: string,
45 opts?: Bull.QueueOptions
46 ): Bull.Queue<T>;
47 /* tslint:enable:no-unnecessary-generics unified-signatures */
48};
49
50declare namespace Bull {
51 interface RateLimiter {
52 /** Max numbers of jobs processed */
53 max: number;
54 /** Per duration in milliseconds */
55 duration: number;
56 /** When jobs get rate limited, they stay in the waiting queue and are not moved to the delayed queue */
57 bounceBack?: boolean | undefined;
58 /** Groups jobs with the specified key from the data object passed to the Queue#add ex. "network.handle" */
59 groupKey?: string | undefined;
60 }
61
62 interface QueueOptions {
63 /**
64 * Options passed into the `ioredis` constructor's `options` parameter.
65 * `connectionName` is overwritten with `Queue.clientName()`. other properties are copied
66 */
67 redis?: Redis.RedisOptions | string | undefined;
68
69 /**
70 * When specified, the `Queue` will use this function to create new `ioredis` client connections.
71 * This is useful if you want to re-use connections or connect to a Redis cluster.
72 */
73 createClient?(
74 type: 'client' | 'subscriber' | 'bclient',
75 redisOpts?: Redis.RedisOptions
76 ): Redis.Redis | Redis.Cluster;
77
78 /**
79 * Prefix to use for all redis keys
80 */
81 prefix?: string | undefined;
82
83 settings?: AdvancedSettings | undefined;
84
85 limiter?: RateLimiter | undefined;
86
87 defaultJobOptions?: JobOptions | undefined;
88
89 metrics?: MetricsOpts; // Configure metrics
90 }
91
92 interface MetricsOpts {
93 maxDataPoints?: number; // Max number of data points to collect, granularity is fixed at one minute.
94 }
95
96 interface AdvancedSettings {
97 /**
98 * Key expiration time for job locks
99 */
100 lockDuration?: number | undefined;
101
102 /**
103 * Interval in milliseconds on which to acquire the job lock.
104 */
105 lockRenewTime?: number | undefined;
106
107 /**
108 * How often check for stalled jobs (use 0 for never checking)
109 */
110 stalledInterval?: number | undefined;
111
112 /**
113 * Max amount of times a stalled job will be re-processed
114 */
115 maxStalledCount?: number | undefined;
116
117 /**
118 * Poll interval for delayed jobs and added jobs
119 */
120 guardInterval?: number | undefined;
121
122 /**
123 * Delay before processing next job in case of internal error
124 */
125 retryProcessDelay?: number | undefined;
126
127 /**
128 * Define a custom backoff strategy
129 */
130 backoffStrategies?:
131 | {
132 [key: string]: (
133 attemptsMade: number,
134 err: Error,
135 strategyOptions?: any
136 ) => number;
137 }
138 | undefined;
139
140 /**
141 * A timeout for when the queue is in `drained` state (empty waiting for jobs).
142 * It is used when calling `queue.getNextJob()`, which will pass it to `.brpoplpush` on the Redis client.
143 */
144 drainDelay?: number | undefined;
145 }
146
147 type DoneCallback = (error?: Error | null, value?: any) => void;
148
149 type JobId = number | string;
150
151 type ProcessCallbackFunction<T> = (job: Job<T>, done: DoneCallback) => void;
152 type ProcessPromiseFunction<T> = (job: Job<T>) => Promise<void>;
153
154 interface Job<T = any> {
155 id: JobId;
156
157 /**
158 * The custom data passed when the job was created
159 */
160 data: T;
161
162 /**
163 * Options of the job
164 */
165 opts: JobOptions;
166
167 /**
168 * How many attempts where made to run this job
169 */
170 attemptsMade: number;
171
172 /**
173 * When this job was started (unix milliseconds)
174 */
175 processedOn?: number | undefined;
176
177 /**
178 * When this job was completed (unix milliseconds)
179 */
180 finishedOn?: number | undefined;
181
182 /**
183 * Which queue this job was part of
184 */
185 queue: Queue<T>;
186
187 timestamp: number;
188
189 /**
190 * The named processor name
191 */
192 name: string;
193
194 /**
195 * The stacktrace for any errors
196 */
197 stacktrace: string[];
198
199 returnvalue: any;
200
201 failedReason?: string | undefined;
202
203 /**
204 * Get progress on a job
205 */
206 progress(): any;
207
208 /**
209 * Report progress on a job
210 */
211 progress(value: any): Promise<void>;
212
213 /**
214 * Logs one row of log data.
215 *
216 * @param row String with log data to be logged.
217 */
218 log(row: string): Promise<any>;
219
220 /**
221 * Returns a promise resolving to a boolean which, if true, current job's state is completed
222 */
223 isCompleted(): Promise<boolean>;
224
225 /**
226 * Returns a promise resolving to a boolean which, if true, current job's state is failed
227 */
228 isFailed(): Promise<boolean>;
229
230 /**
231 * Returns a promise resolving to a boolean which, if true, current job's state is delayed
232 */
233 isDelayed(): Promise<boolean>;
234
235 /**
236 * Returns a promise resolving to a boolean which, if true, current job's state is active
237 */
238 isActive(): Promise<boolean>;
239
240 /**
241 * Returns a promise resolving to a boolean which, if true, current job's state is wait
242 */
243 isWaiting(): Promise<boolean>;
244
245 /**
246 * Returns a promise resolving to a boolean which, if true, current job's state is paused
247 */
248 isPaused(): Promise<boolean>;
249
250 /**
251 * Returns a promise resolving to a boolean which, if true, current job's state is stuck
252 */
253 isStuck(): Promise<boolean>;
254
255 /**
256 * Returns a promise resolving to the current job's status.
257 * Please take note that the implementation of this method is not very efficient, nor is
258 * it atomic. If your queue does have a very large quantity of jobs, you may want to
259 * avoid using this method.
260 */
261 getState(): Promise<JobStatus | 'stuck'>;
262
263 /**
264 * Update a specific job's data. Promise resolves when the job has been updated.
265 */
266 update(data: T): Promise<void>;
267
268 /**
269 * Removes a job from the queue and from any lists it may be included in.
270 * The returned promise resolves when the job has been removed.
271 */
272 remove(): Promise<void>;
273
274 /**
275 * Re-run a job that has failed. The returned promise resolves when the job
276 * has been scheduled for retry.
277 */
278 retry(): Promise<void>;
279
280 /**
281 * Ensure this job is never ran again even if attemptsMade is less than job.attempts.
282 */
283 discard(): Promise<void>;
284
285 /**
286 * Returns a promise that resolves to the returned data when the job has been finished.
287 * TODO: Add a watchdog to check if the job has finished periodically.
288 * since pubsub does not give any guarantees.
289 */
290 finished(): Promise<any>;
291
292 /**
293 * Moves a job to the `completed` queue. Pulls a job from 'waiting' to 'active'
294 * and returns a tuple containing the next jobs data and id. If no job is in the `waiting` queue, returns null.
295 */
296 moveToCompleted(
297 returnValue?: string,
298 ignoreLock?: boolean,
299 notFetch?: boolean
300 ): Promise<[any, JobId] | null>;
301
302 /**
303 * Moves a job to the `failed` queue. Pulls a job from 'waiting' to 'active'
304 * and returns a tuple containing the next jobs data and id. If no job is in the `waiting` queue, returns null.
305 */
306 moveToFailed(
307 errorInfo: { message: string },
308 ignoreLock?: boolean
309 ): Promise<[any, JobId] | null>;
310
311 /**
312 * Promotes a job that is currently "delayed" to the "waiting" state and executed as soon as possible.
313 */
314 promote(): Promise<void>;
315
316 /**
317 * The lock id of the job
318 */
319 lockKey(): string;
320
321 /**
322 * Releases the lock on the job. Only locks owned by the queue instance can be released.
323 */
324 releaseLock(): Promise<void>;
325
326 /**
327 * Takes a lock for this job so that no other queue worker can process it at the same time.
328 */
329 takeLock(): Promise<number | false>;
330
331 /**
332 * Get job properties as Json Object
333 */
334 toJSON(): {
335 id: JobId;
336 name: string;
337 data: T;
338 opts: JobOptions;
339 progress: number;
340 delay: number;
341 timestamp: number;
342 attemptsMade: number;
343 failedReason: any;
344 stacktrace: string[] | null;
345 returnvalue: any;
346 finishedOn: number | null;
347 processedOn: number | null;
348 };
349 }
350
351 type JobStatus =
352 | 'completed'
353 | 'waiting'
354 | 'active'
355 | 'delayed'
356 | 'failed'
357 | 'paused';
358 type JobStatusClean =
359 | 'completed'
360 | 'wait'
361 | 'active'
362 | 'delayed'
363 | 'failed'
364 | 'paused';
365
366 interface BackoffOptions {
367 /**
368 * Backoff type, which can be either `fixed` or `exponential`
369 */
370 type: string;
371
372 /**
373 * Backoff delay, in milliseconds
374 */
375 delay?: number | undefined;
376
377 /**
378 * Options for custom strategies
379 */
380 options?: any;
381 }
382
383 interface RepeatOptions {
384 /**
385 * Timezone
386 */
387 tz?: string | undefined;
388
389 /**
390 * End date when the repeat job should stop repeating
391 */
392 endDate?: Date | string | number | undefined;
393
394 /**
395 * Number of times the job should repeat at max.
396 */
397 limit?: number | undefined;
398
399 /**
400 * The start value for the repeat iteration count.
401 */
402 count?: number | undefined;
403 }
404
405 interface CronRepeatOptions extends RepeatOptions {
406 /**
407 * Cron pattern specifying when the job should execute
408 */
409 cron: string;
410
411 /**
412 * Start date when the repeat job should start repeating (only with cron).
413 */
414 startDate?: Date | string | number | undefined;
415 }
416
417 interface EveryRepeatOptions extends RepeatOptions {
418 /**
419 * Repeat every millis (cron setting cannot be used together with this setting.)
420 */
421 every: number;
422 }
423
424 interface JobOptions {
425 /**
426 * Optional priority value. ranges from 1 (highest priority) to MAX_INT (lowest priority).
427 * Note that using priorities has a slight impact on performance, so do not use it if not required
428 */
429 priority?: number | undefined;
430
431 /**
432 * An amount of miliseconds to wait until this job can be processed.
433 * Note that for accurate delays, both server and clients should have their clocks synchronized. [optional]
434 */
435 delay?: number | undefined;
436
437 /**
438 * The total number of attempts to try the job until it completes
439 */
440 attempts?: number | undefined;
441
442 /**
443 * Repeat job according to a cron specification
444 */
445 repeat?:
446 | ((CronRepeatOptions | EveryRepeatOptions) & {
447 /**
448 * The key for the repeatable job metadata in Redis.
449 */
450 readonly key: string;
451 })
452 | undefined;
453
454 /**
455 * Backoff setting for automatic retries if the job fails
456 */
457 backoff?: number | BackoffOptions | undefined;
458
459 /**
460 * A boolean which, if true, adds the job to the right
461 * of the queue instead of the left (default false)
462 */
463 lifo?: boolean | undefined;
464
465 /**
466 * The number of milliseconds after which the job should be fail with a timeout error
467 */
468 timeout?: number | undefined;
469
470 /**
471 * Override the job ID - by default, the job ID is a unique
472 * integer, but you can use this setting to override it.
473 * If you use this option, it is up to you to ensure the
474 * jobId is unique. If you attempt to add a job with an id that
475 * already exists, it will not be added.
476 */
477 jobId?: JobId | undefined;
478
479 /**
480 * A boolean which, if true, removes the job when it successfully completes.
481 * When a number, it specifies the amount of jobs to keep.
482 * Default behavior is to keep the job in the completed set.
483 * See KeepJobsOptions if using that interface instead.
484 */
485 removeOnComplete?: boolean | number | KeepJobsOptions | undefined;
486
487 /**
488 * A boolean which, if true, removes the job when it fails after all attempts.
489 * When a number, it specifies the amount of jobs to keep.
490 * Default behavior is to keep the job in the failed set.
491 * See KeepJobsOptions if using that interface instead.
492 */
493 removeOnFail?: boolean | number | KeepJobsOptions | undefined;
494
495 /**
496 * Limits the amount of stack trace lines that will be recorded in the stacktrace.
497 */
498 stackTraceLimit?: number | undefined;
499
500 /**
501 * Prevents JSON data from being parsed.
502 */
503 preventParsingData?: boolean | undefined;
504 }
505
506 /**
507 * Specify which jobs to keep after finishing processing this job.
508 * If both age and count are specified, then the jobs kept will be the ones that satisfies both properties.
509 */
510 interface KeepJobsOptions {
511 /**
512 * Maximum age in *seconds* for job to be kept.
513 */
514 age?: number | undefined;
515
516 /**
517 * Maximum count of jobs to be kept.
518 */
519 count?: number | undefined;
520 }
521
522 interface JobCounts {
523 active: number;
524 completed: number;
525 failed: number;
526 delayed: number;
527 waiting: number;
528 }
529
530 interface JobInformation {
531 key: string;
532 name: string;
533 id?: string | undefined;
534 endDate?: number | undefined;
535 tz?: string | undefined;
536 cron: string;
537 every: number;
538 next: number;
539 }
540
541 interface Queue<T = any> extends EventEmitter {
542 /**
543 * The name of the queue
544 */
545 name: string;
546
547 /**
548 * Queue client (used to add jobs, pause queues, etc);
549 */
550 client: Redis.Redis;
551
552 /**
553 * Returns a promise that resolves when Redis is connected and the queue is ready to accept jobs.
554 * This replaces the `ready` event emitted on Queue in previous verisons.
555 */
556 isReady(): Promise<this>;
557
558 /* tslint:disable:unified-signatures */
559
560 /**
561 * Defines a processing function for the jobs placed into a given Queue.
562 *
563 * The callback is called everytime a job is placed in the queue.
564 * It is passed an instance of the job as first argument.
565 *
566 * If the callback signature contains the second optional done argument,
567 * the callback will be passed a done callback to be called after the job has been completed.
568 * The done callback can be called with an Error instance, to signal that the job did not complete successfully,
569 * or with a result as second argument (e.g.: done(null, result);) when the job is successful.
570 * Errors will be passed as a second argument to the "failed" event; results, as a second argument to the "completed" event.
571 *
572 * If, however, the callback signature does not contain the done argument,
573 * a promise must be returned to signal job completion.
574 * If the promise is rejected, the error will be passed as a second argument to the "failed" event.
575 * If it is resolved, its value will be the "completed" event's second argument.
576 */
577 process(callback: ProcessCallbackFunction<T>): Promise<void>;
578 process(callback: ProcessPromiseFunction<T>): Promise<void>;
579 process(callback: string): Promise<void>;
580
581 /**
582 * Defines a processing function for the jobs placed into a given Queue.
583 *
584 * The callback is called everytime a job is placed in the queue.
585 * It is passed an instance of the job as first argument.
586 *
587 * If the callback signature contains the second optional done argument,
588 * the callback will be passed a done callback to be called after the job has been completed.
589 * The done callback can be called with an Error instance, to signal that the job did not complete successfully,
590 * or with a result as second argument (e.g.: done(null, result);) when the job is successful.
591 * Errors will be passed as a second argument to the "failed" event; results, as a second argument to the "completed" event.
592 *
593 * If, however, the callback signature does not contain the done argument,
594 * a promise must be returned to signal job completion.
595 * If the promise is rejected, the error will be passed as a second argument to the "failed" event.
596 * If it is resolved, its value will be the "completed" event's second argument.
597 *
598 * @param concurrency Bull will then call your handler in parallel respecting this maximum value.
599 */
600 process(
601 concurrency: number,
602 callback: ProcessCallbackFunction<T>
603 ): Promise<void>;
604 process(
605 concurrency: number,
606 callback: ProcessPromiseFunction<T>
607 ): Promise<void>;
608 process(concurrency: number, callback: string): Promise<void>;
609
610 /**
611 * Defines a processing function for the jobs placed into a given Queue.
612 *
613 * The callback is called everytime a job is placed in the queue.
614 * It is passed an instance of the job as first argument.
615 *
616 * If the callback signature contains the second optional done argument,
617 * the callback will be passed a done callback to be called after the job has been completed.
618 * The done callback can be called with an Error instance, to signal that the job did not complete successfully,
619 * or with a result as second argument (e.g.: done(null, result);) when the job is successful.
620 * Errors will be passed as a second argument to the "failed" event; results, as a second argument to the "completed" event.
621 *
622 * If, however, the callback signature does not contain the done argument,
623 * a promise must be returned to signal job completion.
624 * If the promise is rejected, the error will be passed as a second argument to the "failed" event.
625 * If it is resolved, its value will be the "completed" event's second argument.
626 *
627 * @param name Bull will only call the handler if the job name matches
628 */
629 process(name: string, callback: ProcessCallbackFunction<T>): Promise<void>;
630 process(name: string, callback: ProcessPromiseFunction<T>): Promise<void>;
631 process(name: string, callback: string): Promise<void>;
632
633 /**
634 * Defines a processing function for the jobs placed into a given Queue.
635 *
636 * The callback is called everytime a job is placed in the queue.
637 * It is passed an instance of the job as first argument.
638 *
639 * If the callback signature contains the second optional done argument,
640 * the callback will be passed a done callback to be called after the job has been completed.
641 * The done callback can be called with an Error instance, to signal that the job did not complete successfully,
642 * or with a result as second argument (e.g.: done(null, result);) when the job is successful.
643 * Errors will be passed as a second argument to the "failed" event; results, as a second argument to the "completed" event.
644 *
645 * If, however, the callback signature does not contain the done argument,
646 * a promise must be returned to signal job completion.
647 * If the promise is rejected, the error will be passed as a second argument to the "failed" event.
648 * If it is resolved, its value will be the "completed" event's second argument.
649 *
650 * @param name Bull will only call the handler if the job name matches
651 * @param concurrency Bull will then call your handler in parallel respecting this maximum value.
652 */
653 process(
654 name: string,
655 concurrency: number,
656 callback: ProcessCallbackFunction<T>
657 ): Promise<void>;
658 process(
659 name: string,
660 concurrency: number,
661 callback: ProcessPromiseFunction<T>
662 ): Promise<void>;
663 process(name: string, concurrency: number, callback: string): Promise<void>;
664
665 /* tslint:enable:unified-signatures */
666
667 /**
668 * Creates a new job and adds it to the queue.
669 * If the queue is empty the job will be executed directly,
670 * otherwise it will be placed in the queue and executed as soon as possible.
671 */
672 add(data: T, opts?: JobOptions): Promise<Job<T>>;
673
674 /**
675 * Creates a new named job and adds it to the queue.
676 * If the queue is empty the job will be executed directly,
677 * otherwise it will be placed in the queue and executed as soon as possible.
678 */
679 add(name: string, data: T, opts?: JobOptions): Promise<Job<T>>;
680
681 /**
682 * Adds an array of jobs to the queue.
683 * If the queue is empty the jobs will be executed directly,
684 * otherwise they will be placed in the queue and executed as soon as possible.
685 * 'repeat' option is not supported in addBulk https://github.com/OptimalBits/bull/issues/1731
686 */
687 addBulk(
688 jobs: Array<{
689 name?: string | undefined;
690 data: T;
691 opts?: Omit<JobOptions, 'repeat'> | undefined;
692 }>
693 ): Promise<Array<Job<T>>>;
694
695 /**
696 * Returns a promise that resolves when the queue is paused.
697 *
698 * A paused queue will not process new jobs until resumed, but current jobs being processed will continue until
699 * they are finalized. The pause can be either global or local. If global, all workers in all queue instances
700 * for a given queue will be paused. If local, just this worker will stop processing new jobs after the current
701 * lock expires. This can be useful to stop a worker from taking new jobs prior to shutting down.
702 *
703 * If doNotWaitActive is true, pause will not wait for any active jobs to finish before resolving. Otherwise, pause
704 * will wait for active jobs to finish. See Queue#whenCurrentJobsFinished for more information.
705 *
706 * Pausing a queue that is already paused does nothing.
707 */
708 pause(isLocal?: boolean, doNotWaitActive?: boolean): Promise<void>;
709
710 /**
711 * Returns a promise that resolves when the queue is resumed after being paused.
712 *
713 * The resume can be either local or global. If global, all workers in all queue instances for a given queue
714 * will be resumed. If local, only this worker will be resumed. Note that resuming a queue globally will not
715 * resume workers that have been paused locally; for those, resume(true) must be called directly on their
716 * instances.
717 *
718 * Resuming a queue that is not paused does nothing.
719 */
720 resume(isLocal?: boolean): Promise<void>;
721
722 /**
723 * Returns a promise that resolves with a boolean if queue is paused
724 */
725 isPaused(isLocal?: boolean): Promise<boolean>;
726
727 /**
728 * Returns a promise that returns the number of jobs in the queue, waiting or paused.
729 * Since there may be other processes adding or processing jobs, this value may be true only for a very small amount of time.
730 */
731 count(): Promise<number>;
732
733 /**
734 * Empties a queue deleting all the input lists and associated jobs.
735 */
736 empty(): Promise<void>;
737
738 /**
739 * Closes the underlying redis client. Use this to perform a graceful shutdown.
740 *
741 * `close` can be called from anywhere, with one caveat:
742 * if called from within a job handler the queue won't close until after the job has been processed
743 */
744 close(doNotWaitJobs?: boolean): Promise<void>;
745
746 /**
747 * Returns a promise that will return the job instance associated with the jobId parameter.
748 * If the specified job cannot be located, the promise callback parameter will be set to null.
749 */
750 getJob(jobId: JobId): Promise<Job<T> | null>;
751
752 /**
753 * Returns a promise that will return an array with the waiting jobs between start and end.
754 */
755 getWaiting(start?: number, end?: number): Promise<Array<Job<T>>>;
756
757 /**
758 * Returns a promise that will return an array with the active jobs between start and end.
759 */
760 getActive(start?: number, end?: number): Promise<Array<Job<T>>>;
761
762 /**
763 * Returns a promise that will return an array with the delayed jobs between start and end.
764 */
765 getDelayed(start?: number, end?: number): Promise<Array<Job<T>>>;
766
767 /**
768 * Returns a promise that will return an array with the completed jobs between start and end.
769 */
770 getCompleted(start?: number, end?: number): Promise<Array<Job<T>>>;
771
772 /**
773 * Returns a promise that will return an array with the failed jobs between start and end.
774 */
775 getFailed(start?: number, end?: number): Promise<Array<Job<T>>>;
776
777 /**
778 * Returns JobInformation of repeatable jobs (ordered descending). Provide a start and/or an end
779 * index to limit the number of results. Start defaults to 0, end to -1 and asc to false.
780 */
781 getRepeatableJobs(
782 start?: number,
783 end?: number,
784 asc?: boolean
785 ): Promise<JobInformation[]>;
786
787 /**
788 * ???
789 */
790 nextRepeatableJob(
791 name: string,
792 data: any,
793 opts: JobOptions
794 ): Promise<Job<T>>;
795
796 /**
797 * Removes a given repeatable job. The RepeatOptions and JobId needs to be the same as the ones
798 * used for the job when it was added.
799 */
800 removeRepeatable(
801 repeat: (CronRepeatOptions | EveryRepeatOptions) & {
802 jobId?: JobId | undefined;
803 }
804 ): Promise<void>;
805
806 /**
807 * Removes a given repeatable job. The RepeatOptions and JobId needs to be the same as the ones
808 * used for the job when it was added.
809 *
810 * name: The name of the to be removed job
811 */
812 removeRepeatable(
813 name: string,
814 repeat: (CronRepeatOptions | EveryRepeatOptions) & {
815 jobId?: JobId | undefined;
816 }
817 ): Promise<void>;
818
819 /**
820 * Removes a given repeatable job by key.
821 */
822 removeRepeatableByKey(key: string): Promise<void>;
823
824 /**
825 * Returns a promise that will return an array of job instances of the given job statuses.
826 * Optional parameters for range and ordering are provided.
827 */
828 getJobs(
829 types: JobStatus[],
830 start?: number,
831 end?: number,
832 asc?: boolean
833 ): Promise<Array<Job<T>>>;
834
835 /**
836 * Returns a promise that resolves to a Metrics object.
837 */
838 getMetrics(type: 'completed' | 'failed', start?: number, end?: number): Promise<{
839 meta: {
840 count: number;
841 prevTS: number;
842 prevCount: number;
843 };
844 data: number[];
845 count: number;
846 }>
847
848 /**
849 * Returns a promise that resolves to the next job in queue.
850 */
851 getNextJob(): Promise<Job<T> | undefined>;
852
853 /**
854 * Returns a object with the logs according to the start and end arguments. The returned count
855 * value is the total amount of logs, useful for implementing pagination.
856 */
857 getJobLogs(
858 jobId: JobId,
859 start?: number,
860 end?: number
861 ): Promise<{ logs: string[]; count: number }>;
862
863 /**
864 * Returns a promise that resolves with the job counts for the given queue.
865 */
866 getJobCounts(): Promise<JobCounts>;
867
868 /**
869 * Returns a promise that resolves with the job counts for the given queue of the given job statuses.
870 */
871 getJobCountByTypes(types: JobStatus[] | JobStatus): Promise<number>;
872
873 /**
874 * Returns a promise that resolves with the quantity of completed jobs.
875 */
876 getCompletedCount(): Promise<number>;
877
878 /**
879 * Returns a promise that resolves with the quantity of failed jobs.
880 */
881 getFailedCount(): Promise<number>;
882
883 /**
884 * Returns a promise that resolves with the quantity of delayed jobs.
885 */
886 getDelayedCount(): Promise<number>;
887
888 /**
889 * Returns a promise that resolves with the quantity of waiting jobs.
890 */
891 getWaitingCount(): Promise<number>;
892
893 /**
894 * Returns a promise that resolves with the quantity of paused jobs.
895 */
896 getPausedCount(): Promise<number>;
897
898 /**
899 * Returns a promise that resolves with the quantity of active jobs.
900 */
901 getActiveCount(): Promise<number>;
902
903 /**
904 * Returns a promise that resolves to the quantity of repeatable jobs.
905 */
906 getRepeatableCount(): Promise<number>;
907
908 /**
909 * Tells the queue remove all jobs created outside of a grace period in milliseconds.
910 * You can clean the jobs with the following states: completed, wait (typo for waiting), active, delayed, and failed.
911 * @param grace Grace period in milliseconds.
912 * @param status Status of the job to clean. Values are completed, wait, active, delayed, and failed. Defaults to completed.
913 * @param limit Maximum amount of jobs to clean per call. If not provided will clean all matching jobs.
914 */
915 clean(
916 grace: number,
917 status?: JobStatusClean,
918 limit?: number
919 ): Promise<Array<Job<T>>>;
920
921 /**
922 * Returns a promise that resolves to a Metrics object.
923 * @param type Job metric type either 'completed' or 'failed'
924 * @param start Start point of the metrics, where 0 is the newest point to be returned.
925 * @param end End point of the metrics, where -1 is the oldest point to be returned.
926 * @returns - Returns an object with queue metrics.
927 */
928 getMetrics(
929 type: 'completed' | 'failed',
930 start?: number,
931 end?: number
932 ): Promise<{
933 meta: {
934 count: number;
935 prevTS: number;
936 prevCount: number;
937 };
938 data: number[];
939 count: number;
940 }>;
941
942 /**
943 * Returns a promise that marks the start of a transaction block.
944 */
945 multi(): Redis.Pipeline;
946
947 /**
948 * Returns the queue specific key.
949 */
950 toKey(queueType: string): string;
951
952 /**
953 * Completely destroys the queue and all of its contents irreversibly.
954 * @param ops.force Obliterate the queue even if there are active jobs
955 */
956 obliterate(ops?: { force: boolean }): Promise<void>;
957
958 /**
959 * Listens to queue events
960 */
961 on(event: string, callback: (...args: any[]) => void): this;
962
963 /**
964 * An error occured
965 */
966 on(event: 'error', callback: ErrorEventCallback): this;
967
968 /**
969 * A Job is waiting to be processed as soon as a worker is idling.
970 */
971 on(event: 'waiting', callback: WaitingEventCallback): this;
972
973 /**
974 * A job has started. You can use `jobPromise.cancel()` to abort it
975 */
976 on(event: 'active', callback: ActiveEventCallback<T>): this;
977
978 /**
979 * A job has been marked as stalled.
980 * This is useful for debugging job workers that crash or pause the event loop.
981 */
982 on(event: 'stalled', callback: StalledEventCallback<T>): this;
983
984 /**
985 * A job's progress was updated
986 */
987 on(event: 'progress', callback: ProgressEventCallback<T>): this;
988
989 /**
990 * A job successfully completed with a `result`
991 */
992 on(event: 'completed', callback: CompletedEventCallback<T>): this;
993
994 /**
995 * A job failed with `err` as the reason
996 */
997 on(event: 'failed', callback: FailedEventCallback<T>): this;
998
999 /**
1000 * The queue has been paused
1001 */
1002 on(event: 'paused', callback: EventCallback): this;
1003
1004 /**
1005 * The queue has been resumed
1006 */
1007 on(event: 'resumed', callback: EventCallback): this; // tslint:disable-line unified-signatures
1008
1009 /**
1010 * A job successfully removed.
1011 */
1012 on(event: 'removed', callback: RemovedEventCallback<T>): this;
1013
1014 /**
1015 * Old jobs have been cleaned from the queue.
1016 * `jobs` is an array of jobs that were removed, and `type` is the type of those jobs.
1017 *
1018 * @see Queue#clean() for details
1019 */
1020 on(event: 'cleaned', callback: CleanedEventCallback<T>): this;
1021
1022 /**
1023 * Emitted every time the queue has processed all the waiting jobs
1024 * (even if there can be some delayed jobs not yet processed)
1025 */
1026 on(event: 'drained', callback: EventCallback): this; // tslint:disable-line unified-signatures
1027
1028 /**
1029 * Array of Redis clients the queue uses
1030 */
1031 clients: Redis.Redis[];
1032
1033 /**
1034 * Set clientName to Redis.client
1035 */
1036 setWorkerName(): Promise<any>;
1037
1038 /**
1039 * Returns array of workers that are currently working on this queue.
1040 */
1041 getWorkers(): Promise<{ [index: string]: string }[]>;
1042
1043 /**
1044 * Returns Queue name in base64 encoded format
1045 */
1046 base64Name(): string;
1047
1048 /**
1049 * Returns Queue name with keyPrefix (default: 'bull')
1050 */
1051 clientName(): string;
1052
1053 /**
1054 * Returns Redis clients array which belongs to current Queue from string with all redis clients
1055 *
1056 * @param list String with all redis clients
1057 */
1058 parseClientList(list: string): { [index: string]: string }[][];
1059
1060 /**
1061 * Returns a promise that resolves when active jobs are finished
1062 */
1063 whenCurrentJobsFinished(): Promise<void>;
1064
1065 /**
1066 * Removes all the jobs which jobId matches the given pattern. The pattern must follow redis glob-style pattern
1067 * (syntax)[redis.io/commands/keys]
1068 */
1069 removeJobs(pattern: string): Promise<void>;
1070 }
1071
1072 type EventCallback = () => void;
1073
1074 type ErrorEventCallback = (error: Error) => void;
1075
1076 interface JobPromise {
1077 /**
1078 * Abort this job
1079 */
1080 cancel(): void;
1081 }
1082
1083 type ActiveEventCallback<T = any> = (
1084 job: Job<T>,
1085 jobPromise?: JobPromise
1086 ) => void;
1087
1088 type StalledEventCallback<T = any> = (job: Job<T>) => void;
1089
1090 type ProgressEventCallback<T = any> = (job: Job<T>, progress: any) => void;
1091
1092 type CompletedEventCallback<T = any> = (job: Job<T>, result: any) => void;
1093
1094 type FailedEventCallback<T = any> = (job: Job<T>, error: Error) => void;
1095
1096 type CleanedEventCallback<T = any> = (
1097 jobs: Array<Job<T>>,
1098 status: JobStatusClean
1099 ) => void;
1100
1101 type RemovedEventCallback<T = any> = (job: Job<T>) => void;
1102
1103 type WaitingEventCallback = (jobId: JobId) => void;
1104}
1105
1106export = Bull;