Class: Karafka::ExecutionMode

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/execution_mode.rb

Overview

Represents the execution mode state of the Karafka server. Provides a cleaner API for checking and setting execution modes, encapsulating the mode logic and removing the need for symbol comparisons throughout the codebase.

Examples:

Check if running in swarm mode

Server.execution_mode.swarm? #=> false

Set the execution mode to embedded

Server.execution_mode.embedded!

Instance Method Summary collapse

Constructor Details

#initialize(mode = :standalone) ⇒ ExecutionMode

Returns a new instance of ExecutionMode.

Parameters:

  • mode (Symbol) (defaults to: :standalone)

    initial execution mode (defaults to :standalone)

Raises:

  • (ArgumentError)

    when invalid mode is provided



30
31
32
# File 'lib/karafka/execution_mode.rb', line 30

def initialize(mode = :standalone)
  self.mode = mode
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the execution mode with another object. Supports comparison with symbols, strings, and other ExecutionMode instances for backward compatibility with existing code.

Examples:

Compare with symbol

execution_mode == :standalone #=> true

Compare with string

execution_mode == 'standalone' #=> true

Compare with another ExecutionMode

execution_mode == other_mode #=> true/false

Parameters:

  • other (Symbol, String, ExecutionMode)

    object to compare with

Returns:

  • (Boolean)

    true if the modes are equivalent



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/karafka/execution_mode.rb', line 75

def ==(other)
  case other
  when Symbol
    @mode == other
  when String
    @mode.to_s == other
  when ExecutionMode
    @mode == other.to_sym
  else
    false
  end
end

#to_sString

Returns string representation of the current mode.

Returns:

  • (String)

    string representation of the current mode



51
52
53
# File 'lib/karafka/execution_mode.rb', line 51

def to_s
  @mode.to_s
end

#to_symSymbol

Returns symbol representation of the current mode.

Returns:

  • (Symbol)

    symbol representation of the current mode



56
57
58
# File 'lib/karafka/execution_mode.rb', line 56

def to_sym
  @mode
end