Class: Proclib::Executor
- Inherits:
-
Object
- Object
- Proclib::Executor
- Defined in:
- lib/proclib/executor.rb
Overview
Runs a list of commands simultaenously, providing callbacks on their output lines and exits, as well as optional output logging and caching.
Instance Attribute Summary collapse
-
#cache_output ⇒ Object
(also: #cache_output?)
readonly
Returns the value of attribute cache_output.
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#log_to_console ⇒ Object
(also: #log_to_console?)
readonly
Returns the value of attribute log_to_console.
Instance Method Summary collapse
- #exit_states ⇒ Object
-
#initialize(commands, log_to_console: false, cache_output: false) ⇒ Executor
constructor
A new instance of Executor.
- #on_exit(&blk) ⇒ Object
- #on_output(&blk) ⇒ Object
- #run_sync ⇒ Object
- #start ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(commands, log_to_console: false, cache_output: false) ⇒ Executor
Returns a new instance of Executor.
15 16 17 18 19 |
# File 'lib/proclib/executor.rb', line 15 def initialize(commands, log_to_console: false, cache_output: false) @commands, @log_to_console, @cache_output = commands, log_to_console, cache_output @callbacks = Struct.new(:exit, :output).new([], []) end |
Instance Attribute Details
#cache_output ⇒ Object (readonly) Also known as: cache_output?
Returns the value of attribute cache_output.
11 12 13 |
# File 'lib/proclib/executor.rb', line 11 def cache_output @cache_output end |
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
11 12 13 |
# File 'lib/proclib/executor.rb', line 11 def callbacks @callbacks end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
11 12 13 |
# File 'lib/proclib/executor.rb', line 11 def commands @commands end |
#log_to_console ⇒ Object (readonly) Also known as: log_to_console?
Returns the value of attribute log_to_console.
11 12 13 |
# File 'lib/proclib/executor.rb', line 11 def log_to_console @log_to_console end |
Instance Method Details
#exit_states ⇒ Object
47 48 49 |
# File 'lib/proclib/executor.rb', line 47 def exit_states @exit_states ||= Array.new end |
#on_exit(&blk) ⇒ Object
39 40 41 |
# File 'lib/proclib/executor.rb', line 39 def on_exit(&blk) callbacks.exit << blk end |
#on_output(&blk) ⇒ Object
43 44 45 |
# File 'lib/proclib/executor.rb', line 43 def on_output(&blk) callbacks.output << blk end |
#run_sync ⇒ Object
21 22 23 24 |
# File 'lib/proclib/executor.rb', line 21 def run_sync start wait end |
#start ⇒ Object
26 27 28 |
# File 'lib/proclib/executor.rb', line 26 def start processes.each(&:spawn) end |
#wait ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/proclib/executor.rb', line 30 def wait channel.each do || handle_exit() if .type == :exit handle_output() if .type == :output end result end |