Sha256: d23d902e9788b0f17e104142a3191ed39e717e48528ab4f5efcd21689d9c161e

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# Services::Service
#  this is the almalgamate service class which allows you to load a service
#  and it's endpoint/members
#
# TODO: Allow arbitrary entity loading.
#
module Services
  require_relative 'connection'
  require_relative 'endpoint'
  require_relative 'member'

  # service container
  class Service
    attr_reader :name
    attr_reader :members
    attr_reader :endpoint

    def initialize(name)
      @name = name
      @members = []
      @endpoint = Services::Endpoint.new name

      create_if_missing
      load_members
      load_endpoint
    end

    private

    def create_if_missing
      Services.set "#{KEY}/#{name}/_created",
                   value: Time.now unless Services.exists? "#{KEY}/#{name}"
    end

    def load_endpoint
      endpoint.load
      endpoint
    end

    def fetch_members
      if Services.exists? "#{KEY}/#{name}/members"
        Services.get("#{KEY}/#{name}/members").children
      else
        nil
      end
    end

    # rubocop:disable MethodLength
    def load_members
      etcd_members = fetch_members
      unless etcd_members.nil? || etcd_members.empty?
        etcd_members.each do |m|
          m_name = File.basename m.key
          m1 = Services::Member.new(m_name, service: name)
          m1.load
          @members.push m1
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jn_services-1.0.7 lib/services/service.rb
jn_services-1.0.6 lib/services/service.rb
jn_services-1.0.5 lib/services/service.rb