Skip to content

Commit 3ea5ad1

Browse files
danmanasheetalkamat
authored andcommitted
* @types/bull Update to version 3.4.8. Closes DefinitelyTyped#22673 DefinitelyTyped#23965 DefinitelyTyped#18911 * Fix return type for moveTo* methods
1 parent c6fe757 commit 3ea5ad1

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed

types/bull/bull-tests.tsx

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,61 @@ videoQueue.add({ video: 'http://example.com/video1.mov' }, { jobId: 1 })
125125
pdfQueue
126126
.on('error', (err: Error) => undefined)
127127
.on('active', (job: Queue.Job, jobPromise: Queue.JobPromise) => jobPromise.cancel())
128+
.on('waiting', (jobId: Queue.JobId) => undefined)
128129
.on('active', (job: Queue.Job) => undefined)
129130
.on('stalled', (job: Queue.Job) => undefined)
130131
.on('progress', (job: Queue.Job) => undefined)
131132
.on('completed', (job: Queue.Job) => undefined)
132133
.on('failed', (job: Queue.Job) => undefined)
133134
.on('paused', () => undefined)
134135
.on('resumed', () => undefined)
135-
.on('cleaned', (jobs: Queue.Job[], status: Queue.JobStatus) => undefined);
136+
.on('cleaned', (jobs: Queue.Job[], status: Queue.JobStatus) => undefined)
137+
.on('drained', () => undefined)
138+
.on('removed', (job: Queue.Job) => undefined);
139+
140+
// test different process methods
141+
142+
const profileQueue = new Queue('profile');
143+
// Max concurrency for requestProfile is 100
144+
profileQueue.process('requestProfile', 100, () => {});
145+
profileQueue.process(100, () => {});
146+
147+
// other tests
148+
const myQueue = new Queue('myQueue', {
149+
settings: {
150+
drainDelay: 5
151+
},
152+
defaultJobOptions: {
153+
stackTraceLimit: 1,
154+
}
155+
});
156+
157+
myQueue.on('active', (job: Queue.Job) => {
158+
job.moveToCompleted();
159+
job.moveToCompleted('done');
160+
job.moveToCompleted('done', true);
161+
job.moveToCompleted('done', true).then(val => {
162+
if (val) {
163+
const nextJobData: any = val[0];
164+
const nextJobId: Queue.JobId = val[1];
165+
}
166+
});
167+
168+
job.moveToFailed({ message: "Call to external service failed!" }, true);
169+
job.moveToFailed(new Error('test error'), true);
170+
job.moveToFailed(new Error('test error'), true).then(val => {
171+
if (val) {
172+
const nextJobData: any = val[0];
173+
const nextJobId: Queue.JobId = val[1];
174+
}
175+
});
176+
177+
job.discard();
178+
});
179+
180+
// test all constructor options:
181+
182+
new Queue('profile');
183+
new Queue('profile', 'url');
184+
new Queue('profile', { prefix: 'test' });
185+
new Queue('profile', 'url', { prefix: 'test' });

types/bull/index.d.ts

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Type definitions for bull 3.3
1+
// Type definitions for bull 3.4
22
// Project: https://github.com/OptimalBits/bull
33
// Definitions by: Bruno Grieder <https://github.com/bgrieder>
44
// Cameron Crothers <https://github.com/JProgrammer>
@@ -10,6 +10,7 @@
1010
// Bond Akinmade <https://github.com/bondz>
1111
// Wuha Team <https://github.com/wuha-team>
1212
// Alec Brunelle <https://github.com/aleccool213>
13+
// Dan Manastireanu <https://github.com/danmana>
1314
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
1415
// TypeScript Version: 2.8
1516

@@ -23,9 +24,9 @@ import * as Promise from "bluebird";
2324
*/
2425
declare const Bull: {
2526
(queueName: string, opts?: Bull.QueueOptions): Bull.Queue;
26-
(queueName: string, url?: string): Bull.Queue; // tslint:disable-line unified-signatures
27+
(queueName: string, url: string, opts?: Bull.QueueOptions): Bull.Queue; // tslint:disable-line unified-signatures
2728
new (queueName: string, opts?: Bull.QueueOptions): Bull.Queue;
28-
new (queueName: string, url?: string): Bull.Queue; // tslint:disable-line unified-signatures
29+
new (queueName: string, url: string, opts?: Bull.QueueOptions): Bull.Queue; // tslint:disable-line unified-signatures
2930
};
3031

