class CLIChef::Job

Public Class Methods

type() click to toggle source
# File lib/cli_chef/components/job.rb, line 16
def self.type
  self.class.to_s.split('::').last.method_case.to_sym
end

Public Instance Methods

code_for(code) click to toggle source
# File lib/cli_chef/components/job.rb, line 45
def code_for(code)
  exit_codes.find do |ec|
    ec.code == code
  end || ExitCode.new(code)
end
done?() click to toggle source
# File lib/cli_chef/components/job.rb, line 59
def done?
  !running?
end
duration() click to toggle source
# File lib/cli_chef/components/job.rb, line 63
def duration
  timer.current || timer.last
end
error?() click to toggle source
# File lib/cli_chef/components/job.rb, line 72
def error?
  result && result.exit_code.error?
end
estimate_eta() click to toggle source
# File lib/cli_chef/components/job.rb, line 84
def estimate_eta
  return 0 unless percent && timer.current && percent.positive?
  (100 - percent) / timer.current
end
eta() click to toggle source
# File lib/cli_chef/components/job.rb, line 80
def eta
  @eta || estimate_eta
end
exit_codes() click to toggle source
# File lib/cli_chef/components/job.rb, line 51
def exit_codes
  parent ? parent.exit_codes : []
end
kill() click to toggle source
# File lib/cli_chef/components/job.rb, line 67
def kill
  return true unless thread
  thread.kill
end
run() { |line, name, self| ... } click to toggle source
# File lib/cli_chef/components/job.rb, line 23
def run(&block)
  timer.start
  self.percent = 0.0
  self.thread = Thread.new do
    self.result = Result.new(body: '')
    # TODO Have command killed when parent process dies
    Open3.popen3(command) do |sin, out, err, pr|
      self.result.pid = pr.pid
      { stdout: out, stderr: err }.each do |name, stream|
        stream.each do |line|
          block ? yield(line, name, self) : process_line(line, name)
        end
      end
      self.result.exit_code = code_for(pr.value.exitstatus)
    end
    self.percent = 100.0
    self.timer.stop
    self.result
  end
  running?
end
running?() click to toggle source
# File lib/cli_chef/components/job.rb, line 55
def running?
  thread && thread.alive?
end
success?() click to toggle source
# File lib/cli_chef/components/job.rb, line 76
def success?
  !error?
end

Protected Instance Methods

process_line(line, stream = :stdout) click to toggle source
# File lib/cli_chef/components/job.rb, line 91
def process_line(line, stream = :stdout)
  self.result.body = self.result.body + line
  case stream
  when :stderr
    STDERR.puts line unless line.to_s.empty?
  else
    # Nothing happens with stdout in the default job class
    # puts line
  end
end