Class: Smith::Workflow::Parallel

Inherits:
Object
  • Object
show all
Defined in:
lib/smith/workflow/parallel.rb,
lib/smith/workflow/parallel/cancellation.rb,
lib/smith/workflow/parallel/cancellation_signal.rb

Defined Under Namespace

Classes: Cancellation, CancellationSignal

Class Method Summary collapse

Class Method Details

.execute(branches:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/smith/workflow/parallel.rb', line 18

def self.execute(branches:)
  signal = CancellationSignal.new

  futures = branches.map do |branch|
    Concurrent::Promises.future(branch, signal) do |b, s|
      b.call(s)
    rescue StandardError
      s.cancel!
      raise
    end
  end

  fulfilled, values, reasons = Concurrent::Promises.zip(*futures).result

  unless fulfilled
    error = preferred_error(reasons)
    raise error
  end

  values
end

.preferred_error(reasons) ⇒ Object



40
41
42
43
# File 'lib/smith/workflow/parallel.rb', line 40

def self.preferred_error(reasons)
  errors = reasons.compact
  errors.find { |error| !error.is_a?(Cancellation) } || errors.first
end

.resolve_branch_count(transition, context) ⇒ Object

Raises:



10
11
12
13
14
15
16
# File 'lib/smith/workflow/parallel.rb', line 10

def self.resolve_branch_count(transition, context)
  count = transition.agent_opts[:count]
  resolved = count.respond_to?(:call) ? count.call(context) : (count || 1)
  return resolved if resolved.is_a?(Integer) && resolved.positive?

  raise WorkflowError, "parallel branch count must be a positive integer"
end