Module: CMDx::Executors::Fiber Private

Extended by:
Fiber
Included in:
Fiber
Defined in:
lib/cmdx/executors/fiber.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Fiber-scheduler backed executor. Spawns one fiber per job, bounded by ‘concurrency` via a `SizedQueue` semaphore. Requires a Fiber scheduler to be installed on the current thread (e.g. inside `Async { … }` from the `async` gem). `pool_size` caps in-flight fibers. Exceptions raised inside `on_job` are captured and re-raised once every fiber has completed.

Instance Method Summary collapse

Instance Method Details

#call(jobs:, concurrency:, on_job:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • jobs (Array)
  • concurrency (Integer)

    max in-flight fibers (must be >= 1)

  • on_job (#call)

Raises:

  • (ArgumentError)

    when ‘concurrency` is not a positive Integer

  • (RuntimeError)

    when no ‘Fiber.scheduler` is installed

  • (StandardError)

    re-raises the first exception captured from any fiber



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cmdx/executors/fiber.rb', line 23

def call(jobs:, concurrency:, on_job:)
  raise ArgumentError, "executor concurrency must be a positive Integer (got #{concurrency.inspect})" unless concurrency.is_a?(Integer) && concurrency.positive?

  raise "executor: :fibers requires Fiber.scheduler; run the workflow inside a scheduler block (e.g. Async { ... })" unless ::Fiber.scheduler

  slots = SizedQueue.new(concurrency)
  concurrency.times { slots << :slot }
  done   = Queue.new
  errors = Queue.new

  jobs.each do |job|
    slots.pop
    ::Fiber.schedule do
      begin
        on_job.call(job)
      rescue StandardError => e
        errors << e
      end
    ensure
      slots << :slot
      done << true
    end
  end

  jobs.size.times { done.pop }

  raise errors.pop unless errors.empty?
end