Automattic / kue

Kue is a priority job queue backed by redis, built for node.js.
http://automattic.github.io/kue
MIT License
9.46k stars 867 forks source link

Job on complete event #1117

Open sarincasm opened 7 years ago

sarincasm commented 7 years ago

When fetching a job like this -

kue.Job.get(someId, (err, job) => {
    job.on('complete', () => { // never called
        debug('on get')
        // do stuff after job is complete
    })
    job.inactive()
})

here this event handler is never called, probably because kue.Job.get doesn't return an object that has that listener. Is there a way to listen to job events when fetching events like this ? or a way to listen to complete event for a particular job (by job id)

This works fine, but doesn't serve the purpose -

const job = queue.create('name', {
    title
})
.delay(1200000)
.attempts(3)
.save(err => {
    if(err)
        return
    // save job.id
})
job.on('complete', () => {
    debug('on create')
})
CQBinh commented 6 years ago

Did you save() the job before fetch it from kue server? Is yes, let log the (error, job) when you receive response from fetch.

Let try this approach:

let someId
const job = queue.create('name', {title})
.delay(1200000)
.attempts(3)
.save(err => {
  if(err)
    return
  someId = job.id
})

kue.Job.get(someId, (err, job) => {
console.log('error', err)
console.log('job', job)
  job.on('complete', () => { 
    debug('on get')
    // do stuff after job is complete
  })
  job.inactive()
})
sarincasm commented 6 years ago

hi @CQBinh .. how is your code different ? That is what i have done, but the job.on('complete') listener is never called.

CQBinh commented 6 years ago

@eelsweb First, I guess that you forget to save() the job after call: queue.create().

Btw: let post your full code with log from kue.Job.get() calling, so I can look deeper.

expresstechsoftwares commented 6 years ago

Does anyone know how to listen for when all jobs in a queue are completed?

CQBinh commented 6 years ago

@expresstechsoftwares https://github.com/Automattic/kue#queue-events

vernk commented 6 years ago

Anyone solve this?