Module: TrackRelay::Dispatcher

Defined in:
lib/track_relay/dispatcher.rb

Overview

Single ActiveSupport::Notifications subscription that fans ‘track_relay.event` notifications out to every subscriber in Configuration#subscribers.

**Error contract — collect-then-reraise (locked in 01-CONTEXT.md):**

  1. Iterate every configured subscriber, calling ‘handle(payload)`.

  2. Subscribers::Base#handle returns ‘nil` on success or the StandardError on a sync failure (it never re-raises inline). For non-Base subscribers that ignore the contract and raise inline, a defensive rescue here preserves the “peers still run” invariant — exceptions from those rogues are also collected.

  3. AFTER fan-out completes, if Configuration#swallow_subscriber_errors is ‘false` AND any exception was collected, re-raise the first one. This is the locked dev/test loudness rule: surface failures, but only after every peer has had its chance to receive the event.

Lifecycle: Dispatcher.start! registers a single subscription block; Dispatcher.stop! unsubscribes. Both are idempotent so the Plan 06 Railtie can call ‘start!` once at boot without worrying about double-subscription. Dispatcher.started? reports the current state.

‘lib/track_relay.rb` requires this file but does NOT call `start!` — only the Railtie does, so non-Rails environments can opt in.

Constant Summary collapse

NOTIFICATION =
"track_relay.event"

Class Method Summary collapse

Class Method Details

.start!(notifier = ActiveSupport::Notifications) ⇒ Object

Register the AS::Notifications subscription. Idempotent: calling twice does not double-subscribe.

Parameters:

  • notifier (#subscribe) (defaults to: ActiveSupport::Notifications)

    defaults to ActiveSupport::Notifications

Returns:

  • (Object)

    the subscription handle (opaque AS object)



40
41
42
43
44
45
# File 'lib/track_relay/dispatcher.rb', line 40

def start!(notifier = ActiveSupport::Notifications)
  return @subscription if @subscription
  @subscription = notifier.subscribe(NOTIFICATION) do |*, payload|
    dispatch(payload[:event])
  end
end

.started?Boolean

Returns whether a subscription is currently registered.

Returns:

  • (Boolean)

    whether a subscription is currently registered



59
60
61
# File 'lib/track_relay/dispatcher.rb', line 59

def started?
  !@subscription.nil?
end

.stop!(notifier = ActiveSupport::Notifications) ⇒ void

This method returns an undefined value.

Unsubscribe the AS::Notifications subscription. Idempotent — safe to call when no subscription has been registered.

Parameters:

  • notifier (#unsubscribe) (defaults to: ActiveSupport::Notifications)

    defaults to ActiveSupport::Notifications



52
53
54
55
56
# File 'lib/track_relay/dispatcher.rb', line 52

def stop!(notifier = ActiveSupport::Notifications)
  return unless @subscription
  notifier.unsubscribe(@subscription)
  @subscription = nil
end