Running with Ruby

Category: Rails (page 1 of 78)

Karafka framework 1.4.0 Release Notes (Ruby + Kafka)

This release mostly solves problems related to message deserialization and normalizes some of the naming conventions to ease during the upgrade to the upcoming 2.0 version.

Note: This release is the last release with ruby-kafka under the hood. We’ve already started the process of moving to rdkafka-ruby.

Note: If you are using Sidekiq-Backend plugin, please make sure that you’ve processed all the jobs from your Sidekiq queue before upgrading Karafka gems.

Changes (features, incompatibilities, etc)

consumer#metadata is now consumer#batch_metadata

This change is trivial: if you use batch consuming mode and you use the Consumer#metadata method, replace it with Consumer#batch_metadata.

# Karafka 1.3
class UsersConsumer < ApplicationConsumer
  def consume
    puts metadata
  end
end

# Karafka 1.4
class UsersConsumer < ApplicationConsumer
  def consume
    puts batch_metadata
  end
end

Message metadata available under #metadata method

Up to version 1.3, all the message metadata would be directly available under the root scope of the params object using both direct method reference as well as with #[] accessor.

While it felt like “The Rails way”, it had several side-effects, amongst which the biggest were the need of having a hash like API, issues with accessing metadata without payload deserialization, and a lack of clear separation between payload and the metadata.

From now on, you can use the params.metadata object to fetch all the metadata.

Note: we’ve preserved the direct metadata values fetching from the params object to preserve backwards compatibility.

# 1.3
params['partition'] #=> 0
params.partition #=> 0

# 1.4
params['partition'] #=> NoMethodError (undefined method '[]')

# This will work due to backward compatibility
params.partition #=> 0

# This is the recommended way of accessing metadata
params.metadata.partition #=> 0

# This will also work as metadata is a struct now
params.metadata[:partition] #=> 0
params.metadata['partition'] #=> 0

Message metadata access allowed without message deserialization

When accessing metadata, the payload is not being deserialized until #payload method is being used.

null message support in the default JSON deserializer

When the Kafka message payload is null / nil, deserialization won’t fail. Support for it was added as some of the Karafka users use log compaction with a nil payload. In case like that, #payload will return nil.

Karafka::Params::Params no longer inherits from a Hash

Karafka::Params::Params is now just a struct. This change is introduced to normalize the setup, limit the corner cases and simplify the interface only to methods that are really needed.

Documentation

Our Wiki has been updated accordingly to the 1.4 status. Please notify us if you find any incompatibilities.

Getting started with Karafka

If you want to get started with Kafka and Karafka as fast as possible, then the best idea is to just clone our example repository:

git clone https://github.com/karafka/example-app ./example_app

then, just bundle install all the dependencies:

cd ./example_app
bundle install

and follow the instructions from the example app Wiki.

NameError: undefined method ‘parse’ for class ‘NilClass’ when doing Time.zone.parse

If you get following error when trying to parse time:

Time.zone.parse('2019-01-01 11:11:11')

Traceback (most recent call last):
16: from /bundler/friendly_errors.rb:124:in `with_friendly_errors'
15: from /bundle:30:in `block in <top (required)>'
14: from /bundler/cli.rb:18:in `start'
13: from /bundler/vendor/thor/lib/thor/base.rb:466:in `start'
12: from /bundler/cli.rb:27:in `dispatch'
11: from /bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
10: from /bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
9: from /bundler/vendor/thor/lib/thor/command.rb:27:in `run'
8: from /bundler/cli.rb:465:in `exec'
7: from /bundler/cli/exec.rb:28:in `run'
6: from /bundler/cli/exec.rb:74:in `kernel_load'
5: from /bundler/cli/exec.rb:74:in `load'
4: from bin/irb:23:in `<top (required)>'
3: from bin/irb:23:in `load'
2: from lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
1: from (irb):5

NoMethodError (undefined method `parse' for nil:NilClass)

it means you’ve forgotten to set the timezone:

Time.zone = 'UTC'

Also, keep in mind, that each time you spin up a new thread, it won’t have a timezone defined (outside of Rails):

Time.zone = 'UTC'

Time.zone.parse('2019-01-01 11:11:11') # works

Thread.abort_on_exception = true

# fails
Thread.new do
  Time.zone.parse('2019-01-01 11:11:11')
end

# NoMethodError: undefined method `parse' for nil:NilClass

In order to fix that, you need to set the time zone in each of your newly created threads:

Time.zone = 'UTC'

Time.zone.parse('2019-01-01 11:11:11') # works

Thread.abort_on_exception = true

# works
Thread.new do
  Time.zone = 'UTC'
  Time.zone.parse('2019-01-01 11:11:11')
end

Cover photo by Alex The Shutter on Attribution-NonCommercial 2.0 Generic (CC BY-NC 2.0) license.

Olderposts

Copyright © 2020 Running with Ruby

Theme by Anders NorenUp ↑