I'm currently working on a Nestjs app that relies on a mix of Bull queues, scheduled tasks (via cron) and various async generator loops for iterating among fetched items from an external API. These features are all working rather nicely together but I can't seem to make a graceful shutdown work as expected. The main problem boils down to, whenever the app is shutting down, despite the TypeORM datasource having been configured with keepConnectionAlive: true, when the queues are closing and any of their corresponding event listeners are then called (e.g.: onQueueCompleted, onQueueError, onQueueFailed), where some housekeeping chores need to be performed against the database, I inevitably get a TypeORMError: Driver not Connected exception on the console, barring any of these chores from being performed, ultimately leaving some data behind in an inconsistent state.
I've been looking around on the interwebs for almost two days now, but I couldn't find any definitive solution to this problem. I hope someone here can help me figure this out.