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

Initializes the activity manager with empty inclusion and exclusion lists



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

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



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

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



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

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



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

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



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

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



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

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