Class: Karafka::BaseController

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::DescendantsTracker
Includes:
ActiveSupport::Callbacks
Defined in:
lib/karafka/base_controller.rb

Overview

Base controller from which all Karafka controllers should inherit Similar to Rails controllers we can define before_enqueue callbacks that will be executed

Note that if before_enqueue return false, the chain will be stopped and

the perform method won't be executed in sidekiq (won't peform_async it)

Examples:

Create simple controller

class ExamplesController < Karafka::BaseController
  def perform
    # some logic here
  end
end

Create a controller with a block before_enqueue

class ExampleController < Karafka::BaseController
  before_enqueue do
    # Here we should have some checking logic
    # If false is returned, won't schedule a perform action
  end

  def perform
    # some logic here
  end
end

Create a controller with a method before_enqueue

class ExampleController < Karafka::BaseController
  before_enqueue :before_method

  def perform
    # some logic here
  end

  private

  def before_method
    # Here we should have some checking logic
    # If false is returned, won't schedule a perform action
  end
end

Create a controller with an after_failure action

class ExampleController < Karafka::BaseController
  def perform
    # some logic here
  end

  def after_failure
    # action taken in case perform fails
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.before_enqueue(method_name = nil) { ... } ⇒ Object

Note:

If value returned is false, will chalt the chain and not schedlue to Sidekiq

Creates a callback that will be executed before scheduling to Sidekiq

Examples:

Define a block before_enqueue callback

before_enqueue do
  # logic here
end

Define a class name before_enqueue callback

before_enqueue :method_name

Parameters:

  • method_name (Symbol, String) (defaults to: nil)

    method name or nil if we plan to provide a block

Yields:

  • A block with a code that should be executed before scheduling



89
90
91
# File 'lib/karafka/base_controller.rb', line 89

def before_enqueue(method_name = nil, &block)
  set_callback :schedule, :before, method_name ? method_name : block
end

Instance Method Details

#params=(message) ⇒ Object

Note:

Until first params usage, it won’t parse data at all

Creates lazy loaded params object

Parameters:



98
99
100
# File 'lib/karafka/base_controller.rb', line 98

def params=(message)
  @params = Karafka::Params::Params.build(message, self)
end

#performObject

Note:

This method needs bo be implemented in a subclass. We stub it here as a failover if someone forgets about it or makes on with typo

Method that will perform business logic on data received from Kafka

Raises:

  • (NotImplementedError)


121
122
123
# File 'lib/karafka/base_controller.rb', line 121

def perform
  raise NotImplementedError, 'Implement this in a subclass'
end

#scheduleObject

Executes the default controller flow, runs callbacks and if not halted will schedule a perform task in sidekiq



104
105
106
107
108
# File 'lib/karafka/base_controller.rb', line 104

def schedule
  run_callbacks :schedule do
    inline ? perform_inline : perform_async
  end
end

#to_hHash

Returns hash with all controller details - it works similar to #params method however it won’t parse data so it will return unparsed details about controller and its parameters.

Examples:

Get data about ctrl

ctrl.to_h #=> { "worker"=>WorkerClass, "parsed"=>false, "content"=>"{}" }

Returns:

  • (Hash)

    hash with all controller details - it works similar to #params method however it won’t parse data so it will return unparsed details about controller and its parameters



114
115
116
# File 'lib/karafka/base_controller.rb', line 114

def to_h
  @params
end