Class: Ixtlan::Core::Heartbeat
- Inherits:
-
Object
- Object
- Ixtlan::Core::Heartbeat
- Defined in:
- lib/ixtlan/core/heartbeat.rb
Defined Under Namespace
Classes: Resource
Instance Method Summary collapse
- #beat(resource = nil) ⇒ Object
-
#initialize ⇒ Heartbeat
constructor
A new instance of Heartbeat.
- #reg ⇒ Object
- #register(local, remote) ⇒ Object
- #to_log ⇒ Object (also: #to_s)
Constructor Details
#initialize ⇒ Heartbeat
Returns a new instance of Heartbeat.
20 21 22 23 |
# File 'lib/ixtlan/core/heartbeat.rb', line 20 def initialize @count = 0 @failures = 0 end |
Instance Method Details
#beat(resource = nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ixtlan/core/heartbeat.rb', line 33 def beat(resource = nil) resources = resource.nil? ? reg.values : [reg[resource]] resources.each do |res| last_date = res.local.maximum(:updated_at) || 2000.years.ago last_update = last_date.strftime('%Y-%m-%d %H:%M:%S.') + ("%06d" % last_date.usec) res.remote.get(:last_changes, :updated_at => last_update).each do |remote| id = remote.delete('id') u = res.local.find_by_id(id) result = if u u.update_attributes(remote) else u = res.local.new(remote) u.id = id u.save end if result res.count = res.count + 1 else res.failures = res.failures + 1 end end end end |
#reg ⇒ Object
25 26 27 |
# File 'lib/ixtlan/core/heartbeat.rb', line 25 def reg @reg ||= {} end |
#register(local, remote) ⇒ Object
29 30 31 |
# File 'lib/ixtlan/core/heartbeat.rb', line 29 def register(local, remote) reg[local] = Resource.new(local, remote) end |
#to_log ⇒ Object Also known as: to_s
56 57 58 |
# File 'lib/ixtlan/core/heartbeat.rb', line 56 def to_log reg.values.collect { |r| r.to_log }.join("\n\t") end |