Class: Easyop::Events::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/easyop/events/registry.rb

Overview

Thread-safe global registry for the event bus and handler subscriptions.

The Registry is the coordination point between the Events plugin (producer) and the EventHandlers plugin (subscriber). Neither plugin references the other directly — they communicate only through the bus managed here.

Configure the bus once at boot (e.g. in a Rails initializer):

Easyop::Events::Registry.bus = :memory           # default, in-process
Easyop::Events::Registry.bus = :active_support   # ActiveSupport::Notifications
Easyop::Events::Registry.bus = MyRabbitBus.new   # custom adapter

Or via the global config:

Easyop.configure { |c| c.event_bus = :active_support }

IMPORTANT: Configure the bus BEFORE handler classes are loaded (before autoloading runs in Rails). Subscriptions are registered at class load time and are bound to whatever bus is active at that moment.

Class Method Summary collapse

Class Method Details

.busBus::Base

Returns the active bus adapter. Defaults to a Memory bus.

Falls back to Easyop.config.event_bus if set, then :memory.

Returns:



40
41
42
43
44
45
46
47
# File 'lib/easyop/events/registry.rb', line 40

def bus
  @mutex.synchronize do
    @bus ||= _resolve_bus(
      defined?(Easyop.config) && Easyop.config.respond_to?(:event_bus) && Easyop.config.event_bus ||
      :memory
    )
  end
end

.bus=(bus_or_symbol) ⇒ Object

Set the global bus adapter.

Parameters:

  • bus_or_symbol (:memory, :active_support, Bus::Base, Object)


31
32
33
# File 'lib/easyop/events/registry.rb', line 31

def bus=(bus_or_symbol)
  @mutex.synchronize { @bus = _resolve_bus(bus_or_symbol) }
end

.register_handler(pattern:, handler_class:, async: false, **options) ⇒ Object

Register a handler operation as a subscriber for pattern.

Called automatically by the EventHandlers plugin when ‘on` is declared.

Parameters:

  • pattern (String, Regexp)

    event name or glob

  • handler_class (Class)

    operation class to invoke

  • async (Boolean) (defaults to: false)

    use call_async (requires Async plugin)

  • options (Hash)

    e.g. queue: “low”



57
58
59
60
61
62
63
64
# File 'lib/easyop/events/registry.rb', line 57

def register_handler(pattern:, handler_class:, async: false, **options)
  entry = { pattern: pattern, handler_class: handler_class,
            async: async, options: options }

  @mutex.synchronize { _subscriptions << entry }

  bus.subscribe(pattern) { |event| _dispatch(event, entry) }
end

.reset!Object

Reset the registry: drop all subscriptions and replace the bus with a fresh Memory instance. Intended for use in tests (called in before/after hooks).



75
76
77
78
79
80
# File 'lib/easyop/events/registry.rb', line 75

def reset!
  @mutex.synchronize do
    @bus           = nil
    @subscriptions = nil
  end
end

.subscriptionsArray<Hash>

Returns a copy of all registered handler entries (for introspection).

Returns:

  • (Array<Hash>)


69
70
71
# File 'lib/easyop/events/registry.rb', line 69

def subscriptions
  @mutex.synchronize { _subscriptions.dup }
end