tsedio / tsed

:triangular_ruler: Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone. ⭐️ Star to support our work!
https://tsed.io/
MIT License
2.86k stars 284 forks source link

Run BullMQ workers $onReady? #2764

Open denis-maiorov-brightsec opened 3 months ago

denis-maiorov-brightsec commented 3 months ago

Is your feature request related to a problem? Please describe.

Default configuration of BullMQ workers uses autorun: true and as a result the worker runs before async providers are built that establish db connections, etc. So, any waiting to be executed job fails during startup as the server is not yet properly initialized https://docs.bullmq.io/guide/workers

Describe the solution you'd like

Not sure what is the best practices there but I feel that framework should handle this. Http server is not listening before all the routes, hooks and providers are built so the same behavior should be regarding processing jobs from queues

Describe alternatives you've considered

Solution today is to disable autorun through defaultWorkerOptions Manually inject workers

@Inject(BullMQTypes.WORKER) private readonly workers: Worker[]

and then do in $afterInit hook

// do not await run as it never resolves
this.workers.forEach(worker => worker.run());

Additional context

No response

Acceptance criteria

No response

Romakita commented 3 months ago

Hello @denis-maiorov-brightsec Maybe delay job startup at the approriate hook will be the solution as you suggested ;)

Let's go for a small pr !