Add generic types to Queue and Job

This commit is contained in:
David Koblas
2018-06-05 16:59:15 -04:00
parent 471e6f1e4b
commit 4bcbb70240
2 changed files with 23 additions and 22 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

43
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