From 3659cf3831328d49417de0e36e5438374cc7613b Mon Sep 17 00:00:00 2001 From: Akshat Khandelwal Date: Sat, 13 Apr 2024 15:54:42 +0530 Subject: [PATCH] feat(@nestjs/bull): Allow overriding the onApplicationShutdown behaviour Allows customising the onApplicationShutdown behaviour by providing a callback in the `BullModuleOptions`. For eg. if someone doesn't want to `close()` the queues instead just `pause(true)` them Resolves #2069 --- packages/bull/lib/bull.providers.ts | 4 +++- .../lib/interfaces/bull-module-options.interface.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/bull/lib/bull.providers.ts b/packages/bull/lib/bull.providers.ts index cbcb553a..df7642b5 100644 --- a/packages/bull/lib/bull.providers.ts +++ b/packages/bull/lib/bull.providers.ts @@ -43,7 +43,9 @@ function buildQueue(options: BullModuleOptions): Queue { (queue as unknown as OnApplicationShutdown).onApplicationShutdown = function ( this: Queue, ) { - return this.close(); + return options.onApplicationShutdown + ? options.onApplicationShutdown(this) + : this.close(); }; return queue; } diff --git a/packages/bull/lib/interfaces/bull-module-options.interface.ts b/packages/bull/lib/interfaces/bull-module-options.interface.ts index 783febd7..79b4639a 100644 --- a/packages/bull/lib/interfaces/bull-module-options.interface.ts +++ b/packages/bull/lib/interfaces/bull-module-options.interface.ts @@ -28,6 +28,12 @@ export interface BullModuleOptions extends BullRootModuleOptions { * Additional queue processors */ processors?: BullQueueProcessor[]; + + /** + * Callback for application shutdown, for eg. to do `queue.pause(true)` to pause processing + * The default behavior is `queue.close()` + */ + onApplicationShutdown?: (queue: Bull.Queue) => Promise; } export interface BullOptionsFactory { @@ -48,6 +54,12 @@ export interface BullModuleAsyncOptions */ configKey?: string; + /** + * Callback for application shutdown, for eg. to do `queue.pause(true)` to pause processing + * The default behavior is `queue.close()` + */ + onApplicationShutdown?: (queue: Bull.Queue) => Promise; + /** * Existing Provider to be used. */