Sha256: a62ba8333b734eeeae43880394128f42cc7f691dc971535ea757c0dfe27c8bda

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 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|
        u = res.local.find_by_id(remote.delete('id'))
        u.update_attributes(res.remote)
        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.2 lib/ixtlan/core/heartbeat.rb~
resty-generators-0.7.1 lib/ixtlan/core/heartbeat.rb