1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/core/model.rb
Chris Frederick 105f6a81d8 Fog::Model#wait_for: eliminate inner retry loop
AWS frequently takes more than 3 seconds for an instance to appear.
Rather than having the overlapping timeouts for reload and
instance_eval, simply make them both occur with the same timeout.

 + Prevents spurious failures when waiting for AMI
 + Easier to follow flow
2013-03-25 15:05:07 -07:00

70 lines
1.9 KiB
Ruby

module Fog
class Model
extend Fog::Attributes::ClassMethods
include Fog::Attributes::InstanceMethods
attr_accessor :collection, :connection
def initialize(new_attributes = {})
merge_attributes(new_attributes)
end
def inspect
Thread.current[:formatador] ||= Formatador.new
data = "#{Thread.current[:formatador].indentation}<#{self.class.name}"
Thread.current[:formatador].indent do
unless self.class.attributes.empty?
data << "\n#{Thread.current[:formatador].indentation}"
data << self.class.attributes.map {|attribute| "#{attribute}=#{send(attribute).inspect}"}.join(",\n#{Thread.current[:formatador].indentation}")
end
end
data << "\n#{Thread.current[:formatador].indentation}>"
data
end
def reload
requires :identity
return unless data = begin
collection.get(identity)
rescue Excon::Errors::SocketError
nil
end
new_attributes = data.attributes
merge_attributes(new_attributes)
self
end
def to_json(options = {})
Fog::JSON.encode(attributes)
end
def symbolize_keys(hash)
return nil if hash.nil?
hash.inject({}) do |options, (key, value)|
options[(key.to_sym rescue key) || key] = value
options
end
end
def wait_for(timeout=Fog.timeout, interval=1, &block)
reload_has_succeeded = false
duration = Fog.wait_for(timeout, interval) do # Note that duration = false if it times out
if reload
reload_has_succeeded = true
instance_eval(&block)
else
false
end
end
if reload_has_succeeded
return duration # false if timeout; otherwise {:duration => elapsed time }
else
raise Fog::Errors::Error.new("Reload failed, #{self.class} #{self.identity} not present.")
end
end
end
end