Module: Karafka::Constraints

Defined in:
lib/karafka/constraints.rb

Overview

Module used to check optional requirements (constraints) that cannot be easily defined by Bundler. Constraints are registered under one of two phases and verified centrally, so all environment requirements live and surface in one place:

  • :load - verified when karafka itself is required, for requirements independent of the configuration (like ecosystem gems version compatibility)
  • :config - verified during Karafka::App.setup right after the configuration is validated, for requirements that depend on what features are actually enabled (features register those themselves, keeping their specifics out of this generic module)

Class Method Summary collapse

Class Method Details

.register(name, phase:, &block) ⇒ Object

Registers a constraint for verification. Registrations are keyed by name, so re-registration (e.g. when setup runs multiple times in tests) overwrites a previous one instead of accumulating duplicates.

Parameters:

  • name (Symbol)

    unique constraint name

  • phase (Symbol)

    :load or :config

  • block (Proc)

    verification receiving the config node (nil in the :load phase) and expected to raise Karafka::Errors::DependencyConstraintsError when the requirement is not met

Raises:



29
30
31
32
33
# File 'lib/karafka/constraints.rb', line 29

def register(name, phase:, &block)
  raise(Errors::UnsupportedCaseError, phase) unless PHASES.include?(phase)

  constraints[phase][name] = block
end

.verify!(phase = :load, config = nil) ⇒ Object

Verifies all the constraints registered for a given phase

Parameters:

  • phase (Symbol) (defaults to: :load)

    :load or :config

  • config (Karafka::Core::Configurable::Node, nil) (defaults to: nil)

    config node for the :config phase, nil in the :load phase

Raises:



40
41
42
43
44
# File 'lib/karafka/constraints.rb', line 40

def verify!(phase = :load, config = nil)
  raise(Errors::UnsupportedCaseError, phase) unless PHASES.include?(phase)

  constraints[phase].each_value { |constraint| constraint.call(config) }
end