Class: Karafka::BaseController
- Inherits:
-
Object
- Object
- Karafka::BaseController
- 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)
Class Method Summary collapse
-
.before_enqueue(method_name = nil) { ... } ⇒ Object
Creates a callback that will be executed before scheduling to Sidekiq.
Instance Method Summary collapse
-
#params=(message) ⇒ Object
Creates lazy loaded params object.
-
#perform ⇒ Object
Method that will perform business logic on data received from Kafka.
-
#schedule ⇒ Object
Executes the default controller flow, runs callbacks and if not halted will schedule a perform task in sidekiq.
-
#to_h ⇒ 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.
Class Method Details
.before_enqueue(method_name = nil) { ... } ⇒ Object
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
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
Until first params usage, it won’t parse data at all
Creates lazy loaded params object
98 99 100 |
# File 'lib/karafka/base_controller.rb', line 98 def params=() @params = Karafka::Params::Params.build(, self) end |
#perform ⇒ Object
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
121 122 123 |
# File 'lib/karafka/base_controller.rb', line 121 def perform raise NotImplementedError, 'Implement this in a subclass' end |
#schedule ⇒ Object
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_h ⇒ Hash
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.
114 115 116 |
# File 'lib/karafka/base_controller.rb', line 114 def to_h @params end |