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.

RBS:

  • return: void



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
39
40
41
42
# File 'lib/zizq/lifecycle.rb', line 33

def drain! #: () -> void
  return unless @state == :running

  @state = :draining
  begin
    @drain_latch.close
  rescue StandardError
    nil
  end
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)


45
46
47
48
49
50
51
52
53
54
# File 'lib/zizq/lifecycle.rb', line 45

def stop! #: () -> void
  return if @state == :stopped

  @state = :stopped
  begin
    @stop_latch.close
  rescue StandardError
    nil
  end
end

#wait_until_stoppedObject

Block until the state is :stopped.

Returns:

  • (Object)


62
63
64
# File 'lib/zizq/lifecycle.rb', line 62

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

#wait_while_runningObject

Block until the state is no longer :running.

Returns:

  • (Object)


57
58
59
# File 'lib/zizq/lifecycle.rb', line 57

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