Class: NatsListener::Client

Inherits:
NatsListenerCore::AbstractClient
  • Object
show all
Defined in:
lib/nats_listener/client.rb

Overview

Client for nats implementation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Use this opts:



16
17
18
19
20
21
# File 'lib/nats_listener/client.rb', line 16

def initialize(opts = {})
  @nats = ::NATS::IO::Client.new unless opts[:disable_nats] # Create nats client
  @logger =  NatsListenerCore::ClientLogger.new(opts)
  @skip = opts[:skip] || false
  @client_catcher =  NatsListenerCore::ClientCatcher.new(opts)
end

Instance Attribute Details

#:catch_errors - used to catch errors around subscribers/connections(be careful with it!)(: catch_errors-usedtocatcherrorsaroundsubscribers/connections(be careful with it!)) ⇒ Object

Use this opts:



16
17
18
19
20
21
# File 'lib/nats_listener/client.rb', line 16

def initialize(opts = {})
  @nats = ::NATS::IO::Client.new unless opts[:disable_nats] # Create nats client
  @logger =  NatsListenerCore::ClientLogger.new(opts)
  @skip = opts[:skip] || false
  @client_catcher =  NatsListenerCore::ClientCatcher.new(opts)
end

#:catch_provider - this class will be called with catch_provider.error(e)(: catch_provider-this) ⇒ Object

Use this opts:



16
17
18
19
20
21
# File 'lib/nats_listener/client.rb', line 16

def initialize(opts = {})
  @nats = ::NATS::IO::Client.new unless opts[:disable_nats] # Create nats client
  @logger =  NatsListenerCore::ClientLogger.new(opts)
  @skip = opts[:skip] || false
  @client_catcher =  NatsListenerCore::ClientCatcher.new(opts)
end

#:disable_nats - if something is passed to that attribute - nats won't be initialized(: disable_nats-) ⇒ Object

Use this opts:



16
17
18
19
20
21
# File 'lib/nats_listener/client.rb', line 16

def initialize(opts = {})
  @nats = ::NATS::IO::Client.new unless opts[:disable_nats] # Create nats client
  @logger =  NatsListenerCore::ClientLogger.new(opts)
  @skip = opts[:skip] || false
  @client_catcher =  NatsListenerCore::ClientCatcher.new(opts)
end

#:logger - logger used in this service(: logger-loggerused) ⇒ Object

Use this opts:



16
17
18
19
20
21
# File 'lib/nats_listener/client.rb', line 16

def initialize(opts = {})
  @nats = ::NATS::IO::Client.new unless opts[:disable_nats] # Create nats client
  @logger =  NatsListenerCore::ClientLogger.new(opts)
  @skip = opts[:skip] || false
  @client_catcher =  NatsListenerCore::ClientCatcher.new(opts)
end

#:nats - configuration of nats service(: nats-configurationofnatsservice) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nats_listener/client.rb', line 26

def establish_connection(opts)
  return if skip

  @config = opts.to_h
  begin
    @nats.connect(config) # Connect nats to provided configuration
    true
  rescue StandardError => exception
    log(action: :connection_failed, message: exception)
    false
  end
end

#:service_name - Name of current service(: service_name-Nameofcurrentservice) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nats_listener/client.rb', line 26

def establish_connection(opts)
  return if skip

  @config = opts.to_h
  begin
    @nats.connect(config) # Connect nats to provided configuration
    true
  rescue StandardError => exception
    log(action: :connection_failed, message: exception)
    false
  end
end

#:skip - flag attribute used to skip connections(useful for testing)(: skip-flagattributeusedtoskipconnections(useful) ⇒ Object

Use this opts:



16
17
18
19
20
21
# File 'lib/nats_listener/client.rb', line 16

def initialize(opts = {})
  @nats = ::NATS::IO::Client.new unless opts[:disable_nats] # Create nats client
  @logger =  NatsListenerCore::ClientLogger.new(opts)
  @skip = opts[:skip] || false
  @client_catcher =  NatsListenerCore::ClientCatcher.new(opts)
end

Instance Method Details

#Establish(connectionwithnatsserver) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nats_listener/client.rb', line 26

def establish_connection(opts)
  return if skip

  @config = opts.to_h
  begin
    @nats.connect(config) # Connect nats to provided configuration
    true
  rescue StandardError => exception
    log(action: :connection_failed, message: exception)
    false
  end
end

#establish_connection(opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nats_listener/client.rb', line 26

def establish_connection(opts)
  return if skip

  @config = opts.to_h
  begin
    @nats.connect(config) # Connect nats to provided configuration
    true
  rescue StandardError => exception
    log(action: :connection_failed, message: exception)
    false
  end
end

#reestablish_connectionObject



46
47
48
# File 'lib/nats_listener/client.rb', line 46

def reestablish_connection
  establish_connection(config) if nats&.status.to_i.zero?
end

#request(subject, message, opts = {}) ⇒ Object



39
40
41
42
43
44
# File 'lib/nats_listener/client.rb', line 39

def request(subject, message, opts = {})
  with_connection do
    log(action: :request, message: message)
    nats.request(subject, message, opts)
  end
end