Sha256: 086654efd0df37475045b0d437688dfd3959a5d379b8354f533d683f86668343

Contents?: true

Size: 1.66 KB

Versions: 4

Compression:

Stored size: 1.66 KB

Contents

module Lita
  module Handlers
    class Enhance < Handler
      class Node
        attr_accessor :name, :dc, :environment, :fqdn, :last_seen_at

        # Creates a new Node instance and loads its data from Redis
        def self.load(redis, name)
          node = nil

          node_data_json = redis.hget('nodes', name)
          if node_data_json
            node_data = JSON.parse(node_data_json)
            node = self.from_json(node_data)
          end

          node
        end

        # Stores this node in Redis for later retrieval
        def store!(redis)
          node_data_json = JSON.generate(self.as_json)
          redis.hset('nodes', self.name, node_data_json)
        end

        def self.from_json(json)
          self.new.tap do |node|
            %w(name dc environment fqdn).each do |field|
              node.send("#{field}=", json[field])
            end
            node.last_seen_at = Time.parse(json['last_seen_at']) if json['last_seen_at']
          end
        end

        def as_json
          {name: name, dc: dc, environment: environment, fqdn: fqdn, last_seen_at: last_seen_at}
        end

        def render(level)
          case level
          when 1 then name
          when 2 then "#{name} (#{dc})"
          when 3 then "#{name} (#{dc}, #{environment})"
          when 4 then "#{name} (#{dc}, #{environment}, last seen #{last_seen_at})"
          when 5 then "#{fqdn} (#{dc}, #{environment}, last seen #{last_seen_at})"
          end
        end

        # True if this node appears to be gone away because we haven't seen it
        # for a while.
        def old?
          last_seen_at < (Time.now - 6 * 60 * 60)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lita-enhance-0.9.3 lib/lita/handlers/enhance/node.rb
lita-enhance-0.9.2 lib/lita/handlers/enhance/node.rb
lita-enhance-0.9.1 lib/lita/handlers/enhance/node.rb
lita-enhance-0.9.0 lib/lita/handlers/enhance/node.rb