All Projects → karafka → sidekiq-backend

karafka / sidekiq-backend

Licence: MIT license
Karafka Sidekiq backend for background messages processing

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to sidekiq-backend

example-apps
Karafka WaterDrop and Sidekiq backend example application
Stars: ✭ 33 (-52.17%)
Mutual labels:  karafka-framework, karafka
Dry Container
A simple, configurable object container implemented in Ruby
Stars: ✭ 240 (+247.83%)
Mutual labels:  rubygem
Activeinteractor
An implementation of the command pattern for Ruby with ActiveModel::Validations inspired by the interactor gem. Rich support for attributes, callbacks, and validations, and thread safe performance methods.
Stars: ✭ 118 (+71.01%)
Mutual labels:  rubygem
Acts as favoritor
Adds Favorite, Follow, Vote, etc. functionality to ActiveRecord models
Stars: ✭ 165 (+139.13%)
Mutual labels:  rubygem
Five Star
⭐️ FiveStar ⭐️ is a library to build a rating system in Ruby
Stars: ✭ 123 (+78.26%)
Mutual labels:  rubygem
Rom Sql
SQL support for rom-rb
Stars: ✭ 169 (+144.93%)
Mutual labels:  rubygem
Rails server timings
Server Timing headers for Rails apps
Stars: ✭ 112 (+62.32%)
Mutual labels:  rubygem
oanda api
A ruby client for the Oanda REST API.
Stars: ✭ 35 (-49.28%)
Mutual labels:  rubygem
Api Fuzzer
API Fuzzer which allows to fuzz request attributes using common pentesting techniques and lists vulnerabilities
Stars: ✭ 238 (+244.93%)
Mutual labels:  rubygem
Tty Box
Draw various frames and boxes in your terminal window
Stars: ✭ 161 (+133.33%)
Mutual labels:  rubygem
Socksify Ruby
Redirect any TCP connection initiated by a Ruby script through a SOCKS5 proxy
Stars: ✭ 146 (+111.59%)
Mutual labels:  rubygem
Notifications Rails
A flexible notification library supporting the delivery to external services, rendering in various environments, and user configuration by category.
Stars: ✭ 130 (+88.41%)
Mutual labels:  rubygem
Zold
An Experimental Non-Blockchain Cryptocurrency for Fast Micro Payments
Stars: ✭ 183 (+165.22%)
Mutual labels:  rubygem
Dry Logic
Predicate logic with rule composition
Stars: ✭ 118 (+71.01%)
Mutual labels:  rubygem
rack-session-smart cookie
Slightly smarter session cookies for Rack 2 apps
Stars: ✭ 43 (-37.68%)
Mutual labels:  rubygem
Apple id
Sign in with Apple backend library in Ruby
Stars: ✭ 115 (+66.67%)
Mutual labels:  rubygem
Dry Rails
The official dry-rb railtie
Stars: ✭ 142 (+105.8%)
Mutual labels:  rubygem
Asciidoctor Epub3
📘 Asciidoctor EPUB3 is a set of Asciidoctor extensions for converting AsciiDoc to EPUB3 & KF8/MOBI
Stars: ✭ 166 (+140.58%)
Mutual labels:  rubygem
rarbg
Ruby client for the RARBG Torrent API.
Stars: ✭ 17 (-75.36%)
Mutual labels:  rubygem
glimmer-dsl-opal
Glimmer DSL for Opal (Pure-Ruby Web GUI and Auto-Webifier of Desktop Apps)
Stars: ✭ 22 (-68.12%)
Mutual labels:  rubygem

Karafka Sidekiq Backend

Deprecation notice

This backend was designed to compensate for lack of multi-threading in Karafka. Karafka 2.0 is multi-threaded.

We will still support it for Karafka 1.4 but it won't work with Karafka 2.0.

About

Build Status Gem Version Join the chat at https://slack.karafka.io

