class Watership::Client

Public Class Methods

new(uri, env) click to toggle source
# File lib/watership.rb, line 69
def initialize(uri, env)
  @uri, @env = uri, env
end

Public Instance Methods

clear_channel() click to toggle source
# File lib/watership.rb, line 150
def clear_channel
  if thread[:bunny_channel].respond_to?(:close)
    begin
      thread[:bunny_channel].close
    rescue *CLEAR_CHANNEL_EXCEPTIONS => exception # the channel is already closed
      logger.error("Exception #{exception.class.name} got raised in Watership#clear_channel")
    end
  end
ensure
  thread[:bunny_channel] = nil
end
clear_connection() click to toggle source
# File lib/watership.rb, line 129
def clear_connection
  if @bunny_connection.respond_to?(:close)
    begin
      @bunny_connection.close
    rescue *CLEAR_CONNECTION_EXCEPTIONS => exception # the connection is already closed
      logger.error("Exception #{exception.class.name} got raised in Watership#clear_connection")
    end
  end
ensure
  @bunny_connection = nil
end
close() click to toggle source
# File lib/watership.rb, line 108
def close
  clear_channel
  clear_connection
end
connect_with_queue(name, options = {}) click to toggle source
# File lib/watership.rb, line 98
def connect_with_queue(name, options = {})
  with_channel do |channel|
    channel.queue(name, { durable: true }.merge(options)) if channel
  end
end
connection() click to toggle source
# File lib/watership.rb, line 119
def connection
  @bunny_connection ||= begin
    Bunny.new(@uri).tap { |bunny| bunny.start }
  rescue Timeout::Error => exception
    logger.error("Exception #{exception.class.name} got raised in Watership#connection")
    raise exception
  end
end
Also aliased as: create_connection
create_channel() click to toggle source
# File lib/watership.rb, line 146
def create_channel
  thread[:bunny_channel] ||= connection.create_channel
end
create_connection()
Alias for: connection
enqueue(options = {}) click to toggle source
# File lib/watership.rb, line 73
def enqueue(options = {})
  if options[:name]
    raise ArgumentError, ":name is no longer valid. Use :queue instead"
  end

  options  = options.dup
  message  = options.delete(:message)
  name     = options.delete(:queue)
  fallback = options.delete(:fallback)

  queue = connect_with_queue(name, options)
  queue.publish(JSON.generate(message))
rescue *CONNECTION_EXCEPTIONS => exception
  fallback.call if fallback
  notify(exception)
  clear_channel
  logger.error(exception.class.name)
  raise exception
rescue StandardError => exception
  fallback.call if fallback
  notify(exception)
  logger.error(exception.class.name)
  raise exception
end
logger() click to toggle source
# File lib/watership.rb, line 167
def logger
  Watership.logger
end
notify(exception) click to toggle source
# File lib/watership.rb, line 162
def notify(exception)
  Bugsnag.notify(exception) if defined?(Bugsnag) && @env == 'production'
  Airbrake.notify_or_ignore(exception) if defined?(Airbrake) && @env == 'production'
end
reconnect() click to toggle source
# File lib/watership.rb, line 113
def reconnect
  close
  create_channel
  true
end
thread() click to toggle source
# File lib/watership.rb, line 104
def thread
  Thread.current[:"watership_#{self.object_id}"] ||= {}
end
with_channel() { |channel| ... } click to toggle source
# File lib/watership.rb, line 141
def with_channel
  channel = create_channel
  yield(channel) if channel
end