Sha256: df6f058848bc9d4be8529cc2917d98cb160e513f27a5b4ed408aabf4d5e16cdc

Contents?: true

Size: 1.51 KB

Versions: 12

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Karafka
  # Internal stuff related to workers
  module Workers
    # Builder is used to check if there is a proper consumer with the same name as
    # a consumer and if not, it will create a default one using Karafka::BaseWorker
    # This is used as a building layer between consumers and workers. it will be only used
    # when user does not provide his own worker that should perform consumer stuff
    class Builder
      # @param consumer_class [Karafka::BaseConsumer] descendant of Karafka::BaseConsumer
      # @example Create a worker builder
      #   Karafka::Workers::Builder.new(SuperConsumer)
      def initialize(consumer_class)
        @consumer_class = consumer_class
      end

      # @return [Class] Sidekiq worker class that already exists or new build based
      #   on the provided consumer_class name
      # @example Consumer: SuperConsumer
      #   build #=> SuperWorker
      # @example Consumer: Videos::NewVideosConsumer
      #   build #=> Videos::NewVideosWorker
      def build
        return matcher.match if matcher.match

        klass = Class.new(Karafka::BaseWorker.base_worker)
        matcher.scope.const_set(matcher.name, klass)
      end

      private

      # @return [Karafka::Helpers::ClassMatcher] matcher instance for matching between consumer
      #   and appropriate worker
      def matcher
        @matcher ||= Helpers::ClassMatcher.new(
          @consumer_class,
          from: 'Consumer',
          to: 'Worker'
        )
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
karafka-sidekiq-backend-1.4.9 lib/karafka/workers/builder.rb
karafka-sidekiq-backend-1.4.7 lib/karafka/workers/builder.rb
karafka-sidekiq-backend-1.4.6 lib/karafka/workers/builder.rb
karafka-sidekiq-backend-1.4.5 lib/karafka/workers/builder.rb
karafka-sidekiq-backend-1.4.4 lib/karafka/workers/builder.rb
karafka-sidekiq-backend-1.4.3 lib/karafka/workers/builder.rb
karafka-sidekiq-backend-1.4.2 lib/karafka/workers/builder.rb
karafka-sidekiq-backend-1.4.1 lib/karafka/workers/builder.rb
karafka-sidekiq-backend-1.4.0 lib/karafka/workers/builder.rb
karafka-sidekiq-backend-1.3.1 lib/karafka/workers/builder.rb
karafka-sidekiq-backend-1.3.0 lib/karafka/workers/builder.rb
karafka-sidekiq-backend-1.3.0.rc1 lib/karafka/workers/builder.rb