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.
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.