jwarzech
jwarzech

Reputation: 6665

Resque caching active record call

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)
    @cupcake.bake
  end
end

I queue it from a controller action using the 'enqueue' method

def bake
  Resque.enqueue(CupcakeWorker, params[:cupcake_id])
  render :json => 'Baking...'
end

The job queues and executes correctly. However if I modify the record's data in the database 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?

Upvotes: 4

Views: 453

Answers (1)

Daniel Berkompas
Daniel Berkompas

Reputation: 349

This answer is a little crude, but could work. You could call reload on the ActiveRecord model before doing any processing. This forces ActiveRecord to update the data.

The worker would then look like this:

class CupcakeWorker
  @queue = :cupcake_queue
  def self.perform(cupcake_id)
    @cupcake = Cupcake.find(cupcake_id).reload
    @cupcake.bake
  end
end

I'm afraid I don't know why Resque might be doing this, however. Try Sidekiq and see if you get better results.

Upvotes: 0

Related Questions