module Protobuf::Nats

Constants

GET_CONNECTED_MUTEX
NatsClient
VERSION

Attributes

client_nats_connection[RW]

Public Class Methods

config() click to toggle source
# File lib/protobuf/nats.rb, line 35
def self.config
  @config ||= begin
    config = ::Protobuf::Nats::Config.new
    config.load_from_yml
    config
  end
end
error_callbacks() click to toggle source

We will always log an error.

# File lib/protobuf/nats.rb, line 47
def self.error_callbacks
  @error_callbacks ||= [lambda { |error| log_error(error) }]
end
log_error(error) click to toggle source

This will work with both ruby and java errors

# File lib/protobuf/nats.rb, line 118
def self.log_error(error)
  logger.error error.to_s
  logger.error error.class.to_s
  if error.respond_to?(:backtrace) && error.backtrace.is_a?(::Array)
    logger.error error.backtrace.join("\n")
  end
end
logger() click to toggle source
# File lib/protobuf/nats.rb, line 126
def self.logger
  ::Protobuf::Logging.logger
end
notify_error_callbacks(error) click to toggle source
# File lib/protobuf/nats.rb, line 60
def self.notify_error_callbacks(error)
  error_callbacks.each do |callback|
    begin
      callback.call(error)
    rescue => callback_error
      log_error(callback_error)
    end
  end

  nil
end
on_error(&block) click to toggle source
# File lib/protobuf/nats.rb, line 54
def self.on_error(&block)
  fail ::ArgumentError unless block.arity == 1
  error_callbacks << block
  nil
end
start_client_nats_connection() click to toggle source
# File lib/protobuf/nats.rb, line 80
def self.start_client_nats_connection
  return true if @client_nats_connection

  GET_CONNECTED_MUTEX.synchronize do
    break true if @client_nats_connection

    # Disable publisher pending buffer on reconnect
    options = config.connection_options.merge(:disable_reconnect_buffer => true)

    client = NatsClient.new
    client.connect(options)

    # Ensure we have a valid connection to the NATS server.
    client.flush(5)

    client.on_disconnect do
      logger.warn("Client NATS connection was disconnected")
    end

    client.on_reconnect do
      logger.warn("Client NATS connection was reconnected")
    end

    client.on_close do
      logger.warn("Client NATS connection was closed")
    end

    client.on_error do |error|
      notify_error_callbacks(error)
    end

    @client_nats_connection = client

    true
  end
end
subscription_key(service_klass, service_method) click to toggle source
# File lib/protobuf/nats.rb, line 72
def self.subscription_key(service_klass, service_method)
  service_class_name = service_klass.name.underscore.gsub("/", ".")
  service_method_name = service_method.to_s.underscore

  subscription_key = "rpc.#{service_class_name}.#{service_method_name}"
  subscription_key = config.make_subscription_key_replacements(subscription_key)
end