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