Hari
Hari

Reputation: 193

Resque caching active record cal

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?

Edit:

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

Answers (2)

pigster
pigster

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

boulder
boulder

Reputation: 3266

You can try and force resque hit the database with this code

ActiveRecord::Base.uncached do
  @cupcake = Cupcake.find(cupcake_id)
end

uncached is available in 2.3.8

This should work, unless the problem is not really a caching issue. Let us know if it works for you.

Upvotes: 0

Related Questions