Class: Zizq::Lifecycle

Inherits:
Object
  • Object
show all
Defined in:
lib/zizq/lifecycle.rb,
sig/generated/zizq/lifecycle.rbs

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

Constructor Details

#initializeLifecycle

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.

Returns:

  • (Object)


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?Object

Non-blocking, lock-free check.

Returns:

  • (Object)


28
29
30
# File 'lib/zizq/lifecycle.rb', line 28

def running? #: () -> bool
  @state == :running
end

#stop!Object

Transition to :stopped.

Returns:

  • (Object)


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_stoppedObject

Block until the state is :stopped.

Returns:

  • (Object)


54
55
56
# File 'lib/zizq/lifecycle.rb', line 54

def wait_until_stopped #: () -> void
  @stop_latch.pop
end

#wait_while_runningObject

Block until the state is no longer :running.

Returns:

  • (Object)


49
50
51
# File 'lib/zizq/lifecycle.rb', line 49

def wait_while_running #: () -> void
  @drain_latch.pop
end