Sha256: cc045b2651aec9ab4f3c66893104178528f9f4cb6c6c02db72fda35f56583de6

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module Karafka
  # Interchangers allow us to format/encode/pack data that is being send to perform_async
  # This is meant to target mostly issues with data encoding like this one:
  # https://github.com/mperham/sidekiq/issues/197
  # Each custom interchanger should implement following methods:
  #   - load - it is meant to encode params before they get stored inside Redis
  #   - parse - decoded params back to a hash format that we can use
  class Interchanger
    class << self
      # @param params_batch [Karafka::Params::ParamsBatch] Karafka params batch object
      # @note Params might not be parsed because of lazy loading feature. If you implement your
      #   own interchanger logic, this method needs to return data that can be converted to
      #   json with default Sidekiqs logic
      # @return [Karafka::Params::ParamsBatch] parsed params batch. There are too many problems
      #   with passing unparsed data from Karafka to Sidekiq, to make it a default. In case you
      #   need this, please implement your own interchanger.
      def load(params_batch)
        params_batch.parsed
      end

      # @param params_batch [Hash] Sidekiqs params that are now a Hash (after they were JSON#parse)
      # @note Since Sidekiq does not like symbols, we restore symbolized keys for system keys, so
      #   everything can work as expected. Keep in mind, that custom data will always be assigned
      #   with string keys per design. To change it, please change this interchanger and create
      #   your own custom parser
      def parse(params_batch)
        params_batch.map! do |params|
          Karafka::Params::Params::SYSTEM_KEYS.each do |key|
            stringified_key = key.to_s
            next unless params.key?(stringified_key)
            params[key] ||= params.delete(stringified_key)
          end

          params
        end

        params_batch
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
karafka-sidekiq-backend-1.2.0.beta2 lib/karafka/interchanger.rb