ssut / nestjs-sqs

A project to make SQS easier to use within NestJS, with decorator-based handling and seamless NestJS-way integration.
MIT License
214 stars 53 forks source link

Handling sqs-consumer stopped event? #82

Closed rbonestell closed 5 months ago

rbonestell commented 5 months ago

I'm looking for the most effective way to gracefully handle the NestJS application lifecycle shutdown event, utilizing nestjs-sqs, while allowing my Nest app to conclude it's operation on the most recently received SQS message (often these operations are not atomic, and tend to be long-running).

I discovered the underlying sqs-consumer library [emits a stopped event]( ) from the private waitForPollingToComplete() function once it has completed after stop() has been called on it.

I would like to take advantage of this event in the OnModuleDestroy or OnApplicationShutdown functions, perhaps by introducing a promise which resolves upon receiving the event such as:

public async onModuleDestroy() {
    await new Promise(resolve => consumer.once('stopped', resolve));
}

Would there be any reason not to want this built-in to the nestjs-sqs library and enabled with a options flag? I do understand there's additional complexity due to the library managing multiple consumers.

rbonestell commented 5 months ago

It only took one mere moment of looking back at the docs and my own existing code to realize I actually already have this stubbed out...

@SqsConsumerEventHandler('jobs.inbound', 'stopped')

Closing this issue and will chalk this up to the rubber ducky debugging process. Cheers!