Class: Karafka::Routing::ActivityManager

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

Overview

Allows us to get track of which consumer groups, subscription groups and topics are enabled or disabled via CLI

Constant Summary collapse

SUPPORTED_TYPES =

Supported types of inclusions and exclusions

%i[
  consumer_groups
  subscription_groups
  topics
].freeze

Instance Method Summary collapse

Constructor Details

#initializeActivityManager

Returns a new instance of ActivityManager.



15
16
17
18
# File 'lib/karafka/routing/activity_manager.rb', line 15

def initialize
  @included = Hash.new { |h, k| h[k] = [] }
  @excluded = Hash.new { |h, k| h[k] = [] }
end

Instance Method Details

#active?(type, name) ⇒ Boolean

Returns is the given resource active or not.

Parameters:

  • type (Symbol)

    type for inclusion

  • name (String)

    name of the element

Returns:

  • (Boolean)

    is the given resource active or not



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/karafka/routing/activity_manager.rb', line 41

def active?(type, name)
  validate!(type)

  included = @included[type]
  excluded = @excluded[type]

  # If nothing defined, all active by default
  return true if included.empty? && excluded.empty?
  # Inclusion supersedes exclusion in case someone wrote both
  return true if !included.empty? && included.include?(name)

  # If there are exclusions but our is not excluded and no inclusions or included, it's ok
  !excluded.empty? &&
    !excluded.include?(name) &&
    (included.empty? || included.include?(name))
end

#clearObject

Clears the manager



67
68
69
70
# File 'lib/karafka/routing/activity_manager.rb', line 67

def clear
  @included.clear
  @excluded.clear
end

#exclude(type, name) ⇒ Object

Adds resource to excluded

Parameters:

  • type (Symbol)

    type for inclusion

  • name (String)

    name of the element



32
33
34
35
36
# File 'lib/karafka/routing/activity_manager.rb', line 32

def exclude(type, name)
  validate!(type)

  @excluded[type] << name
end

#include(type, name) ⇒ Object

Adds resource to included/active

Parameters:

  • type (Symbol)

    type for inclusion

  • name (String)

    name of the element



23
24
25
26
27
# File 'lib/karafka/routing/activity_manager.rb', line 23

def include(type, name)
  validate!(type)

  @included[type] << name
end

#to_hHash

Returns accumulated data in a hash for validations.

Returns:

  • (Hash)

    accumulated data in a hash for validations



59
60
61
62
63
64
# File 'lib/karafka/routing/activity_manager.rb', line 59

def to_h
  (
    SUPPORTED_TYPES.map { |type| ["include_#{type}".to_sym, @included[type]] } +
    SUPPORTED_TYPES.map { |type| ["exclude_#{type}".to_sym, @excluded[type]] }
  ).to_h
end