Class: Freddy

Inherits:
Object
  • Object
show all
Defined in:
lib/freddy.rb,
lib/freddy/utils.rb,
lib/freddy/payload.rb,
lib/freddy/request.rb,
lib/freddy/delivery.rb,
lib/freddy/consumer.rb,
lib/freddy/producer.rb,
lib/freddy/timeout_error.rb,
lib/freddy/error_response.rb,
lib/freddy/adaptive_queue.rb,
lib/freddy/request_manager.rb,
lib/freddy/message_handler.rb,
lib/freddy/message_handlers.rb,
lib/freddy/responder_handler.rb,
lib/freddy/invalid_request_error.rb,
lib/freddy/sync_response_container.rb

Defined Under Namespace

Modules: MessageHandlers Classes: AdaptiveQueue, Consumer, Delivery, ErrorResponse, InvalidRequestError, MessageHandler, Payload, Producer, Request, RequestManager, ResponderHandler, SyncResponseContainer, TimeoutError, Utils

Constant Summary collapse

FREDDY_TOPIC_EXCHANGE_NAME =
'freddy-topic'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, logger, max_concurrency) ⇒ Freddy

Returns a new instance of Freddy.


52
53
54
55
56
57
58
59
# File 'lib/freddy.rb', line 52

def initialize(connection, logger, max_concurrency)
  @connection = connection
  @channel  = connection.create_channel
  @consume_thread_pool = Thread.pool(max_concurrency)
  @consumer = Consumer.new channel, logger, @consume_thread_pool
  @producer = Producer.new channel, logger
  @request  = Request.new channel, logger, @producer, @consumer
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel


50
51
52
# File 'lib/freddy.rb', line 50

def channel
  @channel
end

#consumerObject (readonly)

Returns the value of attribute consumer


50
51
52
# File 'lib/freddy.rb', line 50

def consumer
  @consumer
end

#producerObject (readonly)

Returns the value of attribute producer


50
51
52
# File 'lib/freddy.rb', line 50

def producer
  @producer
end

#requestObject (readonly)

Returns the value of attribute request


50
51
52
# File 'lib/freddy.rb', line 50

def request
  @request
end

Class Method Details

.build(logger = Logger.new(STDOUT), config = {}) ⇒ Freddy

Creates a new freddy instance

Examples:

Freddy.build(Logger.new(STDOUT), user: 'thumper', pass: 'howdy')

Parameters:

  • logger (Logger) (defaults to: Logger.new(STDOUT))

    instance of a logger, defaults to the STDOUT logger

  • config (Hash) (defaults to: {})

    rabbitmq connection information

Options Hash (config):

  • :host (String) — default: 'localhost'
  • :port (Integer) — default: 5672
  • :user (String) — default: 'guest'
  • :pass (String) — default: 'guest'

Returns:


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/freddy.rb', line 38

def self.build(logger = Logger.new(STDOUT), config = {})
  if RUBY_PLATFORM == 'java'
    connection = MarchHare.connect(config)
  else
    connection = Bunny.new(config)
    connection.start
    connection
  end

  new(connection, logger, config.fetch(:max_concurrency, 4))
end

Instance Method Details

#closeObject


136
137
138
# File 'lib/freddy.rb', line 136

def close
  @connection.close
end

#deliver(destination, payload, options = {}) ⇒ void

This method returns an undefined value.

Sends a message to given destination

This is *send and forget* type of delivery. It sends a message to given destination and does not wait for response. This is useful when there are multiple consumers that are using #tap_into or you just do not care about the response.

Examples:

freddy.deliver 'Metrics', user_id: 5, metric: 'signed_in'

Parameters:

  • destination (String)

    the queue name

  • payload (Hash)

    the payload that can be serialized to json

  • options (Hash) (defaults to: {})

    the options for delivery

Options Hash (options):

  • :timeout (Integer) — default: 0

    discards the message after given seconds if nobody consumes it. Message won't be discarded if timeout it set to 0 (default).


90
91
92
93
94
95
96
# File 'lib/freddy.rb', line 90

def deliver(destination, payload, options = {})
  timeout = options.fetch(:timeout, 0)
  opts = {}
  opts[:expiration] = (timeout * 1000).to_i if timeout > 0

  @producer.produce destination, payload, opts
end

#deliver_with_response(destination, payload, options = {}) ⇒ Hash

Sends a message and waits for the response

Examples:

begin
  response = freddy.deliver_with_response 'Users', type: 'fetch_all'
  puts "Got response #{response}"
rescue Freddy::TimeoutError
  puts "Service unavailable"
rescue Freddy::InvalidRequestError => e
  puts "Got error response: #{e.response}"
end

Parameters:

  • destination (String)

    the queue name

  • payload (Hash)

    the payload that can be serialized to json

  • options (Hash) (defaults to: {})

    the options for delivery

Options Hash (options):

  • :timeout (Integer) — default: 3

    throws a time out exception after given seconds when there is no response

  • :delete_on_timeout (Boolean) — default: true

    discards the message when timeout error is raised

Returns:

  • (Hash)

    the response

Raises:


127
128
129
130
131
132
133
134
# File 'lib/freddy.rb', line 127

def deliver_with_response(destination, payload, options = {})
  timeout = options.fetch(:timeout, 3)
  delete_on_timeout = options.fetch(:delete_on_timeout, true)

  @request.sync_request destination, payload, {
    timeout: timeout, delete_on_timeout: delete_on_timeout
  }
end

#respond_to(destination, &callback) ⇒ Object


61
62
63
# File 'lib/freddy.rb', line 61

def respond_to(destination, &callback)
  @request.respond_to destination, &callback
end

#tap_into(pattern, &callback) ⇒ Object


65
66
67
# File 'lib/freddy.rb', line 65

def tap_into(pattern, &callback)
  @consumer.tap_into pattern, &callback
end