Class: Roast::Workflow::ParallelExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/roast/workflow/parallel_executor.rb

Overview

Executes workflow steps in parallel using threads

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executor) ⇒ ParallelExecutor

Returns a new instance of ParallelExecutor.



13
14
15
# File 'lib/roast/workflow/parallel_executor.rb', line 13

def initialize(executor)
  @executor = executor
end

Class Method Details

.execute(steps, executor) ⇒ Object



8
9
10
# File 'lib/roast/workflow/parallel_executor.rb', line 8

def execute(steps, executor)
  new(executor).execute(steps)
end

Instance Method Details

#execute(steps) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/roast/workflow/parallel_executor.rb', line 17

def execute(steps)
  threads = steps.map do |sub_step|
    Thread.new do
      # Each thread needs its own isolated execution context
      Thread.current[:step] = sub_step
      Thread.current[:result] = nil
      Thread.current[:error] = nil

      begin
        # Execute the single step in this thread
        @executor.execute_steps([sub_step])
        Thread.current[:result] = :success
      rescue => e
        Thread.current[:error] = e
      end
    end
  end

  # Wait for all threads to complete
  threads.each(&:join)

  # Check for errors in any thread
  threads.each_with_index do |thread, _index|
    if thread[:error]
      raise thread[:error]
    end
  end

  :success
end