Class: RubyReactor::MiddlewareRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_reactor/middleware_runner.rb

Overview

MiddlewareRunner executes event hooks on a collection of configured middlewares.

Instance Method Summary collapse

Constructor Details

#initialize(middlewares) ⇒ MiddlewareRunner

Returns a new instance of MiddlewareRunner.



6
7
8
# File 'lib/ruby_reactor/middleware_runner.rb', line 6

def initialize(middlewares)
  @middlewares = middlewares || []
end

Instance Method Details

#on(event, *args) ⇒ Object

Dispatches the given lifecycle event to all configured middlewares. Invokes the specific event hook method if defined, e.g., ‘on_start_reactor`. Fallback to the generic `on` method if implemented on the middleware. StandardErrors are swallowed and logged to prevent middleware failure from halting execution.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_reactor/middleware_runner.rb', line 14

def on(event, *args)
  @middlewares.each do |middleware|
    method_name = "on_#{event}"
    if middleware.respond_to?(method_name)
      middleware.send(method_name, *args)
    elsif middleware.respond_to?(:on)
      middleware.on(event, *args)
    end
  rescue StandardError => e
    RubyReactor.configuration.logger.warn(
      "RubyReactor middleware error in #{middleware.class} during #{event}: #{e.message}"
    )
  end
end