1

I create a simple queue-job system with using BullQueue in TypeScript and NestJS like below:

    async addToQueue(): Promise<void> {
        try {
            await this.reportQueue.add('generate_report', {
                //some data
            })
            this.logger.log(`Added to queue.`)
        } catch (error) {
            this.logger.error(`Not added to Queue.`);
        }
    }


    @Process('generate_report')
    async generateReport(job: Job<{options: ReportFilterDto, context: CustomContext, reportType: Type, worktime?: WorktimeDto}>): Promise<any> {
        try {
            //some working code
        } catch (error) {
            //
        }
    }

    @OnQueueActive()
    async onActive(job: Job): Promise<void> {
        //
    }

    @OnQueueCompleted()
    async onCompleted(job: Job): Promise<void> {
        //
    }

    @OnQueueWaiting()
    async onWaiting(job: Job): Promise<void> {
        //
    }

    @OnQueueFailed()
    onError(job: Job<any>, error: BadRequestException): void {
        //
    }

i run my function addToQueue() from my controller where i provide a parameters, buuuuuut it is possible to return any response to the client from the queue about sucessfull or failed job? If it is possible can someone show how to do it?

thanks for any help

.////////////////////////.

  • Your question is a bit unclear but depending on what you're trying to do, consider [`await job.finished()`](https://stackoverflow.com/a/67609723/114558) – rinogo Nov 10 '21 at 19:13

1 Answers1

0

Immediately, there isn't really any way. A queue is there so you can run asynchronous tasks without blocking your main thread or holding up your server in any way. If you were to want to return the result, depending on the job the queue is running, you could be waiting for a few seconds to a few days (a bit of an exaggeration, but it gets the point across). What you could do instead is one of two things (at least that come immediately to mind)

  1. return an Id that you create that relates back to this job. When the job finishes, save the result with that Id to the database. Then, the user can request the status at any time.

  2. Take note of the user who sent in the request, and send an email to that user once the job completes. This will require more integrations, but can get more accurate results over "Ping every so often and we'll tell you if it's done".

Both approaches are valid, and you could even make a combination of the two (probably the best of both worlds so you can save the results and alert the user when it's done).

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • `await job.finished()` would work, wouldn't it? Of course, whether or not it's a "good idea" depends on the use case. – rinogo Nov 10 '21 at 19:14
  • Probably. Wasn't really aware that was a thing – Jay McDoniel Nov 10 '21 at 19:21
  • 1
    Neither was I - I found a few very obscure references to it, tried it out, and it seems to work just as expected! It works for both successes and failures. One of the big drawbacks I see is that I'd guess that if a Job stalls, it will happily continue waiting as long as Bull keeps the Job active. So, you'd want to make sure to appropriately set the Job/Queue's timeout or else you could exhaust resources on the webserver pretty quickly. – rinogo Nov 10 '21 at 19:31