Sha256: 510dbfd5e6f876429e1141d3d4be32e8f778ca18f22dc5fff9508ab38d62d9cb
Contents?: true
Size: 1.2 KB
Versions: 4
Compression:
Stored size: 1.2 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? end end # Runs the `#call` method in a new thread def async_call MUTEX.synchronize do return if @thread&.alive? @thread = Thread.new do Thread.current.abort_on_exception = true call end end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
karafka-2.3.4 | lib/karafka/helpers/async.rb |
karafka-2.3.3 | lib/karafka/helpers/async.rb |
karafka-2.3.2 | lib/karafka/helpers/async.rb |
karafka-2.3.1 | lib/karafka/helpers/async.rb |