Karafka Sidekiq Backend provides support for consuming (processing) received Kafka messages inside of Sidekiq workers.

Installations

Add this to your gemfile:

gem 'karafka-sidekiq-backend'

and create a file called application_worker.rb inside of your app/workers directory, that looks like that:

class ApplicationWorker < Karafka::BaseWorker
end

and you are ready to go. Karafka Sidekiq Backend integrates with Karafka automatically

Note: You can name your application worker base class with any name you want. The only thing that is required is a direct inheritance from the Karafka::BaseWorker class.

Usage

If you want to process messages with Sidekiq backend, you need to tell this to Karafka.

To do so, you can either configure that in a configuration block:

class App < Karafka::App
  setup do |config|
    config.backend = :sidekiq
    # Other config options...
  end
end

or on a per topic level:

App.routes.draw do
  consumer_group :videos_consumer do
    topic :binary_video_details do
      backend :sidekiq
      consumer Videos::DetailsConsumer
      worker Workers::DetailsWorker
      interchanger Interchangers::MyCustomInterchanger.new
    end
  end
end

You don't need to do anything beyond that. Karafka will know, that you want to run your consumer's #consume method in a background job.

Configuration

There are two options you can set inside of the topic block:

Option Value type Description
worker Class Name of a worker class that we want to use to schedule perform code
interchanger Instance Instance of an interchanger class that we want to use to pass the incoming data to Sidekiq

Workers

Karafka by default will build a worker that will correspond to each of your consumers (so you will have a pair - consumer and a worker). All of them will inherit from ApplicationWorker and will share all its settings.

To run Sidekiq you should have sidekiq.yml file in config folder. The example of sidekiq.yml file will be generated to config/sidekiq.yml.example once you run bundle exec karafka install.

However, if you want to use a raw Sidekiq worker (without any Karafka additional magic), or you want to use SidekiqPro (or any other queuing engine that has the same API as Sidekiq), you can assign your own custom worker:

topic :incoming_messages do
  consumer MessagesConsumer
  worker MyCustomWorker
end

Note that even then, you need to specify a consumer that will schedule a background task.

Custom workers need to provide a #perform_async method. It needs to accept two arguments:

  • topic_id - first argument is a current topic id from which a given message comes
  • params_batch - all the params that came from Kafka + additional metadata. This data format might be changed if you use custom interchangers. Otherwise, it will be an instance of Karafka::Params::ParamsBatch.

Note: If you use custom interchangers, keep in mind, that params inside params batch might be in two states: parsed or unparsed when passed to #perform_async. This means, that if you use custom interchangers and/or custom workers, you might want to look into Karafka's sources to see exactly how it works.

Interchangers

Custom interchangers target issues with non-standard (binary, etc.) data that we want to store when we do #perform_async. This data might be corrupted when fetched in a worker (see this issue). With custom interchangers, you can encode/compress data before it is being passed to scheduling and decode/decompress it when it gets into the worker.

To specify the interchanger for a topic, specify the interchanger inside routes like this:

App.routes.draw do
  consumer_group :videos_consumer do
    topic :binary_video_details do
      consumer Videos::DetailsConsumer
      interchanger Interchangers::MyCustomInterchanger.new
    end
  end
end

Each custom interchanger should define encode to encode params before they get stored in Redis, and decode to convert the params to hash format, as shown below:

class Base64Interchanger < ::Karafka::Interchanger
  def encode(params_batch)
    Base64.encode64(Marshal.dump(super))
  end

  def decode(params_string)
    Marshal.load(Base64.decode64(super))
  end
end

Warning: if you decide to use slow interchangers, they might significantly slow down Karafka.

References

Note on contributions

First, thank you for considering contributing to the Karafka ecosystem! It's people like you that make the open source community such a great community!

Each pull request must pass all the RSpec specs, integration tests and meet our quality requirements.

Fork it, update and wait for the Github Actions results.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].