lib/org-converge/engine.rb in org-converge-0.0.3 vs lib/org-converge/engine.rb in org-converge-0.0.4

- old
+ new

@@ -2,17 +2,20 @@ # Re-use most of the proven tricks from Foreman for this # and just customize to watch the output from runnable code blocks # require 'foreman/engine' require 'foreman/process' +require 'tco' module OrgConverge class Engine < Foreman::Engine attr_reader :logger attr_reader :babel + RAINBOW = ["#622e90", "#2d3091", "#00aaea", "#02a552", "#fdea22", "#eb443b", "#f37f5a"] + def initialize(options={}) super(options) @logger = options[:logger] || Logger.new(STDOUT) @babel = options[:babel] end @@ -33,11 +36,11 @@ def spawn_processes @processes.each do |process| reader, writer = create_pipe begin pid = process.run(:output => writer) - @names[process] = "#{@names[process]}(#{pid})" + @names[process] = "#{@names[process]}.#{pid}" writer.puts "started with pid #{pid}" rescue Errno::ENOENT writer.puts "unknown command: #{process.command}" end @running[pid] = [process] @@ -53,14 +56,13 @@ @processes << process end def output(name, data) data.to_s.lines.map(&:chomp).each do |message| - # FIXME: In case the process has finished before its lines where flushed - ps = name.empty? ? '<defunct>' : name.split('.').first - output = "#{pad_process_name(ps)}".yellow + ps, pid = name.empty? ? '<defunct>' : name.split('.') + output = "#{pad_process_name(ps)}(#{pid})".fg get_color_for_pid(pid.to_i) output += " -- " output += message # FIXME: When the process has stopped already, # the name of the process does not appear logger.info output @@ -77,9 +79,13 @@ end end def pad_process_name(name) name.ljust(name_padding, " ") + end + + def get_color_for_pid(pid) + RAINBOW[pid % 7] end end class CodeBlockProcess < Foreman::Process; end end