Class: DaemonObjects::ConsumerBase

Inherits:
Object
  • Object
show all
Includes:
NewRelic::Agent::Instrumentation::ControllerInstrumentation
Defined in:
lib/daemon_objects/consumer_base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ ConsumerBase

Returns a new instance of ConsumerBase.



11
12
13
14
15
# File 'lib/daemon_objects/consumer_base.rb', line 11

def initialize(opts)
  @logger        = opts[:logger]
  @app_directory = opts[:app_directory]
  @environment   = opts[:environment]
end

Class Attribute Details

.message_handlerObject

Returns the value of attribute message_handler.



6
7
8
# File 'lib/daemon_objects/consumer_base.rb', line 6

def message_handler
  @message_handler
end

Instance Attribute Details

#app_directoryObject (readonly)

Returns the value of attribute app_directory.



9
10
11
# File 'lib/daemon_objects/consumer_base.rb', line 9

def app_directory
  @app_directory
end

#environmentObject (readonly)

Returns the value of attribute environment.



9
10
11
# File 'lib/daemon_objects/consumer_base.rb', line 9

def environment
  @environment
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/daemon_objects/consumer_base.rb', line 9

def logger
  @logger
end

Class Method Details

.handle_messages_with(&block) ⇒ Object

Raises:

  • (StandardError)


33
34
35
36
# File 'lib/daemon_objects/consumer_base.rb', line 33

def self.handle_messages_with(&block)
  raise StandardError.new("Provided block must take at least one argument - 'payload'") if block.arity < 1
  define_method(:handle_message_impl, &block) 
end

Instance Method Details

#handle_message(payload) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/daemon_objects/consumer_base.rb', line 21

def handle_message(payload)
  logger.info("Handling message #{payload}")
  response = handle_message_impl(payload)
  logger.info("Completed handling message")
  response
rescue StandardError => e
  logger.error("#{e.class}:  #{e.message}")
  logger.error(e.backtrace.join("\n"))
  Airbrake.notify(e) if defined?(Airbrake)
  e
end

#runObject



17
18
19
# File 'lib/daemon_objects/consumer_base.rb', line 17

def run
  logger.info("Starting consumer")
end