kue icon indicating copy to clipboard operation
kue copied to clipboard

In master, jobs are executed more than once if they have > 1 attempt

Open ItalyPaleAle opened this issue 11 years ago • 17 comments

One of the last commits in master broke something, and my jobs are executed more than once. It seems to be a race condition, because this happens randomly:

jobs.process('email', 20, function(job, done) {
    console.log('start')

    // Do something async
    setTimeout(function() {
        console.log('done')
    }, 1000)
})

Jobs of type email have all 2 possible attempts.

Randomly, in my console I either see the correct output (one "start" and then one "end), or

start
start
done
done

Seems like there's a race condition somewhere, so jobs are not marked as in progress before they're started again.

ItalyPaleAle avatar Apr 23 '15 18:04 ItalyPaleAle

Actually, this happens even if the number of attempts is just 1.

In the UI, I often see things like attempts: 2/1, attempts: 5/3

ItalyPaleAle avatar Apr 23 '15 18:04 ItalyPaleAle

this is related to ttl feature, will be fixed by next commit

behrad avatar Apr 23 '15 23:04 behrad

should be fixed

behrad avatar Apr 24 '15 13:04 behrad

Master is still giving me this issue!

ItalyPaleAle avatar Apr 28 '15 15:04 ItalyPaleAle

are you sure? provide a log based on job Id. Does the above code generates this?

behrad avatar Apr 28 '15 16:04 behrad

My bad, my npm-shrinkwrap wasn't updated. Sorry for this

ItalyPaleAle avatar Apr 28 '15 17:04 ItalyPaleAle

No, actually, the error is still there, after a while.

I don't know why on my tests it wasn't showing up, but now I get 2 emails delivered at the same time, again. On the dashboard, I see the job marked as complete, with "3/2" attempts

ItalyPaleAle avatar Apr 28 '15 17:04 ItalyPaleAle

wiered... it shouldn't happen.

behrad avatar Apr 29 '15 07:04 behrad

I've kept updating the code with the last one in master, and I still get this. My jobs fail with "TTL exceeded", even though I didn't set any TTL. I'm using the latest revision in master.

ItalyPaleAle avatar May 04 '15 20:05 ItalyPaleAle

Also, setting a TTL to my jobs (for example, two hours), is simply ignored.

It may help to tell that these jobs are quite "long" (they take ~2 seconds to run).

ItalyPaleAle avatar May 04 '15 20:05 ItalyPaleAle

Here's a test code that demonstrates the issue, running with Kue revision #6f6db5f79fb0e5c7fce4d8e6acba4f137f53a2e9 .

I'm using OSX 10.10, redis-server 2.8.17 (last version available on MacPorts) and Node 0.12.2. I've re-installed all my npm packages so I'm sure kue is installed correctly.

Test code:

'use strict';

let kue = require('kue')
let queue = kue.createQueue({
    redis: {
        port: 6379,
        host: 'localhost',
        db: 8
    }
})

// Create the auto-signout job
let job = queue.create('long-job', {title: 'long job'})
job.priority('low')
job.attempts(3)
job.searchKeys(['title'])
job.save(function(err) {
    if(err) {
        throw new Error('Cannot create job: ' + err.toString())
    }   
})

queue.process('long-job', function(job, done) {
    console.log('start')

    setTimeout(function() {
        console.log('done')
        done(null)
    }, 5000)
})

Output:

$ node --harmony test-kue.js 
start
start
start
done
done
done

// EDIT Updated to Redis 3.0.0 and still not working.

ItalyPaleAle avatar May 04 '15 20:05 ItalyPaleAle

I've kept updating the code with the last one in master, and I still get this. My jobs fail with "TTL exceeded", even though I didn't set any TTL. I'm using the latest revision in master.

I get this error as well.

andrewtamura avatar May 05 '15 01:05 andrewtamura

thank you @EgoAleSum for the test case, will run and let you know

behrad avatar May 05 '15 06:05 behrad

this was a critical BUG, my bad! thank you guys for reporting and improving kue :+1:

behrad avatar May 05 '15 07:05 behrad

Thanks for the fix!

ItalyPaleAle avatar May 05 '15 13:05 ItalyPaleAle

Hey, I'm having a similar issue, so I updated to 0.9.1 in an attempt to fix the issue, but it dosn't look like it worked for me.

I am using the cluster module, and after doing some logging, I am seeing the same job.id being handled by multiple process.pid's. Could this be related? Thanks!

UPDATE: In my case, I have one job that ends up creating a lot of other jobs, and delays each one out by a second more than the last to spread their execution out. During some more testing, it seems like jobs will only be picked up by multiple workers if I'm using the delay feature. Removing the delay seems to fix this particular issue, although it's not suitable for my actual application.

schnie avatar May 05 '15 20:05 schnie

@schnie I think this should be related to #449 and #440 and #443. I've improved delayed job promotion in 0.9, but your claims shows there still exists a race when promoting jobs, which should be here: https://github.com/Automattic/kue/blob/master/lib/kue.js#L188 I think unlock should be called pessimistically after all jobs inactive are done.

behrad avatar May 06 '15 06:05 behrad