views:

51

answers:

1

I'm trying to implement a comet approach in my Rails application. I have the following:

def poll
  records = []
  start_time = Time.now.to_i

  while records.length == 0 do
    records = Something.find(:all,
        :conditions => { :some_condition => false})

    if records.length > 0
      break
    end

    sleep 1

    if Time.now.to_i - start_time >= 10
      break
    end
  end

  responseData = []

  records.each do |record|
    responseData << {
      'something' => record.some_value
    }

    # Flag message as received.
    record.some_condition = true
    record.save
  end

  render :text => responseData.to_json
end

Now, when I go to the URL manually, it sits there and waits for 10 seconds and times out as expected. If I modify my database so that the Something.find() returns records and then go to the URL again, the call returns immediately.

However, if I go to the URL, and then I quickly run an update on the database such that the Something.find() should find records, it just sits there until it times out after 10 seconds. I would expect that it should see the change to the database right after I make the change and return.

Any ideas why? I'm also open to suggestions on improvements.

+3  A: 

This is because your query is cached by default. Try this:

records = Something.uncached{Something.find(:all,
            :conditions => { :some_condition => false})}

Reference: Article 1

KandadaBoggu
Sweet! It works. Thanks.
Chad Johnson