Class: WaterDrop::Producer::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/waterdrop/producer/status.rb

Overview

Producer lifecycle status object representation

Constant Summary collapse

ACTIVE_STATES =

States in which the producer is considered active and able to accept work. Kept as a single set so the current state can be classified in one atomic read (see ‘#active?` / `#to_sym`) rather than via a chain of predicate calls that could straddle a concurrent transition.

%i[
  connected
  configured
  disconnecting
  disconnected
].freeze

Instance Method Summary collapse

Constructor Details

#initializeStatus

Creates a new instance of status with the initial state



32
33
34
# File 'lib/waterdrop/producer/status.rb', line 32

def initialize
  @current = LIFECYCLE.first
end

Instance Method Details

#active?Boolean

Returns true if producer is in a active state. Active means, that we can start sending messages. Active states are connected (connection established), configured which means, that producer is configured, but connection with Kafka is not yet established or disconnected, meaning it was working but user disconnected for his own reasons though sending could reconnect and continue.

Returns:

  • (Boolean)

    true if producer is in a active state. Active means, that we can start sending messages. Active states are connected (connection established), configured which means, that producer is configured, but connection with Kafka is not yet established or disconnected, meaning it was working but user disconnected for his own reasons though sending could reconnect and continue.



41
42
43
44
45
46
# File 'lib/waterdrop/producer/status.rb', line 41

def active?
  # Single read of @current so a concurrent transition cannot make this return false for a
  # status that is in fact active (for example flipping configured -> connected mid-check
  # while another thread reloads the client after a fatal error).
  ACTIVE_STATES.include?(@current)
end

#to_sString

Returns current status as a string.

Returns:

  • (String)

    current status as a string



49
50
51
# File 'lib/waterdrop/producer/status.rb', line 49

def to_s
  @current.to_s
end

#to_symSymbol

Returns current lifecycle state captured as a single atomic read. Lets callers branch on one consistent value instead of issuing several predicate calls that could observe different states if the producer is transitioning on another thread.

Returns:

  • (Symbol)

    current lifecycle state captured as a single atomic read. Lets callers branch on one consistent value instead of issuing several predicate calls that could observe different states if the producer is transitioning on another thread.



56
57
58
# File 'lib/waterdrop/producer/status.rb', line 56

def to_sym
  @current
end