Sha256: bb3b2701bfc67751bb35d118039393064a24e110edb1f33d8e44fa2ad6432977

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

class Ixtlan::Core::Heartbeat

  class Resource

    attr_reader :local, :remote
    attr_accessor :count, :failure

    def initialize(local, remote)
      @count = 0
      @failures = 0
      @local = local
      @remote = remote
    end

    def to_log
      "update #{@local} - total: #{@count + @failures}  success: #{@count}  failures: #{@failures}"
    end
  end

  def initialize
    @count = 0
    @failures = 0
  end

  def reg
    @reg ||= {}
  end

  def register(local, remote)
    reg[local] = Resource.new(local, remote)
  end

  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
  def to_log
    reg.collect { |r| r.to_log }.join('\n')
  end
  alias :to_s :to_log
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
resty-generators-0.7.3 lib/ixtlan/core/heartbeat.rb~
resty-generators-0.7.2 lib/ixtlan/core/heartbeat.rb