gaffneyc / sunspot-queue

Background search indexing using existing worker systems
MIT License
35 stars 37 forks source link

Add delayed enqueue for workers that support it #15

Open gaffneyc opened 11 years ago

gaffneyc commented 11 years ago

A couple forks have added delay support. I don't fully understand the reasoning or what is causing the need for it. My best assumption is that the worker system is pulling the index job off the queue before the record can be saved to the database.

vinniefranco commented 11 years ago

Hi @gaffneyc

The reason I added it was to deal with a race condition I was having between my services. I would have a 99.5% miss with a index job on it's first try, since the job would try to run before the record actually existed. Since indexing was not something that needed to be async, I added the functionality to have it as a scheduled job with a configurable delay.

gaffneyc commented 11 years ago

@vinniefranco that's interesting, I'm curious what could be causing the index job to be enqueued before the record exists in the database. Sunspot should be calling index during after_commit and we do a check to make sure the record has been persisted.

Was this with ActiveRecord and a single master database? Was the record being saved through an association?

I think adding a delay is the quickest and simplest approach but I would like to understand that root cause if possible.

gaffneyc commented 11 years ago

Actually... it looks like the index is being done in the after_save callback and not after_commit.

vinniefranco commented 11 years ago

Yeah, I figured as much since it had a id passed and yes, you are correct the record was being saved through an association. Single master

xinuc commented 11 years ago

Yup. Pretty much what I've experienced.

The worker would run the index before the actual record saved. Usually, I just need 1 sec delay to get around this problem.

quake commented 11 years ago

I'm using delay_for to resolve race condition issue: sunspot is using after_save callback, index job will start before db commit sometimes.

coorasse commented 10 years ago

Same problem here with Sidekiq. The job starts always before the object is saved. I can't understand if there is a reason not to put the callback in after_commit instead of after_save callback

britg commented 10 years ago

I'll throw my hat in - race conditions started happening after a added additional 'after_save' callbacks to my model. Near 100% lookup miss on first try.

ghempton commented 10 years ago

Hopefully if sunspot/sunspot#534 gets merged this problem will go away.