class Proclib::Executor
Runs a list of commands simultaenously, providing callbacks on their output lines and exits, as well as optional output logging and caching.
Attributes
cache_output[R]
cache_output?[R]
callbacks[R]
commands[R]
log_to_console[R]
log_to_console?[R]
Public Class Methods
new(commands, log_to_console: false, cache_output: false)
click to toggle source
# 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
Public Instance Methods
exit_states()
click to toggle source
# File lib/proclib/executor.rb, line 47 def exit_states @exit_states ||= Array.new end
on_exit(&blk)
click to toggle source
# File lib/proclib/executor.rb, line 39 def on_exit(&blk) callbacks.exit << blk end
on_output(&blk)
click to toggle source
# File lib/proclib/executor.rb, line 43 def on_output(&blk) callbacks.output << blk end
run_sync()
click to toggle source
# File lib/proclib/executor.rb, line 21 def run_sync start wait end
start()
click to toggle source
# File lib/proclib/executor.rb, line 26 def start processes.each(&:spawn) end
wait()
click to toggle source
# File lib/proclib/executor.rb, line 30 def wait channel.each do |message| handle_exit(message) if message.type == :exit handle_output(message) if message.type == :output end result end
Private Instance Methods
aggregate_exit_code()
click to toggle source
# File lib/proclib/executor.rb, line 60 def aggregate_exit_code if exit_states.all? {|c| c == 0} 0 elsif exit_states.size == 1 exit_states.first else 1 end end
channel()
click to toggle source
# File lib/proclib/executor.rb, line 91 def channel @channel ||= Channel.new(:output, :exit) end
console_logger()
click to toggle source
# File lib/proclib/executor.rb, line 87 def console_logger @console_logger ||= Loggers::Console.new end
handle_exit(message)
click to toggle source
# File lib/proclib/executor.rb, line 70 def handle_exit(message) exit_states << message.data channel.close if exit_states.size == processes.size callbacks.exit.each {|c| c[message.data] } end
handle_output(message)
click to toggle source
# File lib/proclib/executor.rb, line 76 def handle_output(message) callbacks.output.each {|c| c[message.data]} console_logger << message.data if log_to_console? output_cache << message.data if cache_output? end
output_cache()
click to toggle source
# File lib/proclib/executor.rb, line 83 def output_cache @output_cache ||= OutputCache.new end
processes()
click to toggle source
# File lib/proclib/executor.rb, line 95 def processes commands.map {|c| Process.new(c, channel: channel) } end
result()
click to toggle source
# File lib/proclib/executor.rb, line 53 def result Result.new( exit_code: aggregate_exit_code, output_cache: (output_cache if cache_output?) ) end