From 3ea5ad14c4b453916df64fbec2bf3530d15c9184 Mon Sep 17 00:00:00 2001 From: Dan Manastireanu <498419+danmana@users.noreply.github.com> Date: Mon, 22 Oct 2018 19:38:28 +0300 Subject: [PATCH] @types/bull Update to version 3.4.8. Closes #22673 #23965 #18911 (#29875) * @types/bull Update to version 3.4.8. Closes #22673 #23965 #18911 * Fix return type for moveTo* methods --- types/bull/bull-tests.tsx | 52 ++++++++++++++++++++++++++++++++- types/bull/index.d.ts | 60 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 108 insertions(+), 4 deletions(-) diff --git a/types/bull/bull-tests.tsx b/types/bull/bull-tests.tsx index a8c7f403a7..7fd5a6a45d 100644 --- a/types/bull/bull-tests.tsx +++ b/types/bull/bull-tests.tsx @@ -125,6 +125,7 @@ videoQueue.add({ video: 'http://example.com/video1.mov' }, { jobId: 1 }) pdfQueue .on('error', (err: Error) => undefined) .on('active', (job: Queue.Job, jobPromise: Queue.JobPromise) => jobPromise.cancel()) +.on('waiting', (jobId: Queue.JobId) => undefined) .on('active', (job: Queue.Job) => undefined) .on('stalled', (job: Queue.Job) => undefined) .on('progress', (job: Queue.Job) => undefined) @@ -132,4 +133,53 @@ pdfQueue .on('failed', (job: Queue.Job) => undefined) .on('paused', () => undefined) .on('resumed', () => undefined) -.on('cleaned', (jobs: Queue.Job[], status: Queue.JobStatus) => undefined); +.on('cleaned', (jobs: Queue.Job[], status: Queue.JobStatus) => undefined) +.on('drained', () => undefined) +.on('removed', (job: Queue.Job) => undefined); + +// test different process methods + +const profileQueue = new Queue('profile'); +// Max concurrency for requestProfile is 100 +profileQueue.process('requestProfile', 100, () => {}); +profileQueue.process(100, () => {}); + +// other tests +const myQueue = new Queue('myQueue', { + settings: { + drainDelay: 5 + }, + defaultJobOptions: { + stackTraceLimit: 1, + } +}); + +myQueue.on('active', (job: Queue.Job) => { + job.moveToCompleted(); + job.moveToCompleted('done'); + job.moveToCompleted('done', true); + job.moveToCompleted('done', true).then(val => { + if (val) { + const nextJobData: any = val[0]; + const nextJobId: Queue.JobId = val[1]; + } + }); + + job.moveToFailed({ message: "Call to external service failed!" }, true); + job.moveToFailed(new Error('test error'), true); + job.moveToFailed(new Error('test error'), true).then(val => { + if (val) { + const nextJobData: any = val[0]; + const nextJobId: Queue.JobId = val[1]; + } + }); + + job.discard(); +}); + +// test all constructor options: + +new Queue('profile'); +new Queue('profile', 'url'); +new Queue('profile', { prefix: 'test' }); +new Queue('profile', 'url', { prefix: 'test' }); diff --git a/types/bull/index.d.ts b/types/bull/index.d.ts index ac820290f2..82b09928da 100644 --- a/types/bull/index.d.ts +++ b/types/bull/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for bull 3.3 +// Type definitions for bull 3.4 // Project: https://github.com/OptimalBits/bull // Definitions by: Bruno Grieder // Cameron Crothers @@ -10,6 +10,7 @@ // Bond Akinmade // Wuha Team // Alec Brunelle +// Dan Manastireanu // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 @@ -23,9 +24,9 @@ import * as Promise from "bluebird"; */ declare const Bull: { (queueName: string, opts?: Bull.QueueOptions): Bull.Queue; - (queueName: string, url?: string): Bull.Queue; // tslint:disable-line unified-signatures + (queueName: string, url: string, opts?: Bull.QueueOptions): Bull.Queue; // tslint:disable-line unified-signatures new (queueName: string, opts?: Bull.QueueOptions): Bull.Queue; - new (queueName: string, url?: string): Bull.Queue; // tslint:disable-line unified-signatures + new (queueName: string, url: string, opts?: Bull.QueueOptions): Bull.Queue; // tslint:disable-line unified-signatures }; declare namespace Bull { @@ -92,6 +93,12 @@ declare namespace Bull { backoffStrategies?: { [key: string]: (attemptsMade: number, err: typeof Error) => number; }; + + /** + * A timeout for when the queue is in `drained` state (empty waiting for jobs). + * It is used when calling `queue.getNextJob()`, which will pass it to `.brpoplpush` on the Redis client. + */ + drainDelay?: number; } type DoneCallback = (error?: Error | null, value?: any) => void; @@ -141,6 +148,11 @@ declare namespace Bull { */ retry(): Promise; + /** + * Ensure this job is never ran again even if attemptsMade is less than job.attempts. + */ + discard(): Promise; + /** * Returns a promise that resolves to the returned data when the job has been finished. * TODO: Add a watchdog to check if the job has finished periodically. @@ -148,6 +160,18 @@ declare namespace Bull { */ finished(): Promise; + /** + * Moves a job to the `completed` queue. Pulls a job from 'waiting' to 'active' + * and returns a tuple containing the next jobs data and id. If no job is in the `waiting` queue, returns null. + */ + moveToCompleted(returnValue?: string, ignoreLock?: boolean): Promise<[any, JobId] | null>; + + /** + * Moves a job to the `failed` queue. Pulls a job from 'waiting' to 'active' + * and returns a tuple containing the next jobs data and id. If no job is in the `waiting` queue, returns null. + */ + moveToFailed(errorInfo: { message: string; }, ignoreLock?: boolean): Promise<[any, JobId] | null>; + /** * Promotes a job that is currently "delayed" to the "waiting" state and executed as soon as possible. */ @@ -205,6 +229,11 @@ declare namespace Bull { * Cron pattern specifying when the job should execute */ cron: string; + + /** + * Start date when the repeat job should start repeating (only with cron). + */ + startDate?: Date | string | number; } interface EveryRepeatOptions extends RepeatOptions { @@ -273,6 +302,11 @@ declare namespace Bull { * Default behavior is to keep the job in the completed set. */ removeOnFail?: boolean; + + /** + * Limits the amount of stack trace lines that will be recorded in the stacktrace. + */ + stackTraceLimit?: number; } interface JobCounts { @@ -618,6 +652,11 @@ declare namespace Bull { */ on(event: 'error', callback: ErrorEventCallback): this; + /** + * A Job is waiting to be processed as soon as a worker is idling. + */ + on(event: 'waiting', callback: WaitingEventCallback): this; + /** * A job has started. You can use `jobPromise.cancel()` to abort it */ @@ -654,6 +693,11 @@ declare namespace Bull { */ on(event: 'resumed', callback: EventCallback): this; // tslint:disable-line unified-signatures + /** + * A job successfully removed. + */ + on(event: 'removed', callback: RemovedEventCallback): this; + /** * Old jobs have been cleaned from the queue. * `jobs` is an array of jobs that were removed, and `type` is the type of those jobs. @@ -661,6 +705,12 @@ declare namespace Bull { * @see Queue#clean() for details */ on(event: 'cleaned', callback: CleanedEventCallback): this; + + /** + * Emitted every time the queue has processed all the waiting jobs + * (even if there can be some delayed jobs not yet processed) + */ + on(event: 'drained', callback: EventCallback): this; // tslint:disable-line unified-signatures } type EventCallback = () => void; @@ -685,6 +735,10 @@ declare namespace Bull { type FailedEventCallback = (job: Job, error: Error) => void; type CleanedEventCallback = (jobs: Array>, status: JobStatus) => void; + + type RemovedEventCallback = (job: Job) => void; + + type WaitingEventCallback = (jobId: JobId) => void; } export = Bull;