Class: Pgbus::Process::WakeSignal

Inherits:
Object
  • Object
show all
Defined in:
lib/pgbus/process/wake_signal.rb

Overview

Wake signal inspired by LavinMQ’s BoolChannel pattern. Replaces polling-based coordination with instant state-change signaling.

IMPORTANT: Single-waiter only. The wait method calls @event.reset immediately after waking, which means concurrent waiters may miss notifications. Callers must ensure only one thread calls wait at a time. In pgbus this is guaranteed because each Worker has exactly one main loop thread that calls wait, while notify! can be called from any thread (signal handlers, lifecycle transitions).

Usage:

signal = WakeSignal.new
# In worker thread (single waiter):
signal.wait(timeout: 5)  # blocks until signaled or timeout
# In another thread (e.g. signal handler, lifecycle transition):
signal.notify!           # wakes the waiting thread

Instance Method Summary collapse

Constructor Details

#initializeWakeSignal

Returns a new instance of WakeSignal.



24
25
26
# File 'lib/pgbus/process/wake_signal.rb', line 24

def initialize
  @event = Concurrent::Event.new
end

Instance Method Details

#notify!Object

Wake all waiting threads immediately.



38
39
40
# File 'lib/pgbus/process/wake_signal.rb', line 38

def notify!
  @event.set
end

#pending?Boolean

Check if a notification is pending without blocking.

Returns:

  • (Boolean)


43
44
45
# File 'lib/pgbus/process/wake_signal.rb', line 43

def pending?
  @event.set?
end

#reset!Object

Clear the pending notification.



48
49
50
# File 'lib/pgbus/process/wake_signal.rb', line 48

def reset!
  @event.reset
end

#wait(timeout: nil) ⇒ Object

Block until notify! is called or timeout expires. Returns true if signaled, false if timed out. Resets the event after waking — only safe with a single waiter.



31
32
33
34
35
# File 'lib/pgbus/process/wake_signal.rb', line 31

def wait(timeout: nil)
  result = @event.wait(timeout)
  @event.reset
  result
end