Reputation: 193
I have a simple resque job that accepts a model id, fetches it, and then calls a method on the fetched item.
class CupcakeWorker
@queue = :cupcake_queue
def self.perform(cupcake_id)
@cupcake = Cupcake.find(cupcake_id)
p @cupcake
end
end
I queue it from Cupcake's model, after_commit using the 'enqueue' method
def bake
Resque.enqueue(CupcakeWorker, self.id)
end
The job queues and executes correctly . However if I modify the record's data in the database or through the UI and proceded to queue the job again the operation doesn't execute using the new values.
I can only get it to fetch the new values if I restart the resque worker process. Is resque caching the object? Is there a way to ensure it refetches from the database every time the worker is called?
I even pulled the query logs... the commit happens before the select query which fetches the Cupcake.. So this is neither a race condition coz of the order, nor a caching problem as a query fetches it... Am clueless guys.. what could it be? Btw am using the after_commit gem in rails 2.3.8 and redis n resque combo
Upvotes: 2
Views: 842
Reputation: 11
I ran into problem with exactly same symptoms
After digging deep i have found, the my problem was in named scopes
i have something like this in perform:
letter = Letter.to_process.first
and in model i have
scope :to_process, where("scheduled_at < ?", Time.now).where('finished_at IS NULL').order('scheduled_at')
which is wrong as it runs all the time with the same value of scheduled_at as in the first execution of the named scope
this was correct
scope :to_process, lambda { where("scheduled_at < ?", Time.now).where('finished_at IS NULL').order('scheduled_at') }
if this is not related by any means to your case - if the you have problem with just the plain find(id), try to add this
Cupcake.connection.clear_query_cache
at the end of your perform method
Upvotes: 0