Class: EventSourcery::Postgres::OptimisedEventPollWaiter

Inherits:
Object
  • Object
show all
Defined in:
lib/event_sourcery/postgres/optimised_event_poll_waiter.rb

Overview

Optimise poll interval with Postgres listen/notify

Constant Summary collapse

ListenThreadDied =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(db_connection:, timeout: 30, after_listen: proc {}) ⇒ OptimisedEventPollWaiter

Returns a new instance of OptimisedEventPollWaiter.



7
8
9
10
11
12
# File 'lib/event_sourcery/postgres/optimised_event_poll_waiter.rb', line 7

def initialize(db_connection:, timeout: 30, after_listen: proc {})
  @db_connection = db_connection
  @timeout = timeout
  @events_queue = QueueWithIntervalCallback.new
  @after_listen = after_listen
end

Instance Method Details

#poll(after_listen: proc { }, &block) ⇒ Object



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

def poll(after_listen: proc { }, &block)
  @events_queue.callback = proc do
    ensure_listen_thread_alive!
    block.call
  end
  start_async(after_listen: after_listen)
  catch(:stop) do
    block.call
    loop do
      ensure_listen_thread_alive!
      wait_for_new_event_to_appear
      clear_new_event_queue
      block.call
    end
  end
ensure
  shutdown!
end