OptimalBits / bull

Premium Queue package for handling distributed jobs and messages in NodeJS.
Other
15.59k stars 1.43k forks source link

The job returned by Queue.add() is not updated by progress() #1900

Open oxabz opened 4 years ago

oxabz commented 4 years ago

Summary

The progress value of the Job returned by Queue.add() isn't updated. It's a surprising behavior considering that the Job objects returned by the events are. Is it an intended behavior? If it is it should probably be added in the reference documentation

Reproduce / Test

When executing this program :

const Queue = require('bull')

let returnedJob = null
let activeJob = null

let queue = new Queue('test');
queue.process(async (_job, done) => {
    await new Promise((resolve, reject) => {
        let progress = 0
        let i = setInterval(async () => {
            progress += 1;
            await _job.progress(progress)
            if(progress == 100){
                clearInterval(i)
                resolve()
            }
        }, 100)
    })
    done()
})
queue.on('active', (job)=>{
    activeJob = job
})
queue.on('progress', async (_job) => {
    console.log('event_job : ' + await _job.progress())
    console.log('active_job : ' + await activeJob.progress())
    console.log('returned_job : ' + await returnedJob.progress())
})
queue.add({}).then((_job) => { returnedJob = _job})

We obtain the following traces :

...
event_job : 97
active_job : 97
returned_job : 0
event_job : 98
active_job : 98
returned_job : 0
event_job : 99
active_job : 99
returned_job : 0
event_job : 100
active_job : 100
returned_job : 0

Version

manast commented 4 years ago

I marked it as an enhancement.

aalex commented 3 years ago

I confirm this issue.

manast commented 3 years ago

Although I agree it is an inconsistent behaviour there are several workarounds for this so it is not very high prioritized.

pascalprat commented 3 years ago

What are the workarounds? Retrieving the job by using Queue.getJob() before sending the progress event doesn't seem to work either.

manast commented 3 years ago

@pascalprat you should not need any workarounds. The progress is reported as an event and that is they way it should be handled in your code. In a distributed system you cannot just rely on a particular instantiation of a job class to handle the progress reports. If you write your code listening to the events and reporting that to the user it would be the more robust and scalable way to do it.