Active Support 结构化事件订阅器¶ ↑
ActiveSupport::StructuredEventSubscriber 通过 ActiveSupport::Notifications 消费事件,以便通过 Rails.event 发射结构化事件。
一个例子是 Action Controller 结构化事件订阅器,它负责发射请求处理事件。
module ActionController class StructuredEventSubscriber < ActiveSupport::StructuredEventSubscriber attach_to :action_controller def start_processing(event) emit_event("controller.request_started", controller: event.payload[:controller], action: event.payload[:action], format: event.payload[:format] ) end end end
配置完成后,每当发布一个 "start_processing.action_controller" 通知时,它将正确地将事件 (ActiveSupport::Notifications::Event) 分派到 start_processing 方法。然后,订阅器可以通过 emit_event 方法发射一个结构化事件。
方法
- C
- E
- N
- S
常量
| DEBUG_CHECK | = | proc { !ActiveSupport.event_reporter.debug_mode? } |
类公共方法
new() 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/structured_event_subscriber.rb, line 56 def initialize super @silenced_events = {} end
实例公共方法
call(event) 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/structured_event_subscriber.rb, line 88 def call(event) super rescue => e handle_event_error(event.name, e) end
emit_debug_event(name, payload = nil, caller_depth: 1, **kwargs) 链接
类似于 emit_event,但仅在事件报告器处于调试模式时发射。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/structured_event_subscriber.rb, line 82 def emit_debug_event(name, payload = nil, caller_depth: 1, **kwargs) ActiveSupport.event_reporter.debug(name, payload, caller_depth: caller_depth + 1, **kwargs) rescue => e handle_event_error(name, e) end
emit_event(name, payload = nil, caller_depth: 1, **kwargs) 链接
通过 Rails.event.notify 发射一个结构化事件。
参数¶ ↑
-
name- 事件名称,字符串或符号。 -
payload- 事件载荷,哈希或对象。 -
caller_depth- 源位置的堆栈深度(默认为 1)。 -
kwargs- 与载荷哈希合并的其他载荷数据。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/structured_event_subscriber.rb, line 75 def emit_event(name, payload = nil, caller_depth: 1, **kwargs) ActiveSupport.event_reporter.notify(name, payload, caller_depth: caller_depth + 1, **kwargs) rescue => e handle_event_error(name, e) end
silenced?(event) 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/structured_event_subscriber.rb, line 61 def silenced?(event) ActiveSupport.event_reporter.subscribers.none? || @silenced_events[event]&.call end