Callbacks have to return Promise

This commit is contained in:
Christian Düfel
2018-11-29 00:53:29 +01:00
parent 28cad56c45
commit 7ea37092f0
2 changed files with 40 additions and 89 deletions
+7 -1
View File
@@ -98,11 +98,17 @@ const pdfQueue = new Queue('pdf transcoding', {
//
//////////////////////////////////////////////////////////////////////////////////
pdfQueue.process((job) => {
pdfQueue.process((job: Queue.Job) => {
// Processors can also return promises instead of using the done callback
return Promise.resolve();
});
async function pfdPromise(job: Queue.Job) {
return Promise.resolve();
}
pdfQueue.process(1, pfdPromise);
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
+33 -88
View File
@@ -12,6 +12,7 @@
// Alec Brunelle <https://github.com/aleccool213>
// Dan Manastireanu <https://github.com/danmana>
// Kjell-Morten Bratsberg Thorsen <https://github.com/kjellmorten>
// Christian D. <https://github.com/pc-jedi>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
@@ -342,50 +343,39 @@ declare namespace Bull {
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* If the callback signature contains the second optional done argument,
* the callback will be passed a done callback to be called after the job has been completed.
* 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<T>, done: DoneCallback) => void): void;
/**
* Defines a processing function for the jobs placed into a given Queue.
* or with a result 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.
*
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
* The callback can also be defined as the string path to a module
* exporting the callback function. Using a path has several advantages:
* - The process is sandboxed so if it crashes it does not affect the worker.
* - You can run blocking code without affecting the queue (jobs will not stall).
* - Much better utilization of multi-core CPUs.
* - Less connections to redis.
*
* A promise must be returned to signal job completion.
* If, however, the callback signature does not contain the done 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<T>) => void) | string): Promise<any>;
process(callback: ((job: Job<T>, done: DoneCallback) => void) | ((job: Job<T>) => Promise<any>) | string): 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 callback can also be defined as the string path to a module
* exporting the callback function. Using a path has several advantages:
* - The process is sandboxed so if it crashes it does not affect the worker.
* - You can run blocking code without affecting the queue (jobs will not stall).
* - Much better utilization of multi-core CPUs.
* - Less connections to redis.
*
* A promise must be returned to signal job completion.
* If the callback signature contains the second optional done argument,
* the callback will be passed a done callback to be called after the job has been completed.
* 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 (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.
*
* If, however, the callback signature does not contain the done 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.
*
* @param concurrency Bull will then call you handler in parallel respecting this max number.
* @param concurrency Bull will then call your handler in parallel respecting this maximum value.
*/
process(concurrency: number, callback: ((job: Job<T>) => void) | string): Promise<any>;
process(concurrency: number, callback: ((job: Job<T>, done: DoneCallback) => void) | ((job: Job<T>) => Promise<any>) | string): void;
/**
* Defines a processing function for the jobs placed into a given Queue.
@@ -393,35 +383,21 @@ declare namespace Bull {
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* If the callback signature contains the second optional done argument,
* the callback will be passed a done callback to be called after the job has been completed.
* 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.
* or with a result 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.
*
* @param concurrency Bull will then call you handler in parallel respecting this max number.
*/
process(concurrency: number, callback: (job: Job<T>, done: DoneCallback) => void): void;
/**
* Defines a named 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 callback can also be defined as the string path to a module
* exporting the callback function. Using a path has several advantages:
* - The process is sandboxed so if it crashes it does not affect the worker.
* - You can run blocking code without affecting the queue (jobs will not stall).
* - Much better utilization of multi-core CPUs.
* - Less connections to redis.
*
* A promise must be returned to signal job completion.
* If, however, the callback signature does not contain the done 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.
*
* @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<T>) => void) | string): Promise<any>;
process(name: string, callback: ((job: Job<T>, done: DoneCallback) => void) | ((job: Job<T>) => Promise<any>) | string): void;
/**
* Defines a processing function for the jobs placed into a given Queue.
@@ -429,52 +405,21 @@ declare namespace Bull {
* The callback is called everytime a job is placed in the queue.
* It is passed an instance of the job as first argument.
*
* If the callback signature contains the second optional done argument,
* the callback will be passed a done callback to be called after the job has been completed.
* 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.
* or with a result 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.
*
* @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<T>, done: DoneCallback) => void): void;
/**
* Defines a named 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 callback can also be defined as the string path to a module
* exporting the callback function. Using a path has several advantages:
* - The process is sandboxed so if it crashes it does not affect the worker.
* - You can run blocking code without affecting the queue (jobs will not stall).
* - Much better utilization of multi-core CPUs.
* - Less connections to redis.
*
* A promise must be returned to signal job completion.
* If, however, the callback signature does not contain the done 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.
*
* @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.
* @param concurrency Bull will then call your handler in parallel respecting this maximum value.
*/
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.
*
* 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.
*
* @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<T>, done: DoneCallback) => void): void;
process(name: string, concurrency: number, callback: ((job: Job<T>, done: DoneCallback) => void) | ((job: Job<T>) => Promise<any>) | string): void;
/**
* Creates a new job and adds it to the queue.