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.

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

  • on_job (#call)

Raises:

  • (RuntimeError)

    when no ‘Fiber.scheduler` is installed



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cmdx/executors/fiber.rb', line 20

def call(jobs:, concurrency:, on_job:)
  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

  jobs.each do |job|
    slots.pop
    ::Fiber.schedule do
      on_job.call(job)
    ensure
      slots << :slot
      done << true
    end
  end

  jobs.size.times { done.pop }
end