Sha256: 2ca0afbab8dc77d9e7d043cd3f99e1b8c76582341ef9f7faccc515242c180d1e

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Karafka
  module Helpers
    # Allows a given class to run async in a separate thread. Provides also few methods we may
    # want to use to control the underlying thread
    #
    # @note Thread running code needs to manage it's own exceptions. If they leak out, they will
    #   abort thread on exception.
    module Async
      # Mutex used to ensure we do not create multiple threads if we decide to run this
      # in parallel on multiple threads
      MUTEX = Mutex.new

      private_constant :MUTEX

      class << self
        # Adds forwardable to redirect thread-based control methods to the underlying thread that
        # runs the async operations
        #
        # @param base [Class] class we're including this module in
        def included(base)
          base.extend ::Forwardable

          base.def_delegators :@thread, :join, :terminate, :alive?, :name
        end
      end

      # Runs the `#call` method in a new thread
      # @param thread_name [String] name that we want to assign to the thread when we start it
      def async_call(thread_name)
        MUTEX.synchronize do
          return if @thread&.alive?

          @thread = Thread.new do
            Thread.current.name = thread_name

            Thread.current.abort_on_exception = true

            call
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
karafka-2.4.3 lib/karafka/helpers/async.rb