Class: EventSourcery::EventStore::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/event_sourcery/event_store/subscription.rb

Overview

This allows Event Stream Processors (ESPs) to subscribe to an event store, and be notified when new events are added.

Instance Method Summary collapse

Constructor Details

#initialize(event_store:, poll_waiter:, from_event_id:, event_types: nil, on_new_events:, subscription_master:, events_table_name: :events, batch_size: EventSourcery.config.subscription_batch_size) ⇒ Subscription

Returns a new instance of Subscription.

Parameters:

  • event_store

    Event store to source events from

  • poll_waiter

    Poll waiter instance used (such as PollWaiter) for polling the event store

  • from_event_id (Integer)

    Start reading events from this event ID

  • event_types (Array) (defaults to: nil)

    Optional. If specified, only subscribe to given event types.

  • on_new_events (Proc)

    Code block to be executed when new events are received

  • subscription_master

    A subscription master instance (such as EventSourcery::EventStore::SignalHandlingSubscriptionMaster) which orchestrates a graceful shutdown of the subscription, if one is requested.

  • events_table_name (Symbol) (defaults to: :events)

    Optional. Defaults to ‘:events`



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/event_sourcery/event_store/subscription.rb', line 15

def initialize(event_store:,
               poll_waiter:,
               from_event_id:,
               event_types: nil,
               on_new_events:,
               subscription_master:,
               events_table_name: :events,
               batch_size: EventSourcery.config.subscription_batch_size)
  @event_store = event_store
  @from_event_id = from_event_id
  @poll_waiter = poll_waiter
  @event_types = event_types
  @on_new_events = on_new_events
  @subscription_master = subscription_master
  @current_event_id = from_event_id - 1
  @batch_size = batch_size
end

Instance Method Details

#startObject

Start listening for new events. This method will continue to listen for new events until a shutdown is requested through the subscription_master provided.



37
38
39
40
41
42
43
# File 'lib/event_sourcery/event_store/subscription.rb', line 37

def start
  catch(:stop) do
    @poll_waiter.poll do
      read_events
    end
  end
end