Class: Karafka::Routing::ConsumerGroup
- Inherits:
-
Object
- Object
- Karafka::Routing::ConsumerGroup
- Defined in:
- lib/karafka/routing/consumer_group.rb
Overview
A single consumer group represents Kafka consumer group, but it may not match 1:1 with subscription groups. There can be more subscription groups than consumer groups
Object used to describe a single consumer group that is going to subscribe to given topics It is a part of Karafka’s DSL
Instance Attribute Summary collapse
-
#current_subscription_group_id ⇒ Object
This is a “virtual” attribute that is not building subscription groups.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#topics ⇒ Object
readonly
Returns the value of attribute topics.
Instance Method Summary collapse
-
#active? ⇒ Boolean
True if this consumer group should be active in our current process.
-
#initialize(name) ⇒ ConsumerGroup
constructor
A new instance of ConsumerGroup.
-
#subscription_group=(name = SubscriptionGroup.id, &block) ⇒ Object
Assigns the current subscription group id based on the defined one and allows for further topic definition.
-
#subscription_groups ⇒ Array<Routing::SubscriptionGroup>
All the subscription groups build based on the consumer group topics.
-
#to_h ⇒ Hash
Hashed version of consumer group that can be used for validation purposes topics inside of it.
-
#topic=(name, &block) ⇒ Karafka::Routing::Topic
Builds a topic representation inside of a current consumer group route.
Constructor Details
#initialize(name) ⇒ ConsumerGroup
Returns a new instance of ConsumerGroup.
23 24 25 26 27 28 29 30 |
# File 'lib/karafka/routing/consumer_group.rb', line 23 def initialize(name) @name = name.to_s @id = Karafka::App.config.consumer_mapper.call(name) @topics = Topics.new([]) # Initialize the subscription group so there's always a value for it, since even if not # defined directly, a subscription group will be created @current_subscription_group_id = SubscriptionGroup.id end |
Instance Attribute Details
#current_subscription_group_id ⇒ Object
This is a “virtual” attribute that is not building subscription groups. It allows us to store the “current” subscription group defined in the routing This subscription group id is then injected into topics, so we can compute the subscription groups
17 18 19 |
# File 'lib/karafka/routing/consumer_group.rb', line 17 def current_subscription_group_id @current_subscription_group_id end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/karafka/routing/consumer_group.rb', line 11 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/karafka/routing/consumer_group.rb', line 11 def name @name end |
#topics ⇒ Object (readonly)
Returns the value of attribute topics.
11 12 13 |
# File 'lib/karafka/routing/consumer_group.rb', line 11 def topics @topics end |
Instance Method Details
#active? ⇒ Boolean
Returns true if this consumer group should be active in our current process.
33 34 35 36 37 38 |
# File 'lib/karafka/routing/consumer_group.rb', line 33 def active? cgs = Karafka::App.config.internal.routing.active.consumer_groups # When empty it means no groups were specified, hence all should be used cgs.empty? || cgs.include?(name) end |
#subscription_group=(name = SubscriptionGroup.id, &block) ⇒ Object
Assigns the current subscription group id based on the defined one and allows for further topic definition
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/karafka/routing/consumer_group.rb', line 58 def subscription_group=(name = SubscriptionGroup.id, &block) # We cast it here, so the routing supports symbol based but that's anyhow later on # validated as a string @current_subscription_group_id = name Proxy.new(self, &block) # We need to reset the current subscription group after it is used, so it won't leak # outside to other topics that would be defined without a defined subscription group @current_subscription_group_id = SubscriptionGroup.id end |
#subscription_groups ⇒ Array<Routing::SubscriptionGroup>
Returns all the subscription groups build based on the consumer group topics.
72 73 74 75 76 77 78 79 |
# File 'lib/karafka/routing/consumer_group.rb', line 72 def subscription_groups @subscription_groups ||= App .config .internal .routing .subscription_groups_builder .call(topics) end |
#to_h ⇒ Hash
Hashed version of consumer group that can be used for validation purposes topics inside of it.
84 85 86 87 88 89 |
# File 'lib/karafka/routing/consumer_group.rb', line 84 def to_h { topics: topics.map(&:to_h), id: id }.freeze end |
#topic=(name, &block) ⇒ Karafka::Routing::Topic
Builds a topic representation inside of a current consumer group route
44 45 46 47 48 49 50 51 52 |
# File 'lib/karafka/routing/consumer_group.rb', line 44 def topic=(name, &block) topic = Topic.new(name, self) @topics << Proxy.new(topic, &block).target built_topic = @topics.last # We overwrite it conditionally in case it was not set by the user inline in the topic # block definition built_topic.subscription_group ||= current_subscription_group_id built_topic end |