3132
declare namespace Bull {
@@ -92,6 +93,12 @@ declare namespace Bull {
9293
backoffStrategies?: {
9394
[key: string]: (attemptsMade: number, err: typeof Error) => number;
9495
};
96+
97+
/**
98+
* A timeout for when the queue is in `drained` state (empty waiting for jobs).
99+
* It is used when calling `queue.getNextJob()`, which will pass it to `.brpoplpush` on the Redis client.
100+
*/
101+
drainDelay?: number;
95102
}
96103

97104
type DoneCallback = (error?: Error | null, value?: any) => void;
@@ -141,13 +148,30 @@ declare namespace Bull {
141148
*/
142149
retry(): Promise<void>;
143150

151+
/**
152+
* Ensure this job is never ran again even if attemptsMade is less than job.attempts.
153+
*/
154+
discard(): Promise<void>;
155+
144156
/**
145157
* Returns a promise that resolves to the returned data when the job has been finished.
146158
* TODO: Add a watchdog to check if the job has finished periodically.
147159
* since pubsub does not give any guarantees.
148160
*/
149161
finished(): Promise<any>;
150162

163+
/**
164+
* Moves a job to the `completed` queue. Pulls a job from 'waiting' to 'active'
165+
* and returns a tuple containing the next jobs data and id. If no job is in the `waiting` queue, returns null.
166+
*/
167+
moveToCompleted(returnValue?: string, ignoreLock?: boolean): Promise<[any, JobId] | null>;
168+
169+
/**
170+
* Moves a job to the `failed` queue. Pulls a job from 'waiting' to 'active'
171+
* and returns a tuple containing the next jobs data and id. If no job is in the `waiting` queue, returns null.
172+
*/
173+
moveToFailed(errorInfo: { message: string; }, ignoreLock?: boolean): Promise<[any, JobId] | null>;
174+
151175
/**
152176
* Promotes a job that is currently "delayed" to the "waiting" state and executed as soon as possible.
153177
*/
@@ -205,6 +229,11 @@ declare namespace Bull {
205229
* Cron pattern specifying when the job should execute
206230
*/
207231
cron: string;
232+
233+
/**
234+
* Start date when the repeat job should start repeating (only with cron).
235+
*/
236+
startDate?: Date | string | number;
208237
}
209238

210239
interface EveryRepeatOptions extends RepeatOptions {
@@ -273,6 +302,11 @@ declare namespace Bull {
273302
* Default behavior is to keep the job in the completed set.
274303
*/
275304
removeOnFail?: boolean;
305+
306+
/**
307+
* Limits the amount of stack trace lines that will be recorded in the stacktrace.
308+
*/
309+
stackTraceLimit?: number;
276310
}
277311

278312
interface JobCounts {
@@ -618,6 +652,11 @@ declare namespace Bull {
618652
*/
619653
on(event: 'error', callback: ErrorEventCallback): this;
620654

655+
/**
656+
* A Job is waiting to be processed as soon as a worker is idling.
657+
*/
658+
on(event: 'waiting', callback: WaitingEventCallback): this;
659+
621660
/**
622661
* A job has started. You can use `jobPromise.cancel()` to abort it
623662
*/
@@ -654,13 +693,24 @@ declare namespace Bull {
654693
*/
655694
on(event: 'resumed', callback: EventCallback): this; // tslint:disable-line unified-signatures
656695

696+
/**
697+
* A job successfully removed.
698+
*/
699+
on(event: 'removed', callback: RemovedEventCallback<T>): this;
700+
657701
/**
658702
* Old jobs have been cleaned from the queue.
659703
* `jobs` is an array of jobs that were removed, and `type` is the type of those jobs.
660704
*
661705
* @see Queue#clean() for details
662706
*/
663707
on(event: 'cleaned', callback: CleanedEventCallback<T>): this;
708+
709+
/**
710+
* Emitted every time the queue has processed all the waiting jobs
711+
* (even if there can be some delayed jobs not yet processed)
712+
*/
713+
on(event: 'drained', callback: EventCallback): this; // tslint:disable-line unified-signatures
664714
}
665715

666716
type EventCallback = () => void;
@@ -685,6 +735,10 @@ declare namespace Bull {
685735
type FailedEventCallback<T = any> = (job: Job<T>, error: Error) => void;
686736

687737
type CleanedEventCallback<T = any> = (jobs: Array<Job<T>>, status: JobStatus) => void;
738+
739+
type RemovedEventCallback<T = any> = (job: Job<T>) => void;
740+
741+
type WaitingEventCallback = (jobId: JobId) => void;
688742
}
689743

690744
export = Bull;

0 commit comments

Comments
 (0)