Class: JayAPI::Elasticsearch::ClientFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/jay_api/elasticsearch/client_factory.rb

Overview

A factory class that creates an Elasticsearch Client object. More specifically, a JayAPI wrapper object over the Elasticsearch Client object.

Constant Summary collapse

DEFAULT_ELASTICSEARCH_PORT =

The default port for the Elasticsearch cluster

9200
WAIT_STRATEGIES =

Classes that define the available waiting strategies, used for deciding sleep time between the reconnections.

{
  geometric: JayAPI::Abstract::GeometricWait,
  constant: JayAPI::Abstract::ConstantWait
}.freeze
MAX_ATTEMPTS =

The maximum number of connection attempts to be made.

4
WAIT_INTERVAL =

The default wait time to be passed to the wait strategy class.

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster_url:, port: nil, logger: nil, **credentials) ⇒ ClientFactory

Creates a new instance of the class. disabling :reek:ControlParameter

Parameters:

  • cluster_url (String)

    The URL where the Elasticsearch service is exposed.

  • port (Integer) (defaults to: nil)

    The port to use to connect to the Elasticsearch instance (Needed only when different from the default Elasticsearch port)

  • logger (Logging::Logger) (defaults to: nil)

    The logger object to use, if none is given a new one will be created.

  • credentials (Hash)

Options Hash (**credentials):

  • :username (String)

    The user name to use when authenticating against the Elasticsearch instance.

  • :password (String)

    The password to use when authenticating against the Elasticsearch instance.



48
49
50
51
52
53
54
# File 'lib/jay_api/elasticsearch/client_factory.rb', line 48

def initialize(cluster_url:, port: nil, logger: nil, **credentials)
  @cluster_url = cluster_url
  @port = port || DEFAULT_ELASTICSEARCH_PORT
  @logger = logger || Logging.logger($stdout)
  @username = credentials[:username]
  @password = credentials[:password]
end

Instance Attribute Details

#cluster_urlObject (readonly)

Returns the value of attribute cluster_url.



32
33
34
# File 'lib/jay_api/elasticsearch/client_factory.rb', line 32

def cluster_url
  @cluster_url
end

#portObject (readonly)

Returns the value of attribute port.



32
33
34
# File 'lib/jay_api/elasticsearch/client_factory.rb', line 32

def port
  @port
end

Instance Method Details

#create(max_attempts: MAX_ATTEMPTS, wait_strategy: :geometric, wait_interval: WAIT_INTERVAL) ⇒ JayAPI::Elasticsearch::Client

Returns the current instance of the Elasticsearch client, or creates a new one.

Parameters:

  • max_attempts (Integer) (defaults to: MAX_ATTEMPTS)

    The maximum number of attempts that the connection shall be retried.

  • wait_strategy (Symbol) (defaults to: :geometric)

    The waiting strategy for reconnections (:geometric or :constant).

  • wait_interval (Integer) (defaults to: WAIT_INTERVAL)

    The wait interval for the wait strategy. The sleep time between each connection will be:

    • wait_interval with :constant wait strategy

    • wait_interval**i with :geometric wait strategy (where i is the i’th re-try)

Returns:



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jay_api/elasticsearch/client_factory.rb', line 66

def create(max_attempts: MAX_ATTEMPTS, wait_strategy: :geometric, wait_interval: WAIT_INTERVAL)
  JayAPI::Elasticsearch::Client.new(
    ::Elasticsearch::Client.new(
      hosts: [host],
      log: false
    ),
    logger,
    max_attempts: max_attempts,
    wait_strategy: WAIT_STRATEGIES[wait_strategy].new(wait_interval: wait_interval, logger: logger)
  )
end