0

I need to make a XML request every minute, and then store the data in a Medida object. Every Meter object has many Medidas (measurements).

class Meter < ActiveRecord::Base

  has_one :user_type
  has_one :user
  has_many :medidas
  attr_accessible :user_type_id, :user_id, :address, :etc


  def read_data
      u = User.find(self.user_id)
      Medida.get_data(self.address,u.user_key, self.id) 
  end


  class << self
    def all_read_data
      meters = Meter.all
      meters.each do |meter|
        meter.read_data
      end  
    end
  end
end

Medidas.rb looks like

class Medida < ActiveRecord::Base
  belongs_to :meter
  attr_accessible  :some_attr

  class << self
    def get_data(address,user_key, meter_id)
      url="some_url_with#{variables}.xml?#{a_key}"

      response = HTTParty.get(url)

      hash = Hash.from_xml(response.to_s)
      hash = Medida.some_funct(hash)
      data = hash["ekmMeterReads"]["meter"]["read"]

      #Save hash to bd
    end
    #def some_funct
  end
end

I'm using Whenever for this. And this is how my schedule.rb looks like

set :output, "#{path}/log/cron.log"

every 1.minutes do
    runner "Meter.all_read_data, :environment => 'development'
end

The problem is that actually only one Meter gets its data every minute and all the others are left without it. This is the content for my cron.log (it has an issue, but I have failed to find useful information about it.)

What am I missing? Thanks for any insight.

Sebastialonso
  • 1,437
  • 18
  • 34
  • If you run `Meter.all_read_data` in rails console do you get that error? – Tom L Feb 12 '13 at 21:56
  • It manages to save only for one Meter (the same one), and then, retrieves a user (meaning is getting into `read_data`) and then fails with this error (the same one from the cron.log): `EOFError: end of file reached` – Sebastialonso Feb 13 '13 at 12:43

1 Answers1

0

I got it. The url I'd been given to make the call was the wrong one. Everything working smoothly now.

If you depend on someone else's information (like codes, keys, urls) be sure to be extra inquisitive about that information. Is better to lose some minutes doing that, than a day trying to do something which you are not supposed to do.

Sebastialonso
  • 1,437
  • 18
  • 34