I have a controller on nestjs that is writing a job to the queue.
The job processing suppose to last between 100MS to 5000MS.
I don't want to disconnect the client until I have the answer.
How can I do that and not running a while loop? write now I have this very bad code:
@Post('job')
async job(@Body() request): Promise<JobRes>{
const job = await this.bullQueue.add('hande', request);
while(true){ //UGLY AND BAD
const state = await job.getState();
if(state == 'completed'){
return await this.db.findByJobId(job.id);
}
await sleet(1_000);
}
}
This solution require a lot of resources from the event loop and is not safe at all.
I'm looking for a better solution to hand the request untill I have response.
Thanks