Class: Baton::Consumer

Inherits:
Object
  • Object
show all
Includes:
Logging, Observer
Defined in:
lib/baton/consumer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger, logger=

Methods included from Observer

#notify, #notify_error, #notify_info, #notify_log, #notify_success

Constructor Details

#initialize(consumer_name, server) ⇒ Consumer

Public: Initialize a Consumer.

consumer_name - A String naming the consumer. server - An instance of Baton::Server.



14
15
16
# File 'lib/baton/consumer.rb', line 14

def initialize(consumer_name, server)
  @consumer_name, @server = consumer_name, server
end

Instance Attribute Details

#consumer_managerObject

Returns the value of attribute consumer_manager.



8
9
10
# File 'lib/baton/consumer.rb', line 8

def consumer_manager
  @consumer_manager
end

#consumer_nameObject

Returns the value of attribute consumer_name.



8
9
10
# File 'lib/baton/consumer.rb', line 8

def consumer_name
  @consumer_name
end

#serverObject

Returns the value of attribute server.



8
9
10
# File 'lib/baton/consumer.rb', line 8

def server
  @server
end

Instance Method Details

#attributesObject

Public: Method that provides an hash of attributes, if they are needed.

Examples

attributes
# => {type: "pong"}.merge(server.attributes)

Returns Output depends on the implementation.



85
86
87
# File 'lib/baton/consumer.rb', line 85

def attributes
  {}
end

#exception_notifierObject

Public: Method that wraps a block and notifies when errors occur.

Examples

exception_notifier do
  <your code>
end

Returns nothing.



40
41
42
43
44
# File 'lib/baton/consumer.rb', line 40

def exception_notifier
  yield
rescue Exception => e
  notify_error(e.class, e.message)
end

#handle_message(payload) ⇒ Object

Public: Method that decodes a json message and passes it to process_message to be processed.

payload - A message in json format

Examples

handle_message("{\"message\":\"a message\",\"type\":\"a type\"}")

Returns nothing.



56
57
58
59
60
61
# File 'lib/baton/consumer.rb', line 56

def handle_message(payload)
  exception_notifier do
    message = JSON.load(payload)
    process_message(message)
  end
end

#process_message(message) ⇒ Object

Public: Method that will be called when handle_message receives a message. It should be implemented by baton-like gems and it should add logic to process messages.

message - A message in ruby-format

Examples

process_message({"type" => "current"})

Returns Output depends on the implementation.



74
75
# File 'lib/baton/consumer.rb', line 74

def process_message(message)
end

#routing_keyObject

Public: Defines the routing key for the consumer. Messages with the defined routing queue will be consumed by this consumer.

Examples

routing_key
# => "central-apu.production"

Returns the routing key.



27
28
29
# File 'lib/baton/consumer.rb', line 27

def routing_key
  "#{consumer_name}.#{server.environment}"
end