Sha256: 229ce2c77a1e8668abeff9f6b489da7280a3648682066c394712506b93a283ff

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

require 'json'
require 'open3'
require 'stringio'
require 'logger'
require 'audit'


def time_diff_milli(start, finish)
   (finish - start) * 1000.0
end

def handleIO(stillOpen, ioArray, io, log)
  if ioArray.include?(io)
    begin
      log.write(io.readpartial(4096))
    rescue EOFError
      stillOpen.delete_if{|s| s == io}
    end
  end
end

class RunnerWorker

    def run(command, start, lines, statusIndicator = false )
        audit = Audit.new

	    log = Logger.new(Canzea::config[:logging_root] + '/plans.log')

        log.info("HANDLING: " + command)

        l = command

        audit.start( "#{lines + 1 }", l.chomp)

        log.info("#{lines + 1} FOUND: " + l)

        t1 = Time.now

        Open3.popen3(ENV, l) {|stdin, stdout, stderr, wait_thr|
          pid = wait_thr.pid # pid of the started process.

          log_w = StringIO.new

          stillOpen = [stdout, stderr]
          while !stillOpen.empty?
            fhs = select(stillOpen, nil, nil, nil)
            handleIO(stillOpen, fhs[0], stdout, log_w)
            handleIO(stillOpen, fhs[0], stderr, log_w)
          end

          exit_status = wait_thr.value # wait for it to finish

          log_w.close_write

          output = log_w.string

          output.split(/\n/).each do | line |
              puts "-- #{line}"
          end
          puts "-- Exit Status = #{exit_status}"

          log.info("STDOUT #{output}")

          log.info("Exit Status = #{exit_status}")

          # if exit status is failure then exit right away

          t2 = Time.now

          msecs = time_diff_milli t1, t2

          audit.complete("#{lines + 1}", l.chomp, exit_status.exitstatus, msecs, output)

          if (statusIndicator)
              audit.status("#{lines + 1}", l.chomp, exit_status.exitstatus, msecs, output)
          end

          if exit_status.exitstatus != 0
            abort()
          end
        }

        lines += 1

        return lines

    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
canzea-0.1.4 lib/trace-runner.rb
canzea-0.1.3 lib/trace-runner.rb
canzea-0.1.2 lib/trace-runner.rb