Class: Events::Subscribers::Persister
- Inherits:
-
Object
- Object
- Events::Subscribers::Persister
- Includes:
- Events::Subscriber
- Defined in:
- lib/events/subscribers/persister.rb
Overview
Persists all events to SQLite as they flow through the event bus. Each event is written as an Event record belonging to the active session.
Instance Attribute Summary collapse
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
-
#emit(event) ⇒ Object
Receives a Rails.event notification hash and persists it.
-
#initialize(session) ⇒ Persister
constructor
A new instance of Persister.
Constructor Details
#initialize(session) ⇒ Persister
Returns a new instance of Persister.
17 18 19 20 |
# File 'lib/events/subscribers/persister.rb', line 17 def initialize(session) @session = session @mutex = Mutex.new end |
Instance Attribute Details
#session ⇒ Object
Returns the value of attribute session.
15 16 17 |
# File 'lib/events/subscribers/persister.rb', line 15 def session @session end |
Instance Method Details
#emit(event) ⇒ Object
Receives a Rails.event notification hash and persists it.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/events/subscribers/persister.rb', line 24 def emit(event) payload = event[:payload] return unless payload.is_a?(Hash) event_type = payload[:type] return if event_type.nil? @mutex.synchronize do @session.events.create!( event_type: event_type, payload: payload, tool_use_id: payload[:tool_use_id], timestamp: payload[:timestamp] || Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond) ) end end |