Merge pull request #26303 from koblas/master

Add generic types to Queue and Job
This commit is contained in:
Armando Aguirre
2018-06-06 13:12:39 -07:00
committed by GitHub
2 changed files with 35 additions and 34 deletions

View File

@@ -7,7 +7,7 @@ import Queue = require("bull");
const videoQueue = new Queue('video transcoding', 'redis://127.0.0.1:6379');
const audioQueue = new Queue('audio transcoding', {redis: {port: 6379, host: '127.0.0.1'}}); // Specify Redis connection using object
const imageQueue = new Queue('image transcoding');
const imageQueue: Queue.Queue<{ image: string }> = new Queue('image transcoding');
videoQueue.process((job, done) => {
// job.data contains the custom data passed when the job was created

67
types/bull/index.d.ts vendored
View File

@@ -6,6 +6,7 @@
// Weeco <https://github.com/weeco>
// Gabriel Terwesten <https://github.com/blaugold>
// Oleg Repin <https://github.com/iamolegga>
// David Koblas <https://github.com/koblas>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
@@ -87,13 +88,13 @@ declare namespace Bull {
type JobId = number | string;
interface Job {
interface Job<T = any> {
id: JobId;
/**
* The custom data passed when the job was created
*/
data: any;
data: T;
/**
* How many attempts where made to run this job
@@ -258,7 +259,7 @@ declare namespace Bull {
next: number;
}
interface Queue {
interface Queue<T = any> {
/**
* Returns a promise that resolves when Redis is connected and the queue is ready to accept jobs.
* This replaces the `ready` event emitted on Queue in previous verisons.
@@ -276,7 +277,7 @@ declare namespace Bull {
* Errors will be passed as a second argument to the "failed" event;
* results, as a second argument to the "completed" event.
*/
process(callback: (job: Job, done: DoneCallback) => void): void;
process(callback: (job: Job<T>, done: DoneCallback) => void): void;
/**
* Defines a processing function for the jobs placed into a given Queue.
@@ -294,7 +295,7 @@ declare namespace Bull {
* If the promise is rejected, the error will be passed as a second argument to the "failed" event.
* If it is resolved, its value will be the "completed" event's second argument.
*/
process(callback: ((job: Job) => void) | string): Promise<any>;
process(callback: ((job: Job<T>) => void) | string): Promise<any>;
/**
* Defines a processing function for the jobs placed into a given Queue.
@@ -314,7 +315,7 @@ declare namespace Bull {
*
* @param concurrency Bull will then call you handler in parallel respecting this max number.
*/
process(concurrency: number, callback: ((job: Job) => void) | string): Promise<any>;
process(concurrency: number, callback: ((job: Job<T>) => void) | string): Promise<any>;
/**
* Defines a processing function for the jobs placed into a given Queue.
@@ -329,7 +330,7 @@ declare namespace Bull {
*
* @param concurrency Bull will then call you handler in parallel respecting this max number.
*/
process(concurrency: number, callback: (job: Job, done: DoneCallback) => void): void;
process(concurrency: number, callback: (job: Job<T>, done: DoneCallback) => void): void;
/**
* Defines a named processing function for the jobs placed into a given Queue.
@@ -350,7 +351,7 @@ declare namespace Bull {
* @param name Bull will only call the handler if the job name matches
*/
// tslint:disable-next-line:unified-signatures
process(name: string, callback: ((job: Job) => void) | string): Promise<any>;
process(name: string, callback: ((job: Job<T>) => void) | string): Promise<any>;
/**
* Defines a processing function for the jobs placed into a given Queue.
@@ -366,7 +367,7 @@ declare namespace Bull {
* @param name Bull will only call the handler if the job name matches
*/
// tslint:disable-next-line:unified-signatures
process(name: string, callback: (job: Job, done: DoneCallback) => void): void;
process(name: string, callback: (job: Job<T>, done: DoneCallback) => void): void;
/**
* Defines a named processing function for the jobs placed into a given Queue.
@@ -387,7 +388,7 @@ declare namespace Bull {
* @param name Bull will only call the handler if the job name matches
* @param concurrency Bull will then call you handler in parallel respecting this max number.
*/
process(name: string, concurrency: number, callback: ((job: Job) => void) | string): Promise<any>;
process(name: string, concurrency: number, callback: ((job: Job<T>) => void) | string): Promise<any>;
/**
* Defines a processing function for the jobs placed into a given Queue.
@@ -403,21 +404,21 @@ declare namespace Bull {
* @param name Bull will only call the handler if the job name matches
* @param concurrency Bull will then call you handler in parallel respecting this max number.
*/
process(name: string, concurrency: number, callback: (job: Job, done: DoneCallback) => void): void;
process(name: string, concurrency: number, callback: (job: Job<T>, done: DoneCallback) => void): void;
/**
* Creates a new job and adds it to the queue.
* If the queue is empty the job will be executed directly,
* otherwise it will be placed in the queue and executed as soon as possible.
*/
add(data: any, opts?: JobOptions): Promise<Job>;
add(data: T, opts?: JobOptions): Promise<Job<T>>;
/**
* Creates a new named job and adds it to the queue.
* If the queue is empty the job will be executed directly,
* otherwise it will be placed in the queue and executed as soon as possible.
*/
add(name: string, data: any, opts?: JobOptions): Promise<Job>;
add(name: string, data: T, opts?: JobOptions): Promise<Job<T>>;
/**
* Returns a promise that resolves when the queue is paused.
@@ -466,32 +467,32 @@ declare namespace Bull {
* Returns a promise that will return the job instance associated with the jobId parameter.
* If the specified job cannot be located, the promise callback parameter will be set to null.
*/
getJob(jobId: JobId): Promise<Job>;
getJob(jobId: JobId): Promise<Job<T>>;
/**
* Returns a promise that will return an array with the waiting jobs between start and end.
*/
getWaiting(start?: number, end?: number): Promise<Job[]>;
getWaiting(start?: number, end?: number): Promise<Array<Job<T>>>;
/**
* Returns a promise that will return an array with the active jobs between start and end.
*/
getActive(start?: number, end?: number): Promise<Job[]>;
getActive(start?: number, end?: number): Promise<Array<Job<T>>>;
/**
* Returns a promise that will return an array with the delayed jobs between start and end.
*/
getDelayed(start?: number, end?: number): Promise<Job[]>;
getDelayed(start?: number, end?: number): Promise<Array<Job<T>>>;
/**
* Returns a promise that will return an array with the completed jobs between start and end.
*/
getCompleted(start?: number, end?: number): Promise<Job[]>;
getCompleted(start?: number, end?: number): Promise<Array<Job<T>>>;
/**
* Returns a promise that will return an array with the failed jobs between start and end.
*/
getFailed(start?: number, end?: number): Promise<Job[]>;
getFailed(start?: number, end?: number): Promise<Array<Job<T>>>;
/**
* Returns JobInformation of repeatable jobs (ordered descending). Provide a start and/or an end
@@ -502,7 +503,7 @@ declare namespace Bull {
/**
* ???
*/
nextRepeatableJob(name: string, data: any, opts: JobOptions): Promise<Job>;
nextRepeatableJob(name: string, data: any, opts: JobOptions): Promise<Job<T>>;
/**
* Removes a given repeatable job. The RepeatOptions and JobId needs to be the same as the ones
@@ -565,7 +566,7 @@ declare namespace Bull {
* @param status Status of the job to clean. Values are completed, wait, active, delayed, and failed. Defaults to completed.
* @param limit Maximum amount of jobs to clean per call. If not provided will clean all matching jobs.
*/
clean(grace: number, status?: JobStatus, limit?: number): Promise<Job[]>;
clean(grace: number, status?: JobStatus, limit?: number): Promise<Array<Job<T>>>;
/**
* Listens to queue events
@@ -580,28 +581,28 @@ declare namespace Bull {
/**
* A job has started. You can use `jobPromise.cancel()` to abort it
*/
on(event: 'active', callback: ActiveEventCallback): this;
on(event: 'active', callback: ActiveEventCallback<T>): this;
/**
* A job has been marked as stalled.
* This is useful for debugging job workers that crash or pause the event loop.
*/
on(event: 'stalled', callback: StalledEventCallback): this;
on(event: 'stalled', callback: StalledEventCallback<T>): this;
/**
* A job's progress was updated
*/
on(event: 'progress', callback: ProgressEventCallback): this;
on(event: 'progress', callback: ProgressEventCallback<T>): this;
/**
* A job successfully completed with a `result`
*/
on(event: 'completed', callback: CompletedEventCallback): this;
on(event: 'completed', callback: CompletedEventCallback<T>): this;
/**
* A job failed with `err` as the reason
*/
on(event: 'failed', callback: FailedEventCallback): this;
on(event: 'failed', callback: FailedEventCallback<T>): this;
/**
* The queue has been paused
@@ -619,7 +620,7 @@ declare namespace Bull {
*
* @see Queue#clean() for details
*/
on(event: 'cleaned', callback: CleanedEventCallback): this;
on(event: 'cleaned', callback: CleanedEventCallback<T>): this;
}
type EventCallback = () => void;
@@ -633,17 +634,17 @@ declare namespace Bull {
cancel(): void;
}
type ActiveEventCallback = (job: Job, jobPromise?: JobPromise) => void;
type ActiveEventCallback<T = any> = (job: Job<T>, jobPromise?: JobPromise) => void;
type StalledEventCallback = (job: Job) => void;
type StalledEventCallback<T = any> = (job: Job<T>) => void;
type ProgressEventCallback = (job: Job, progress: any) => void;
type ProgressEventCallback<T = any> = (job: Job<T>, progress: any) => void;
type CompletedEventCallback = (job: Job, result: any) => void;
type CompletedEventCallback<T = any> = (job: Job<T>, result: any) => void;
type FailedEventCallback = (job: Job, error: Error) => void;
type FailedEventCallback<T = any> = (job: Job<T>, error: Error) => void;
type CleanedEventCallback = (jobs: Job[], status: JobStatus) => void;
type CleanedEventCallback<T = any> = (jobs: Array<Job<T>>, status: JobStatus) => void;
}
export = Bull;