Class: Proclib::Executor

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_outputObject (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

#callbacksObject (readonly)

Returns the value of attribute callbacks.



11
12
13
# File 'lib/proclib/executor.rb', line 11

def callbacks
  @callbacks
end

#commandsObject (readonly)

Returns the value of attribute commands.



11
12
13
# File 'lib/proclib/executor.rb', line 11

def commands
  @commands
end

#log_to_consoleObject (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_statesObject



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_syncObject



21
22
23
24
# File 'lib/proclib/executor.rb', line 21

def run_sync
  start
  wait
end

#startObject



26
27
28
# File 'lib/proclib/executor.rb', line 26

def start
  processes.each(&:spawn)
end

#waitObject



30
31
32
33
34
35
36
37
# 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