Class: Karafka::Core::Instrumentation::CallbacksManager

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/core/instrumentation/callbacks_manager.rb

Overview

This manager allows us to register multiple callbacks into a hook that is suppose to support a single callback

Instance Method Summary collapse

Constructor Details

#initialize::Karafka::Core::Instrumentation::CallbacksManager



11
12
13
# File 'lib/karafka/core/instrumentation/callbacks_manager.rb', line 11

def initialize
  @callbacks = {}
end

Instance Method Details

#add(id, callable) ⇒ Object

Adds a callback to the manager

Parameters:

  • id (String)

    id of the callback (used when deleting it)

  • callable (#call)

    object that responds to a #call method



30
31
32
# File 'lib/karafka/core/instrumentation/callbacks_manager.rb', line 30

def add(id, callable)
  @callbacks[id] = callable
end

#call(*args) ⇒ Object

Note:

We do not use #each_value here on purpose. With it being used, we cannot dispatch callbacks and add new at the same time. Since we don’t know when and in what thread things are going to be added to the manager, we need to extract values into an array and run it. That way we can add new things the same time.

Invokes all the callbacks registered one after another

Parameters:

  • args (Object)

    any args that should go to the callbacks



22
23
24
# File 'lib/karafka/core/instrumentation/callbacks_manager.rb', line 22

def call(*args)
  @callbacks.values.each { |callback| callback.call(*args) }
end

#delete(id) ⇒ Object

Removes the callback from the manager

Parameters:

  • id (String)

    id of the callback we want to remove



36
37
38
# File 'lib/karafka/core/instrumentation/callbacks_manager.rb', line 36

def delete(id)
  @callbacks.delete(id)
end