Class: BBK::App::Dispatcher
- Inherits:
-
Object
- Object
- BBK::App::Dispatcher
- Defined in:
- lib/bbk/app/dispatcher.rb,
lib/bbk/app/dispatcher/route.rb,
lib/bbk/app/dispatcher/result.rb,
lib/bbk/app/dispatcher/message.rb,
lib/bbk/app/dispatcher/message_stream.rb,
lib/bbk/app/dispatcher/queue_stream_strategy.rb,
lib/bbk/app/dispatcher/direct_stream_strategy.rb,
sig/bbk/app/dispatcher.rbs,
sig/bbk/app/dispatcher/route.rbs,
sig/bbk/app/dispatcher/result.rbs,
sig/bbk/app/dispatcher/message.rbs,
sig/bbk/app/dispatcher/message_stream.rbs,
sig/bbk/app/dispatcher/stream_strategy.rbs,
sig/bbk/app/dispatcher/queue_stream_strategy.rbs
Defined Under Namespace
Modules: _Consumer, _IncomingMessage, _Mapping, _Message, _Middleware, _MiddlewareBuilder, _MiddlewareClass, _PoolFactory, _ProcessorsStack, _Publisher, _Result, _StreamStrategy Classes: DirectStreamStrategy, Message, MessageStream, QueueStreamStrategy, Result, Route, StreamStrategyClass
Constant Summary collapse
- ANSWER_DOMAIN =
'answer'.freeze
- DEFAULT_PROTOCOL =
'default'.freeze
Instance Attribute Summary collapse
-
#consumers ⇒ Array[_Consumer]
readonly
Returns the value of attribute consumers.
-
#force_quit ⇒ Boolean
Returns the value of attribute force_quit.
-
#logger ⇒ logger
readonly
Returns the value of attribute logger.
-
#middlewares ⇒ Array[_MiddlewareBuilder|_MiddlewareClass]
readonly
Returns the value of attribute middlewares.
-
#observer ⇒ Object
readonly
Returns the value of attribute observer.
-
#pool_size ⇒ Object
Returns the value of attribute pool_size.
-
#publishers ⇒ Array[_Publisher]
readonly
Returns the value of attribute publishers.
Instance Method Summary collapse
- #build_processing_stack ⇒ _ProcessorsStack
-
#close(timeout = 5) ⇒ void
stop dispatcher and wait for termination Чтоб остановить диспетчер надо: 1.
- #execute_message(message) ⇒ Object
- #find_processor(msg) ⇒ [untyped, _Processor]
-
#initialize(observer, pool_size: 3, logger: BBK::App.logger, pool_factory: SimplePoolFactory, stream_strategy: QueueStreamStrategy) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
-
#process(message) ⇒ void
process one message and sending existed results messages.
- #process_message(message) ⇒ Array[Result]
- #publish_result(result) ⇒ Concurrent::Promises::ResolvableFuture
- #register_consumer(consumer) ⇒ void
- #register_middleware(middleware) ⇒ void
- #register_publisher(publisher) ⇒ void
-
#run ⇒ void
Run all consumers and blocks on message processing.
- #send_results(incoming, results) ⇒ void
Constructor Details
#initialize(observer, pool_size: 3, logger: BBK::App.logger, pool_factory: SimplePoolFactory, stream_strategy: QueueStreamStrategy) ⇒ Dispatcher
Returns a new instance of Dispatcher.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bbk/app/dispatcher.rb', line 41 def initialize(observer, pool_size: 3, logger: BBK::App.logger, pool_factory: SimplePoolFactory, stream_strategy: QueueStreamStrategy) @observer = observer @pool_size = pool_size logger = logger.respond_to?(:tagged) ? logger : ActiveSupport::TaggedLogging.new(logger) @logger = BBK::Utils::ProxyLogger.new(logger, tags: 'Dispatcher') @consumers = [] @publishers = [] @middlewares = [] @pool_factory = pool_factory @stream_strategy_class = stream_strategy @force_quit = false end |
Instance Attribute Details
#consumers ⇒ Array[_Consumer] (readonly)
Returns the value of attribute consumers.
36 37 38 |
# File 'lib/bbk/app/dispatcher.rb', line 36 def consumers @consumers end |
#force_quit ⇒ Boolean
Returns the value of attribute force_quit.
35 36 37 |
# File 'lib/bbk/app/dispatcher.rb', line 35 def force_quit @force_quit end |
#logger ⇒ logger (readonly)
Returns the value of attribute logger.
36 37 38 |
# File 'lib/bbk/app/dispatcher.rb', line 36 def logger @logger end |
#middlewares ⇒ Array[_MiddlewareBuilder|_MiddlewareClass] (readonly)
Returns the value of attribute middlewares.
36 37 38 |
# File 'lib/bbk/app/dispatcher.rb', line 36 def middlewares @middlewares end |
#observer ⇒ Object (readonly)
Returns the value of attribute observer.
36 37 38 |
# File 'lib/bbk/app/dispatcher.rb', line 36 def observer @observer end |
#pool_size ⇒ Object
Returns the value of attribute pool_size.
35 36 37 |
# File 'lib/bbk/app/dispatcher.rb', line 35 def pool_size @pool_size end |
#publishers ⇒ Array[_Publisher] (readonly)
Returns the value of attribute publishers.
36 37 38 |
# File 'lib/bbk/app/dispatcher.rb', line 36 def publishers @publishers end |
Instance Method Details
#build_processing_stack ⇒ _ProcessorsStack
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/bbk/app/dispatcher.rb', line 199 def build_processing_stack # Оптимизация.Строим стэк только один раз. Могут быть проблемы, но надеюсь что всё будет норм # Если какая-то мидлварь НЕ должна сохранять состояние (а случайно сохраняет), пусть # реализует метод build и чистит состояние @processing_stack ||= begin stack = proc{|msg| (msg) } middlewares.reverse.reduce(stack) do |stack, middleware| if middleware.respond_to?(:build) middleware.build(stack) else middleware.new(stack) end end end end |
#close(timeout = 5) ⇒ void
This method returns an undefined value.
stop dispatcher and wait for termination Чтоб остановить диспетчер надо:
- остановить консьюмеры
- остановить прием новых сообщений - @stream.close
- дождаться обработки всего в очереди или таймаут
- остановить потоки
- остановить middlewares если они имеют метод stop
- остановить паблишеры
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/bbk/app/dispatcher.rb', line 94 def close(timeout = 5) ActiveSupport::Notifications.instrument 'dispatcher.close', dispatcher: self consumers.each do |cons| begin cons.stop rescue StandardError => e logger.error "Consumer #{cons} stop error: #{e}" logger.debug e.backtrace end end @stream_strategy&.stop(timeout) consumers.each do |cons| begin cons.close rescue StandardError => e logger.error "Consumer #{cons} close error: #{e}" logger.debug e.backtrace end end # останавливаем middlewares middlewares.each do |m| next unless m.respond_to?(:stop) m.stop rescue StandardError => e logger.error "Middleware #{m} stop error: #{e}" logger.debug e.backtrace end publishers.each do |pub| begin pub.close rescue StandardError => e logger.error "Publisher #{pub} close error: #{e}" logger.debug e.backtrace end end end |
#execute_message(message) ⇒ Object
135 136 137 138 139 |
# File 'lib/bbk/app/dispatcher.rb', line 135 def () build_processing_stack.call().select do |r| r.is_a?(BBK::App::Dispatcher::Result) end end |
#find_processor(msg) ⇒ [untyped, _Processor]
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/bbk/app/dispatcher.rb', line 187 def find_processor(msg) matched, callback = if @observer.respond_to?(:match_message) # Новый интерфейс рассчитаный на BBK::Message @observer.(msg) else # старый интерфейс @observer.match(msg.headers, msg.payload, msg.delivery_info) end [matched, callback.is_a?(BBK::App::Factory) ? callback.create : callback] end |
#process(message) ⇒ void
This method returns an undefined value.
process one message and sending existed results messages
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/bbk/app/dispatcher.rb', line 142 def process() results = () if .respond_to?(:nacked?) && .nacked? logger.debug "Ignore sending results: message(#{.headers[:message_id]}) nacked in processor" return end logger.debug "There are #{results.count} results to send from #{.headers[:message_id]}..." ActiveSupport::Notifications.instrument 'dispatcher.process', msg: do send_results(, results).value end rescue StandardError => e logger.error "Failed processing message: #{e.inspect}" # это событие устарело, вместо него надо использовать `dispatcher.process` ActiveSupport::Notifications.instrument 'dispatcher.exception', msg: , exception: e .nack(error: e) close if force_quit end |
#process_message(message) ⇒ Array[Result]
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/bbk/app/dispatcher.rb', line 162 def () matched, processor = find_processor() results = [] begin is_unknown = @observer.instance_variable_get('@default') == processor ActiveSupport::Notifications.instrument 'dispatcher.request.process', msg: , match: matched, unknown: is_unknown, processor: processor do processor.call(, results: results) end rescue StandardError => e logger.error "Failed processing message in processor: #{e.inspect}. Backrace: #{e.backtrace[0..10]}" if processor.respond_to?(:on_error) results = processor.on_error(, e) else raise end end [results].flatten rescue StandardError => e # это событие устарело, вместо него надо использовать `dispatcher.request.process` ActiveSupport::Notifications.instrument 'dispatcher.request.exception', msg: , match: matched, processor: processor, exception: e raise end |
#publish_result(result) ⇒ Concurrent::Promises::ResolvableFuture
245 246 247 248 249 250 251 252 253 |
# File 'lib/bbk/app/dispatcher.rb', line 245 def publish_result(result) route = result.route logger.debug "Publish result to #{route} ..." publisher = publishers.find {|pub| pub.protocols.include?(route.scheme) } raise "Not found publisher for scheme #{route.scheme}" if publisher.nil? # return Concurrent::Promises.resolvable_future publisher.publish(result) end |
#register_consumer(consumer) ⇒ void
This method returns an undefined value.
54 55 56 |
# File 'lib/bbk/app/dispatcher.rb', line 54 def register_consumer(consumer) consumers << consumer end |
#register_middleware(middleware) ⇒ void
This method returns an undefined value.
64 65 66 |
# File 'lib/bbk/app/dispatcher.rb', line 64 def register_middleware(middleware) middlewares << middleware end |
#register_publisher(publisher) ⇒ void
This method returns an undefined value.
58 59 60 61 62 |
# File 'lib/bbk/app/dispatcher.rb', line 58 def register_publisher(publisher) raise "Publisher support #{DEFAULT_PROTOCOL}" if publisher.protocols.include?(DEFAULT_PROTOCOL) publishers << publisher end |
#run ⇒ void
This method returns an undefined value.
Run all consumers and blocks on message processing
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bbk/app/dispatcher.rb', line 69 def run @pool = @pool_factory.call(@pool_size, 10) @stream_strategy = @stream_strategy_class.new(@pool, logger: logger) ActiveSupport::Notifications.instrument 'dispatcher.run', dispatcher: self @stream_strategy.run(consumers) do |msg| begin logger.tagged(msg.headers[:message_id]) do process msg end rescue StandardError => e logger.fatal "E[#{@stream_strategy_class}]: #{e}" logger.fatal "E[#{@stream_strategy_class}]: #{e.backtrace.join("\n")}" end end end |
#send_results(incoming, results) ⇒ void
This method returns an undefined value.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/bbk/app/dispatcher.rb', line 215 def send_results(incoming, results) = incoming.headers[:message_id] answers, = results.partition { _1.route.domain == ANSWER_DOMAIN } # allowed only one answer message raise InvalidAnswersMessagesCountError.new("Get #{answers.size} on processing message with id=#{}") if answers.size > 1 answer = answers.first Concurrent::Promises.zip_futures(*.map do |result| publish_result(result) end).then do |_successes| incoming.ack(answer: answer) end.rescue do |*errors| error = errors.compact.first ActiveSupport::Notifications.instrument 'dispatcher.request.result_rejected', msg: incoming, message: error.inspect logger.error "[Message#{}] Publish failed: #{error.inspect}" incoming.nack(error: error) close if force_quit rescue StandardError => e warn e.backtrace warn "[CRITICAL] #{self.class} [#{Process.pid}] failure exiting: #{e.inspect}" ActiveSupport::Notifications.instrument 'dispatcher.exception', msg: incoming, exception: e sleep(10) exit!(1) end end |