Merge pull request #17002 from marshall007/bull-v3

@types/bull: updates for API changes in v3
This commit is contained in:
Mine Starks
2017-06-15 11:09:41 -07:00
committed by GitHub
7 changed files with 925 additions and 360 deletions

View File

@@ -2,71 +2,92 @@
* Created by Bruno Grieder
*/
import * as Queue from "bull"
import * as Redis from "ioredis";
import * as Queue from "bull";
var videoQueue = Queue( 'video transcoding', 6379, '127.0.0.1' );
var audioQueue = Queue( 'audio transcoding', 6379, '127.0.0.1' );
var imageQueue = Queue( 'image transcoding', 6379, '127.0.0.1' );
videoQueue.process( ( job: Queue.Job, done: Queue.DoneCallback ) => {
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');
videoQueue.process((job, done) => {
// job.data contains the custom data passed when the job was created
// job.jobId contains id of this job.
// transcode video asynchronously and report progress
job.progress( 42 );
job.progress(42);
// call done when finished
done();
// or give a error if error
done( Error( 'error transcoding' ) );
done(new Error('error transcoding'));
// or pass it a result
done( null, { framerate: 29.5 /* etc... */ } );
done(null, { framerate: 29.5 /* etc... */ });
// If the job throws an unhandled exception it is also handled correctly
throw (Error( 'some unexpected error' ));
} );
throw new Error('some unexpected error');
});
audioQueue.process( ( job: Queue.Job, done: Queue.DoneCallback ) => {
audioQueue.process((job, done) => {
// transcode audio asynchronously and report progress
job.progress( 42 );
job.progress(42);
// call done when finished
done();
// or give a error if error
done( Error( 'error transcoding' ) );
done(new Error('error transcoding'));
// or pass it a result
done( null, { samplerate: 48000 /* etc... */ } );
done(null, { samplerate: 48000 /* etc... */ });
// If the job throws an unhandled exception it is also handled correctly
throw (Error( 'some unexpected error' ));
} );
throw new Error('some unexpected error');
});
imageQueue.process( ( job: Queue.Job, done: Queue.DoneCallback ) => {
imageQueue.process((job, done) => {
// transcode image asynchronously and report progress
job.progress( 42 );
job.progress(42);
// call done when finished
done();
// or give a error if error
done( Error( 'error transcoding' ) );
done(new Error('error transcoding'));
// or pass it a result
done( null, { width: 1280, height: 720 /* etc... */ } );
done(null, { width: 1280, height: 720 /* etc... */ });
// If the job throws an unhandled exception it is also handled correctly
throw (Error( 'some unexpected error' ));
} );
throw new Error('some unexpected error');
});
videoQueue.add( { video: 'http://example.com/video1.mov' } );
audioQueue.add( { audio: 'http://example.com/audio1.mp3' } );
imageQueue.add( { image: 'http://example.com/image1.tiff' } );
videoQueue.add({video: 'http://example.com/video1.mov'});
audioQueue.add({audio: 'http://example.com/audio1.mp3'});
imageQueue.add({image: 'http://example.com/image1.tiff'});
//////////////////////////////////////////////////////////////////////////////////
//
// Re-using Redis Connections
//
//////////////////////////////////////////////////////////////////////////////////
const client = new Redis();
const subscriber = new Redis();
const pdfQueue = new Queue('pdf transcoding', {
createClient: (type: string, options: Redis.RedisOptions) => {
switch (type) {
case 'client':
return client;
case 'subscriber':
return subscriber;
default:
return new Redis(options);
}
}
});
//////////////////////////////////////////////////////////////////////////////////
//
@@ -74,42 +95,38 @@ imageQueue.add( { image: 'http://example.com/image1.tiff' } );
//
//////////////////////////////////////////////////////////////////////////////////
const fetchVideo = ( url: string ): Promise<any> => { return null }
const transcodeVideo = ( data: any ): Promise<void> => { return null }
pdfQueue.process((job) => {
// Processors can also return promises instead of using the done callback
return Promise.resolve();
});
interface VideoJob extends Queue.Job {
data: {url: string}
}
videoQueue.process( ( job: VideoJob ) => { // don't forget to remove the done callback!
// Simply return a promise
fetchVideo( job.data.url ).then( transcodeVideo );
// Handles promise rejection
Promise.reject( new Error( 'error transcoding' ) );
// Passes the value the promise is resolved with to the "completed" event
Promise.resolve( { framerate: 29.5 /* etc... */ } );
// same as
Promise.reject( new Error( 'some unexpected error' ) );
// If the job throws an unhandled exception it is also handled correctly
throw new Error( 'some unexpected error' );
} );
var addVideo1Job = videoQueue.add( { video: 'http://example.com/video1.mov' } );
addVideo1Job.then((video1Job) => {
videoQueue.add({ video: 'http://example.com/video1.mov' }, { jobId: 1 })
.then((video1Job) => {
// When job has successfully be placed in the queue the job is returned
// then wait for completion
return video1Job.finished();
})
.then(() => {
// video1Job completed successfully
// completed successfully
})
.catch((err) => {
// error
});
//////////////////////////////////////////////////////////////////////////////////
//
// Typed Event Handlers
//
//////////////////////////////////////////////////////////////////////////////////
pdfQueue
.on('error', (err: Error) => undefined)
.on('active', (job: Queue.Job, jobPromise: Queue.JobPromise) => jobPromise.cancel())
.on('active', (job: Queue.Job) => undefined)
.on('stalled', (job: Queue.Job) => undefined)
.on('progress', (job: Queue.Job) => undefined)
.on('completed', (job: Queue.Job) => undefined)
.on('failed', (job: Queue.Job) => undefined)
.on('paused', () => undefined)
.on('resumed', () => undefined)
.on('cleaned', (jobs: Queue.Job[], status: Queue.JobStatus) => undefined);

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

@@ -1,316 +1,414 @@
// Type definitions for bull 2.1.2
// Type definitions for bull 3.0
// Project: https://github.com/OptimalBits/bull
// Definitions by: Bruno Grieder <https://github.com/bgrieder>, Cameron Crothers <https://github.com/JProgrammer>
// Definitions by: Bruno Grieder <https://github.com/bgrieder>
// Cameron Crothers <https://github.com/JProgrammer>
// Marshall Cottrell <https://github.com/marshall007>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="redis" />
import * as Redis from "ioredis";
declare module "bull" {
/**
* This is the Queue constructor.
* It creates a new Queue that is persisted in Redis.
* Everytime the same queue is instantiated it tries to process all the old jobs that may exist from a previous unfinished session.
*/
declare const Bull: {
// tslint:disable:unified-signatures
(queueName: string, opts?: Bull.QueueOptions): Bull.Queue;
(queueName: string, url?: string): Bull.Queue;
new (queueName: string, opts?: Bull.QueueOptions): Bull.Queue;
new (queueName: string, url?: string): Bull.Queue;
// tslint:enable:unified-signatures
};
import * as Redis from "redis";
/**
* This is the Queue constructor.
* It creates a new Queue that is persisted in Redis.
* Everytime the same queue is instantiated it tries to process all the old jobs that may exist from a previous unfinished session.
*/
function Bull(queueName: string, redisPort: number, redisHost: string, redisOpt?: Redis.ClientOpts): Bull.Queue;
namespace Bull {
export interface DoneCallback {
(error?: Error, value?: any): void
}
export interface Job {
jobId: string
/**
* The custom data passed when the job was created
*/
data: Object;
/**
* Report progress on a job
*/
progress(value: any): Promise<void>;
/**
* Removes a Job from the queue from all the lists where it may be included.
* @returns {Promise} A promise that resolves when the job is removed.
*/
remove(): Promise<void>;
/**
* Rerun a Job that has failed.
* @returns {Promise} A promise that resolves when the job is scheduled for retry.
*/
retry(): Promise<void>;
/**
* Returns a promise the resolves when the job has been finished.
* TODO: Add a watchdog to check if the job has finished periodically.
* since pubsub does not give any guarantees.
*/
finished(): Promise<void>;
}
export interface Backoff {
/**
* Backoff type, which can be either `fixed` or `exponential`
*/
type: string
/**
* Backoff delay, in milliseconds
*/
delay: number;
}
export interface AddOptions {
/**
* An amount of miliseconds to wait until this job can be processed.
* Note that for accurate delays, both server and clients should have their clocks synchronized
*/
delay?: number;
/**
* A number of attempts to retry if the job fails [optional]
*/
attempts?: number;
/**
* Backoff setting for automatic retries if the job fails
*/
backoff?: number | Backoff
/**
* A boolean which, if true, adds the job to the right
* of the queue instead of the left (default false)
*/
lifo?: boolean;
/**
* The number of milliseconds after which the job should be fail with a timeout error
*/
timeout?: number;
}
export interface Queue {
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* The done callback can be called with an Error instance, to signal that the job did not complete successfully,
* or with a result as second argument as second argument (e.g.: done(null, result);) when the job is successful.
* Errors will be passed as a second argument to the "failed" event;
* results, as a second argument to the "completed" event.
*
* concurrency: Bull will then call you handler in parallel respecting this max number.
*/
process(concurrency: number, callback: (job: Job, done: DoneCallback) => void): void;
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* The done callback can be called with an Error instance, to signal that the job did not complete successfully,
* or with a result as second argument as second argument (e.g.: done(null, result);) when the job is successful.
* 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;
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* A promise must be returned to signal job completion.
* 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.
*
* concurrency: Bull will then call you handler in parallel respecting this max number.
*/
process(concurrency: number, callback: (job: Job) => void): Promise<any>;
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* A promise must be returned to signal job completion.
* 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): Promise<any>;
// process(callback: (job: Job, done?: DoneCallback) => void): Promise<any>;
/**
* 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: Object, opts?: AddOptions): Promise<Job>;
/**
* Returns a promise that resolves when the queue is paused.
* The pause is global, meaning that all workers in all queue instances for a given queue will be paused.
* A paused queue will not process new jobs until resumed,
* but current jobs being processed will continue until they are finalized.
*
* Pausing a queue that is already paused does nothing.
*/
pause(): Promise<void>;
/**
* Returns a promise that resolves when the queue is resumed after being paused.
* The resume is global, meaning that all workers in all queue instances for a given queue will be resumed.
*
* Resuming a queue that is not paused does nothing.
*/
resume(): Promise<void>;
/**
* Returns a promise that returns the number of jobs in the queue, waiting or paused.
* Since there may be other processes adding or processing jobs, this value may be true only for a very small amount of time.
*/
count(): Promise<number>;
/**
* Empties a queue deleting all the input lists and associated jobs.
*/
empty(): Promise<void>;
/**
* Closes the underlying redis client. Use this to perform a graceful shutdown.
*
* `close` can be called from anywhere, with one caveat:
* if called from within a job handler the queue won't close until after the job has been processed
*/
close(): Promise<void>;
/**
* 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: string): Promise<Job>;
/**
* Tells the queue remove all jobs created outside of a grace period in milliseconds.
* You can clean the jobs with the following states: completed, waiting, active, delayed, and failed.
*/
clean(gracePeriod: number, jobsState?: string): Promise<Job[]>;
/**
* Listens to queue events
* 'ready', 'error', 'activ', 'progress', 'completed', 'failed', 'paused', 'resumed', 'cleaned'
*/
on(eventName: string, callback: EventCallback): void;
}
interface EventCallback {
(...args: any[]): void
}
interface ReadyEventCallback extends EventCallback {
(): void;
}
interface ErrorEventCallback extends EventCallback {
(error: Error): void;
}
interface JobPromise {
/**
* Abort this job
*/
cancel(): void
}
interface ActiveEventCallback extends EventCallback {
(job: Job, jobPromise: JobPromise): void;
}
interface ProgressEventCallback extends EventCallback {
(job: Job, progress: any): void;
}
interface CompletedEventCallback extends EventCallback {
(job: Job, result: Object): void;
}
interface FailedEventCallback extends EventCallback {
(job: Job, error: Error): void;
}
interface PausedEventCallback extends EventCallback {
(): void;
}
interface ResumedEventCallback extends EventCallback {
(job?: Job): void;
}
declare namespace Bull {
interface QueueOptions {
/**
* Options passed directly to the `ioredis` constructor
*/
redis?: Redis.RedisOptions;
/**
* @see clean() for details
* When specified, the `Queue` will use this function to create new `ioredis` client connections.
* This is useful if you want to re-use connections.
*/
interface CleanedEventCallback extends EventCallback {
(jobs: Job[], type: string): void;
}
createClient?(type: 'client' | 'subscriber', redisOpts?: Redis.RedisOptions): Redis.Redis;
/**
* Prefix to use for all redis keys
*/
prefix?: string;
settings?: AdvancedSettings;
}
export = Bull;
}
interface AdvancedSettings {
/**
* Key expiration time for job locks
*/
lockDuration?: number;
declare module "bull/lib/priority-queue" {
/**
* How often check for stalled jobs (use 0 for never checking)
*/
stalledInterval?: number;
import * as Bull from "bull";
import * as Redis from "redis";
/**
* Max amount of times a stalled job will be re-processed
*/
maxStalledCount?: number;
/**
* This is the Queue constructor of priority queue.
*
* It works same a normal queue, with same function and parameters.
* The only difference is that the Queue#add() allow an options opts.priority
* that could take ["low", "normal", "medium", "hight", "critical"]. If no options provider, "normal" will be taken.
*
* The priority queue will process more often highter priority jobs than lower.
*/
function PQueue(queueName: string, redisPort: number, redisHost: string, redisOpt?: Redis.ClientOpts): PQueue.PriorityQueue;
/**
* Poll interval for delayed jobs and added jobs
*/
guardInterval?: number;
namespace PQueue {
export interface AddOptions extends Bull.AddOptions {
/**
* "low", "normal", "medium", "high", "critical"
*/
priority?: string;
}
export interface PriorityQueue extends Bull.Queue {
/**
* 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: Object, opts?: PQueue.AddOptions): Promise<Bull.Job>;
}
/**
* Delay before processing next job in case of internal error
*/
retryProcessDelay?: number;
}
export = PQueue;
type DoneCallback = (error?: Error | null, value?: any) => void;
type JobId = number | string;
interface Job {
id: JobId;
/**
* The custom data passed when the job was created
*/
data: any;
/**
* Report progress on a job
*/
progress(value: any): Promise<void>;
/**
* Removes a job from the queue and from any lists it may be included in.
* @returns {Promise} A promise that resolves when the job is removed.
*/
remove(): Promise<void>;
/**
* Re-run a job that has failed.
* @returns {Promise} A promise that resolves when the job is scheduled for retry.
*/
retry(): Promise<void>;
/**
* Returns a promise the resolves when the job has been finished.
* TODO: Add a watchdog to check if the job has finished periodically.
* since pubsub does not give any guarantees.
*/
finished(): Promise<void>;
}
type JobStatus = 'completed' | 'waiting' | 'active' | 'delayed' | 'failed';
interface BackoffOptions {
/**
* Backoff type, which can be either `fixed` or `exponential`
*/
type: 'fixed' | 'exponential';
/**
* Backoff delay, in milliseconds
*/
delay: number;
}
interface RepeatOptions {
/**
* Cron pattern specifying when the job should execute
*/
cron: string;
/**
* Timezone
*/
tz?: string;
/**
* End date when the repeat job should stop repeating
*/
endDate?: Date | string | number;
}
interface JobOptions {
/**
* Optional priority value. ranges from 1 (highest priority) to MAX_INT (lowest priority).
* Note that using priorities has a slight impact on performance, so do not use it if not required
*/
priority?: number;
/**
* An amount of miliseconds to wait until this job can be processed.
* Note that for accurate delays, both server and clients should have their clocks synchronized. [optional]
*/
delay?: number;
/**
* The total number of attempts to try the job until it completes
*/
attempts?: number;
/**
* Repeat job according to a cron specification
*/
repeat?: RepeatOptions;
/**
* Backoff setting for automatic retries if the job fails
*/
backoff?: number | BackoffOptions;
/**
* A boolean which, if true, adds the job to the right
* of the queue instead of the left (default false)
*/
lifo?: boolean;
/**
* The number of milliseconds after which the job should be fail with a timeout error
*/
timeout?: number;
/**
* Override the job ID - by default, the job ID is a unique
* integer, but you can use this setting to override it.
* If you use this option, it is up to you to ensure the
* jobId is unique. If you attempt to add a job with an id that
* already exists, it will not be added.
*/
jobId?: JobId;
/**
* A boolean which, if true, removes the job when it successfully completes.
* Default behavior is to keep the job in the completed set.
*/
removeOnComplete?: boolean;
/**
* A boolean which, if true, removes the job when it fails after all attempts
* Default behavior is to keep the job in the completed set.
*/
removeOnFail?: boolean;
}
interface JobCounts {
wait: number;
active: number;
completed: number;
failed: number;
delayed: number;
}
interface Queue {
/**
* 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.
*/
isReady(): Promise<this>;
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* The done callback can be called with an Error instance, to signal that the job did not complete successfully,
* or with a result as second argument as second argument (e.g.: done(null, result);) when the job is successful.
* Errors will be passed as a second argument to the "failed" event;
* results, as a second argument to the "completed" event.
*
* concurrency: Bull will then call you handler in parallel respecting this max number.
*/
process(concurrency: number, callback: (job: Job, done: DoneCallback) => void): void;
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* The done callback can be called with an Error instance, to signal that the job did not complete successfully,
* or with a result as second argument as second argument (e.g.: done(null, result);) when the job is successful.
* 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;
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* A promise must be returned to signal job completion.
* 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.
*
* concurrency: Bull will then call you handler in parallel respecting this max number.
*/
process(concurrency: number, callback: (job: Job) => void): Promise<any>;
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* A promise must be returned to signal job completion.
* 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): Promise<any>;
/**
* 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>;
/**
* Returns a promise that resolves when the queue is paused.
* The pause is global, meaning that all workers in all queue instances for a given queue will be paused.
* A paused queue will not process new jobs until resumed,
* but current jobs being processed will continue until they are finalized.
*
* Pausing a queue that is already paused does nothing.
*/
pause(): Promise<void>;
/**
* Returns a promise that resolves when the queue is resumed after being paused.
* The resume is global, meaning that all workers in all queue instances for a given queue will be resumed.
*
* Resuming a queue that is not paused does nothing.
*/
resume(): Promise<void>;
/**
* Returns a promise that returns the number of jobs in the queue, waiting or paused.
* Since there may be other processes adding or processing jobs, this value may be true only for a very small amount of time.
*/
count(): Promise<number>;
/**
* Empties a queue deleting all the input lists and associated jobs.
*/
empty(): Promise<void>;
/**
* Closes the underlying redis client. Use this to perform a graceful shutdown.
*
* `close` can be called from anywhere, with one caveat:
* if called from within a job handler the queue won't close until after the job has been processed
*/
close(): Promise<void>;
/**
* 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>;
/**
* Returns a promise that resolves with the job counts for the given queue
*/
getJobCounts(): Promise<JobCounts>;
/**
* Tells the queue remove all jobs created outside of a grace period in milliseconds.
* You can clean the jobs with the following states: completed, waiting, active, delayed, and failed.
*/
clean(grace: number, status?: JobStatus, limit?: number): Promise<Job[]>;
// tslint:disable:unified-signatures
/**
* Listens to queue events
*/
on(event: string, callback: (...args: any[]) => void): this;
/**
* An error occured
*/
on(event: 'error', callback: ErrorEventCallback): this;
/**
* A job has started. You can use `jobPromise.cancel()` to abort it
*/
on(event: 'active', callback: ActiveEventCallback): 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;
/**
* A job's progress was updated
*/
on(event: 'progress', callback: ProgressEventCallback): this;
/**
* A job successfully completed with a `result`
*/
on(event: 'completed', callback: CompletedEventCallback): this;
/**
* A job failed with `err` as the reason
*/
on(event: 'failed', callback: FailedEventCallback): this;
/**
* The queue has been paused
*/
on(event: 'paused', callback: EventCallback): this;
/**
* The queue has been resumed
*/
on(event: 'resumed', callback: EventCallback): 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.
*
* @see Queue#clean() for details
*/
on(event: 'cleaned', callback: CleanedEventCallback): this;
// tslint:enable:unified-signatures
}
type EventCallback = () => void;
type ErrorEventCallback = (error: Error) => void;
interface JobPromise {
/**
* Abort this job
*/
cancel(): void;
}
type ActiveEventCallback = (job: Job, jobPromise?: JobPromise) => void;
type StalledEventCallback = (job: Job) => void;
type ProgressEventCallback = (job: Job, progress: any) => void;
type CompletedEventCallback = (job: Job, result: any) => void;
type FailedEventCallback = (job: Job, error: Error) => void;
type CleanedEventCallback = (jobs: Job[], status: JobStatus) => void;
}
export = Bull;

View File

@@ -1,16 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"lib": [ "es6" ],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"baseUrl": "../",
"typeRoots": [
"../"
],
"typeRoots": [ "../" ],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true

1
types/bull/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@@ -0,0 +1,115 @@
/**
* Created by Bruno Grieder
*/
import * as Queue from "bull"
var videoQueue = Queue( 'video transcoding', 6379, '127.0.0.1' );
var audioQueue = Queue( 'audio transcoding', 6379, '127.0.0.1' );
var imageQueue = Queue( 'image transcoding', 6379, '127.0.0.1' );
videoQueue.process( ( job: Queue.Job, done: Queue.DoneCallback ) => {
// job.data contains the custom data passed when the job was created
// job.jobId contains id of this job.
// transcode video asynchronously and report progress
job.progress( 42 );
// call done when finished
done();
// or give a error if error
done( Error( 'error transcoding' ) );
// or pass it a result
done( null, { framerate: 29.5 /* etc... */ } );
// If the job throws an unhandled exception it is also handled correctly
throw (Error( 'some unexpected error' ));
} );
audioQueue.process( ( job: Queue.Job, done: Queue.DoneCallback ) => {
// transcode audio asynchronously and report progress
job.progress( 42 );
// call done when finished
done();
// or give a error if error
done( Error( 'error transcoding' ) );
// or pass it a result
done( null, { samplerate: 48000 /* etc... */ } );
// If the job throws an unhandled exception it is also handled correctly
throw (Error( 'some unexpected error' ));
} );
imageQueue.process( ( job: Queue.Job, done: Queue.DoneCallback ) => {
// transcode image asynchronously and report progress
job.progress( 42 );
// call done when finished
done();
// or give a error if error
done( Error( 'error transcoding' ) );
// or pass it a result
done( null, { width: 1280, height: 720 /* etc... */ } );
// If the job throws an unhandled exception it is also handled correctly
throw (Error( 'some unexpected error' ));
} );
videoQueue.add( { video: 'http://example.com/video1.mov' } );
audioQueue.add( { audio: 'http://example.com/audio1.mp3' } );
imageQueue.add( { image: 'http://example.com/image1.tiff' } );
//////////////////////////////////////////////////////////////////////////////////
//
// Using Promises
//
//////////////////////////////////////////////////////////////////////////////////
const fetchVideo = ( url: string ): Promise<any> => { return null }
const transcodeVideo = ( data: any ): Promise<void> => { return null }
interface VideoJob extends Queue.Job {
data: {url: string}
}
videoQueue.process( ( job: VideoJob ) => { // don't forget to remove the done callback!
// Simply return a promise
fetchVideo( job.data.url ).then( transcodeVideo );
// Handles promise rejection
Promise.reject( new Error( 'error transcoding' ) );
// Passes the value the promise is resolved with to the "completed" event
Promise.resolve( { framerate: 29.5 /* etc... */ } );
// same as
Promise.reject( new Error( 'some unexpected error' ) );
// If the job throws an unhandled exception it is also handled correctly
throw new Error( 'some unexpected error' );
} );
var addVideo1Job = videoQueue.add( { video: 'http://example.com/video1.mov' } );
addVideo1Job.then((video1Job) => {
// When job has successfully be placed in the queue the job is returned
// then wait for completion
return video1Job.finished();
})
.then(() => {
// video1Job completed successfully
})
.catch((err) => {
// error
});

316
types/bull/v2/index.d.ts vendored Normal file
View File

@@ -0,0 +1,316 @@
// Type definitions for bull 2.1.2
// Project: https://github.com/OptimalBits/bull
// Definitions by: Bruno Grieder <https://github.com/bgrieder>, Cameron Crothers <https://github.com/JProgrammer>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="redis" />
declare module "bull" {
import * as Redis from "redis";
/**
* This is the Queue constructor.
* It creates a new Queue that is persisted in Redis.
* Everytime the same queue is instantiated it tries to process all the old jobs that may exist from a previous unfinished session.
*/
function Bull(queueName: string, redisPort: number, redisHost: string, redisOpt?: Redis.ClientOpts): Bull.Queue;
namespace Bull {
export interface DoneCallback {
(error?: Error, value?: any): void
}
export interface Job {
jobId: string
/**
* The custom data passed when the job was created
*/
data: Object;
/**
* Report progress on a job
*/
progress(value: any): Promise<void>;
/**
* Removes a Job from the queue from all the lists where it may be included.
* @returns {Promise} A promise that resolves when the job is removed.
*/
remove(): Promise<void>;
/**
* Rerun a Job that has failed.
* @returns {Promise} A promise that resolves when the job is scheduled for retry.
*/
retry(): Promise<void>;
/**
* Returns a promise the resolves when the job has been finished.
* TODO: Add a watchdog to check if the job has finished periodically.
* since pubsub does not give any guarantees.
*/
finished(): Promise<void>;
}
export interface Backoff {
/**
* Backoff type, which can be either `fixed` or `exponential`
*/
type: string
/**
* Backoff delay, in milliseconds
*/
delay: number;
}
export interface AddOptions {
/**
* An amount of miliseconds to wait until this job can be processed.
* Note that for accurate delays, both server and clients should have their clocks synchronized
*/
delay?: number;
/**
* A number of attempts to retry if the job fails [optional]
*/
attempts?: number;
/**
* Backoff setting for automatic retries if the job fails
*/
backoff?: number | Backoff
/**
* A boolean which, if true, adds the job to the right
* of the queue instead of the left (default false)
*/
lifo?: boolean;
/**
* The number of milliseconds after which the job should be fail with a timeout error
*/
timeout?: number;
}
export interface Queue {
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* The done callback can be called with an Error instance, to signal that the job did not complete successfully,
* or with a result as second argument as second argument (e.g.: done(null, result);) when the job is successful.
* Errors will be passed as a second argument to the "failed" event;
* results, as a second argument to the "completed" event.
*
* concurrency: Bull will then call you handler in parallel respecting this max number.
*/
process(concurrency: number, callback: (job: Job, done: DoneCallback) => void): void;
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* The done callback can be called with an Error instance, to signal that the job did not complete successfully,
* or with a result as second argument as second argument (e.g.: done(null, result);) when the job is successful.
* 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;
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* A promise must be returned to signal job completion.
* 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.
*
* concurrency: Bull will then call you handler in parallel respecting this max number.
*/
process(concurrency: number, callback: (job: Job) => void): Promise<any>;
/**
* Defines a processing function for the jobs placed into a given Queue.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* A promise must be returned to signal job completion.
* 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): Promise<any>;
// process(callback: (job: Job, done?: DoneCallback) => void): Promise<any>;
/**
* 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: Object, opts?: AddOptions): Promise<Job>;
/**
* Returns a promise that resolves when the queue is paused.
* The pause is global, meaning that all workers in all queue instances for a given queue will be paused.
* A paused queue will not process new jobs until resumed,
* but current jobs being processed will continue until they are finalized.
*
* Pausing a queue that is already paused does nothing.
*/
pause(): Promise<void>;
/**
* Returns a promise that resolves when the queue is resumed after being paused.
* The resume is global, meaning that all workers in all queue instances for a given queue will be resumed.
*
* Resuming a queue that is not paused does nothing.
*/
resume(): Promise<void>;
/**
* Returns a promise that returns the number of jobs in the queue, waiting or paused.
* Since there may be other processes adding or processing jobs, this value may be true only for a very small amount of time.
*/
count(): Promise<number>;
/**
* Empties a queue deleting all the input lists and associated jobs.
*/
empty(): Promise<void>;
/**
* Closes the underlying redis client. Use this to perform a graceful shutdown.
*
* `close` can be called from anywhere, with one caveat:
* if called from within a job handler the queue won't close until after the job has been processed
*/
close(): Promise<void>;
/**
* 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: string): Promise<Job>;
/**
* Tells the queue remove all jobs created outside of a grace period in milliseconds.
* You can clean the jobs with the following states: completed, waiting, active, delayed, and failed.
*/
clean(gracePeriod: number, jobsState?: string): Promise<Job[]>;
/**
* Listens to queue events
* 'ready', 'error', 'activ', 'progress', 'completed', 'failed', 'paused', 'resumed', 'cleaned'
*/
on(eventName: string, callback: EventCallback): void;
}
interface EventCallback {
(...args: any[]): void
}
interface ReadyEventCallback extends EventCallback {
(): void;
}
interface ErrorEventCallback extends EventCallback {
(error: Error): void;
}
interface JobPromise {
/**
* Abort this job
*/
cancel(): void
}
interface ActiveEventCallback extends EventCallback {
(job: Job, jobPromise: JobPromise): void;
}
interface ProgressEventCallback extends EventCallback {
(job: Job, progress: any): void;
}
interface CompletedEventCallback extends EventCallback {
(job: Job, result: Object): void;
}
interface FailedEventCallback extends EventCallback {
(job: Job, error: Error): void;
}
interface PausedEventCallback extends EventCallback {
(): void;
}
interface ResumedEventCallback extends EventCallback {
(job?: Job): void;
}
/**
* @see clean() for details
*/
interface CleanedEventCallback extends EventCallback {
(jobs: Job[], type: string): void;
}
}
export = Bull;
}
declare module "bull/lib/priority-queue" {
import * as Bull from "bull";
import * as Redis from "redis";
/**
* This is the Queue constructor of priority queue.
*
* It works same a normal queue, with same function and parameters.
* The only difference is that the Queue#add() allow an options opts.priority
* that could take ["low", "normal", "medium", "hight", "critical"]. If no options provider, "normal" will be taken.
*
* The priority queue will process more often highter priority jobs than lower.
*/
function PQueue(queueName: string, redisPort: number, redisHost: string, redisOpt?: Redis.ClientOpts): PQueue.PriorityQueue;
namespace PQueue {
export interface AddOptions extends Bull.AddOptions {
/**
* "low", "normal", "medium", "high", "critical"
*/
priority?: string;
}
export interface PriorityQueue extends Bull.Queue {
/**
* 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: Object, opts?: PQueue.AddOptions): Promise<Bull.Job>;
}
}
export = PQueue;
}

View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [ "es6" ],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"baseUrl": "../../",
"typeRoots": [ "../../" ],
"types": [],
"paths": {
"bull": [ "bull/v2" ],
"bull/*": [ "bull/v2/*" ]
},
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"bull-tests.tsx"
]
}