Sha256: f6d3303f577f857f07503711b5d0f262218e52034286a236603f5ea355207135

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require "zk"
module Shatter
  module Service
    class Discovery
      class << self
        def connection_pool
          @connection_pool ||= ZK::Pool::Bounded.new(Shatter::Config.zookeeper_host)
        end

        def deregister_service(service_url)
          zk = connection_pool.checkout
          key = Shatter::Util.instances_key
          zk.delete("#{key}/#{service_url}") if zk.exists?("#{key}/#{service_url}")
          connection_pool.checkin(zk)
        end

        def register_service(service_url)
          Shatter.logger.info "Registering #{service_url} to zookeeper"
          zk = connection_pool.checkout
          key = Shatter::Util.instances_key
          unless zk.exists?("#{key}/#{service_url}")
            created = zk.create("#{key}/#{service_url}")
            Shatter.logger.info "Registered #{created}"
          end
          connection_pool.checkin(zk)
        end

        def service_instance_url
          service_instance_urls.sample
        end

        def service_instance_urls
          zk = connection_pool.checkout
          urls = zk.children(Shatter::Util.instances_key)
          connection_pool.checkin(zk)
          urls
        end

        def populate_result_location(uuid)
          zk = connection_pool.checkout
          zk.create(Util.zookeeper_response_key(uuid), my_host)
          connection_pool.checkin(zk)
        end

        def my_host
          my_ip = ENV["HOST_NAME"] || "localhost"
          "#{my_ip}:#{Shatter::Config.service_port}"
        end

        def service_instance_url_for_uuid(uuid)
          key = Util.zookeeper_response_key(uuid)
          zk = connection_pool.checkout
          druby_instance_url = zk.get(key)[0] if zk.exists?(key)
          connection_pool.checkin(zk)
          druby_instance_url
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shatter-rb-0.1.1 lib/shatter/service/discovery.rb
shatter-rb-0.1.0 lib/shatter/service/discovery.rb