Sha256: b35d9e23f0c3206af5662ad886a8f9833f2bf4fa8cb4fd5023a185dba05bb917

Contents?: true

Size: 1.88 KB

Versions: 63

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module Karafka
  module Processing
    # Namespace for all the jobs that are supposed to run in workers.
    module Jobs
      # Base class for all the jobs types that are suppose to run in workers threads.
      # Each job can have 3 main entry-points: `#before_call`, `#call` and `#after_call`
      # Only `#call` is required.
      class Base
        extend Forwardable

        # @note Since one job has always one executor, we use the jobs id and group id as reference
        def_delegators :executor, :id, :group_id

        attr_reader :executor

        # Creates a new job instance
        def initialize
          # All jobs are blocking by default and they can release the lock when blocking operations
          # are done (if needed)
          @non_blocking = false
        end

        # When redefined can run any code prior to the job being enqueued
        # @note This will run in the listener thread and not in the worker
        def before_enqueue; end

        # When redefined can run any code that should run before executing the proper code
        def before_call; end

        # The main entry-point of a job
        def call
          raise NotImplementedError, 'Please implement in a subclass'
        end

        # When redefined can run any code that should run after executing the proper code
        def after_call; end

        # @return [Boolean] is this a non-blocking job
        #
        # @note Blocking job is a job, that will cause the job queue to wait until it is finished
        #   before removing the lock on new jobs being added
        #
        # @note All the jobs are blocking by default
        #
        # @note Job **needs** to mark itself as non-blocking only **after** it is done with all
        #   the blocking things (pausing partition, etc).
        def non_blocking?
          @non_blocking
        end
      end
    end
  end
end

Version data entries

63 entries across 63 versions & 1 rubygems

Version Path
karafka-2.2.13 lib/karafka/processing/jobs/base.rb
karafka-2.2.12 lib/karafka/processing/jobs/base.rb
karafka-2.2.11 lib/karafka/processing/jobs/base.rb
karafka-2.2.10 lib/karafka/processing/jobs/base.rb
karafka-2.2.9 lib/karafka/processing/jobs/base.rb
karafka-2.2.8 lib/karafka/processing/jobs/base.rb
karafka-2.2.8.beta1 lib/karafka/processing/jobs/base.rb
karafka-2.2.7 lib/karafka/processing/jobs/base.rb
karafka-2.2.6 lib/karafka/processing/jobs/base.rb
karafka-2.2.5 lib/karafka/processing/jobs/base.rb
karafka-2.2.4 lib/karafka/processing/jobs/base.rb
karafka-2.2.3 lib/karafka/processing/jobs/base.rb
karafka-2.2.2 lib/karafka/processing/jobs/base.rb
karafka-2.2.1 lib/karafka/processing/jobs/base.rb
karafka-2.2.0 lib/karafka/processing/jobs/base.rb
karafka-2.1.13 lib/karafka/processing/jobs/base.rb
karafka-2.1.12 lib/karafka/processing/jobs/base.rb
karafka-2.1.11 lib/karafka/processing/jobs/base.rb
karafka-2.1.10 lib/karafka/processing/jobs/base.rb
karafka-2.1.9 lib/karafka/processing/jobs/base.rb