Class: CLIChef::Job

Inherits:
Object
  • Object
show all
Includes:
BBLib::Effortless
Defined in:
lib/cli_chef/components/job.rb

Direct Known Subclasses

HandBrakeJob

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.typeObject



16
17
18
# File 'lib/cli_chef/components/job.rb', line 16

def self.type
  self.class.to_s.split('::').last.method_case.to_sym
end

Instance Method Details

#code_for(code) ⇒ Object



45
46
47
48
49
# 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?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/cli_chef/components/job.rb', line 59

def done?
  !running?
end

#durationObject



63
64
65
# File 'lib/cli_chef/components/job.rb', line 63

def duration
  timer.current || timer.last
end

#error?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/cli_chef/components/job.rb', line 72

def error?
  result && result.exit_code.error?
end

#estimate_etaObject



84
85
86
87
# 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

#etaObject



80
81
82
# File 'lib/cli_chef/components/job.rb', line 80

def eta
  @eta || estimate_eta
end

#exit_codesObject



51
52
53
# File 'lib/cli_chef/components/job.rb', line 51

def exit_codes
  parent ? parent.exit_codes : []
end

#killObject



67
68
69
70
# File 'lib/cli_chef/components/job.rb', line 67

def kill
  return true unless thread
  thread.kill
end

#run(&block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/cli_chef/components/job.rb', line 55

def running?
  thread && thread.alive?
end

#success?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/cli_chef/components/job.rb', line 76

def success?
  !error?
end