Class: Karafka::Routing::Builder

Inherits:
Array
  • Object
show all
Includes:
Singleton
Defined in:
lib/karafka/routing/builder.rb

Overview

Builder used as a DSL layer for building consumers and telling them which topics to consume

Examples:

Build a simple (most common) route

consumers do
  topic :new_videos do
    consumer NewVideosConsumer
  end
end

Instance Method Summary collapse

Instance Method Details

#activeArray<Karafka::Routing::ConsumerGroup>

Returns only active consumer groups that we want to use. Since Karafka supports multi-process setup, we need to be able to pick only those consumer groups that should be active in our given process context.

Returns:

  • (Array<Karafka::Routing::ConsumerGroup>)

    only active consumer groups that we want to use. Since Karafka supports multi-process setup, we need to be able to pick only those consumer groups that should be active in our given process context



38
39
40
# File 'lib/karafka/routing/builder.rb', line 38

def active
  select(&:active?)
end

#draw { ... } ⇒ Object

Note:

After it is done drawing it will store and validate all the routes to make sure that they are correct and that there are no topic/group duplications (this is forbidden)

Used to draw routes for Karafka

Examples:

draw do
  topic :xyz do
  end
end

Yields:

  • Evaluates provided block in a builder context so we can describe routes



24
25
26
27
28
29
30
31
32
33
# File 'lib/karafka/routing/builder.rb', line 24

def draw(&block)
  instance_eval(&block)

  each do |consumer_group|
    hashed_group = consumer_group.to_h
    validation_result = Karafka::Schemas::ConsumerGroup.call(hashed_group)
    return if validation_result.success?
    raise Errors::InvalidConfiguration, validation_result.errors
  end
end