Class: Ixtlan::Core::Heartbeat

Inherits:
Object
  • Object
show all
Defined in:
lib/ixtlan/core/heartbeat.rb

Direct Known Subclasses

Heartbeat

Defined Under Namespace

Classes: Resource

Instance Method Summary collapse

Constructor Details

#initializeHeartbeat

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
# File 'lib/ixtlan/core/heartbeat.rb', line 33

def beat(resource = nil)
  resources = resource.nil? ? reg.values : [reg[resource]]
  resources.each do |res|
    last_update = res.local.maximum(:updated_at) || 2000.years.ago
    last_update = last_update.strftime('%Y-%m-%d %H:%M:%S.') + ("%06d" % last_update.usec)
    res.remote.get(:last_changes, :updated_at => last_update).each do |remote|
      id = remote.delete('id')
      u = res.local.find_by_id(id)
      if u
        u.update_attributes(remote)
      else
        u = res.local.new(remote)
        u.id = id
      end
      if u.save
        res.count = res.count + 1
      else
        res.failures = res.failures + 1
      end
    end
  end
end

#regObject



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_logObject Also known as: to_s



55
56
57
# File 'lib/ixtlan/core/heartbeat.rb', line 55

def to_log
  reg.collect { |r| r.to_log }.join('\n')
end