Class: Karafka::BaseWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
lib/karafka/base_worker.rb

Overview

Worker wrapper for Sidekiq workers

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.base_workerClass

Returns the base worker class for application.

Returns:

  • (Class)

    first worker that inherited from Karafka::BaseWorker. Karafka assumes that it is the base worker for an application.

Raises:



15
16
17
# File 'lib/karafka/base_worker.rb', line 15

def base_worker
  @inherited || raise(Errors::BaseWorkerDescentantMissing)
end

.inherited(subclass) ⇒ Class

Returns subclass of the worker that was selected.

Parameters:

  • subclass (Class)

    subclass of the worker

Returns:

  • (Class)

    subclass of the worker that was selected



21
22
23
# File 'lib/karafka/base_worker.rb', line 21

def inherited(subclass)
  @inherited ||= subclass
end

Instance Method Details

#perform(topic_id, params_batch, metadata) ⇒ Object

Executes the logic that lies in #perform Karafka consumer method

Parameters:

  • topic_id (String)

    Unique topic id that we will use to find a proper topic

  • params_batch (Array<Hash>)

    Array with messages batch

  • metadata (Hash, nil)

    hash with all the metadata or nil if not present



30
31
32
33
34
35
36
37
38
# File 'lib/karafka/base_worker.rb', line 30

def perform(topic_id, params_batch, )
  consumer = consumer(topic_id, params_batch, )

  Karafka.monitor.instrument(
    'backends.sidekiq.base_worker.perform',
    caller: self,
    consumer: consumer
  ) { consumer.consume }
end