Class: Zizq::Lifecycle
- Inherits:
-
Object
- Object
- Zizq::Lifecycle
- Defined in:
- lib/zizq/lifecycle.rb
Overview
Thread-safe state machine for coordinating worker shutdown.
States:
:running → normal operation
:draining → stop accepting work, finish in-progress jobs
:stopped → all work drained, safe to disconnect
Transitions: running -> draining -> stopped (forward only).
All transitions are signal-trap safe — they use only atomic symbol assignment and Queue#close for wakeups.
Instance Method Summary collapse
-
#drain! ⇒ Object
Transition to :draining.
-
#initialize ⇒ Lifecycle
constructor
A new instance of Lifecycle.
-
#running? ⇒ Boolean
Non-blocking, lock-free check.
-
#stop! ⇒ Object
Transition to :stopped.
-
#wait_until_stopped ⇒ Object
Block until the state is :stopped.
-
#wait_while_running ⇒ Object
Block until the state is no longer :running.
Constructor Details
#initialize ⇒ Lifecycle
Returns a new instance of Lifecycle.
21 22 23 24 25 |
# File 'lib/zizq/lifecycle.rb', line 21 def initialize @state = :running #: :running | :draining | :stopped @drain_latch = Thread::Queue.new @stop_latch = Thread::Queue.new end |
Instance Method Details
#drain! ⇒ Object
Transition to :draining.
33 34 35 36 37 38 |
# File 'lib/zizq/lifecycle.rb', line 33 def drain! #: () -> void return unless @state == :running @state = :draining @drain_latch.close rescue nil end |
#running? ⇒ Boolean
Non-blocking, lock-free check.
28 29 30 |
# File 'lib/zizq/lifecycle.rb', line 28 def running? #: () -> bool @state == :running end |
#stop! ⇒ Object
Transition to :stopped.
41 42 43 44 45 46 |
# File 'lib/zizq/lifecycle.rb', line 41 def stop! #: () -> void return if @state == :stopped @state = :stopped @stop_latch.close rescue nil end |
#wait_until_stopped ⇒ Object
Block until the state is :stopped.
54 55 56 |
# File 'lib/zizq/lifecycle.rb', line 54 def wait_until_stopped #: () -> void @stop_latch.pop end |
#wait_while_running ⇒ Object
Block until the state is no longer :running.
49 50 51 |
# File 'lib/zizq/lifecycle.rb', line 49 def wait_while_running #: () -> void @drain_latch.pop end |