Class: Msgr::Dispatcher
- Inherits:
-
Object
- Object
- Msgr::Dispatcher
- Includes:
- Logging
- Defined in:
- lib/msgr/dispatcher.rb
Overview
The Dispatcher receives incoming messages, process them through a middleware stack and delegate them to a new and fresh consumer instance.
Instance Attribute Summary collapse
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Instance Method Summary collapse
- #call(message) ⇒ Object
- #dispatch(message) ⇒ Object
-
#initialize(config) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #shutdown ⇒ Object
- #to_s ⇒ Object
Methods included from Logging
Constructor Details
#initialize(config) ⇒ Dispatcher
Returns a new instance of Dispatcher.
14 15 16 17 18 19 |
# File 'lib/msgr/dispatcher.rb', line 14 def initialize(config) config[:pool_class] ||= 'Concurrent::CachedThreadPool' log(:info) { "Initialize new dispatcher (#{config[:pool_class]} with #{config[:max]} threads)..." } @pool = config[:pool_class].constantize.new(max: config[:max]) end |
Instance Attribute Details
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
12 13 14 |
# File 'lib/msgr/dispatcher.rb', line 12 def pool @pool end |
Instance Method Details
#call(message) ⇒ Object
21 22 23 24 25 |
# File 'lib/msgr/dispatcher.rb', line 21 def call() pool.post() do || dispatch end end |
#dispatch(message) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/msgr/dispatcher.rb', line 27 def dispatch() consumer_class = Object.const_get .route.consumer log(:debug) { "Dispatch message to #{consumer_class.name}" } consumer_class.new.dispatch # Acknowledge message unless it is already acknowledged. .ack unless .acked? rescue => error .nack unless .acked? log(:error) do "Dispatcher error: #{error.class.name}: #{error}\n" + error.backtrace.join("\n") end ensure if defined?(ActiveRecord) && ActiveRecord::Base.connection_pool.active_connection? log(:debug) { 'Release used AR connection for dispatcher thread.' } ActiveRecord::Base.connection_pool.release_connection end end |
#shutdown ⇒ Object
50 51 |
# File 'lib/msgr/dispatcher.rb', line 50 def shutdown end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/msgr/dispatcher.rb', line 53 def to_s self.class.name end |