Class: PDK::CLI::ParallelExecGroup

Inherits:
ExecGroup
  • Object
show all
Defined in:
lib/pdk/cli/exec_group.rb

Overview

Executes registered blocks in parallel using Ruby threads

See Also:

Instance Method Summary collapse

Methods inherited from ExecGroup

create

Constructor Details

#initialize(message, opts = {}) ⇒ ParallelExecGroup

Returns a new instance of ParallelExecGroup.



77
78
79
80
81
# File 'lib/pdk/cli/exec_group.rb', line 77

def initialize(message, opts = {})
  super(message, opts)
  @threads = []
  @exit_codes = []
end

Instance Method Details

#exit_codeObject



95
96
97
98
99
100
# File 'lib/pdk/cli/exec_group.rb', line 95

def exit_code
  @threads.each(&:join)
  return 0 if @exit_codes.empty?

  @exit_codes.max
end

#register(&block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pdk/cli/exec_group.rb', line 83

def register(&block)
  super(&block)

  # TODO: This executes the thread immediately, whereas the SerialExecGroup executes only when exit_code
  # is called.  Need to change this so it uses a kind of ThreadPool to limit to number on concurrent jobs
  # and only starts on the call to exit_code
  # e.g. max_threads = No. of CPUs
  @threads << Thread.new do
    @exit_codes << yield
  end
end