Class: Philiprehberger::TaskRunner::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/task_runner/result.rb

Overview

Represents the result of a shell command execution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, stderr:, exit_code:, duration:, signal: nil) ⇒ Result

Returns a new instance of Result.

Parameters:

  • stdout (String)
  • stderr (String)
  • exit_code (Integer)
  • duration (Float)
  • signal (Symbol, nil) (defaults to: nil)


29
30
31
32
33
34
35
# File 'lib/philiprehberger/task_runner/result.rb', line 29

def initialize(stdout:, stderr:, exit_code:, duration:, signal: nil)
  @stdout = stdout
  @stderr = stderr
  @exit_code = exit_code
  @duration = duration
  @signal = signal
end

Instance Attribute Details

#durationFloat (readonly)

Returns execution duration in seconds.

Returns:

  • (Float)

    execution duration in seconds



19
20
21
# File 'lib/philiprehberger/task_runner/result.rb', line 19

def duration
  @duration
end

#exit_codeInteger (readonly)

Returns process exit code.

Returns:

  • (Integer)

    process exit code



16
17
18
# File 'lib/philiprehberger/task_runner/result.rb', line 16

def exit_code
  @exit_code
end

#signalSymbol? (readonly)

Returns signal that killed the process (:TERM, :KILL, or nil).

Returns:

  • (Symbol, nil)

    signal that killed the process (:TERM, :KILL, or nil)



22
23
24
# File 'lib/philiprehberger/task_runner/result.rb', line 22

def signal
  @signal
end

#stderrString (readonly)

Returns standard error.

Returns:

  • (String)

    standard error



13
14
15
# File 'lib/philiprehberger/task_runner/result.rb', line 13

def stderr
  @stderr
end

#stdoutString (readonly)

Returns standard output.

Returns:

  • (String)

    standard output



10
11
12
# File 'lib/philiprehberger/task_runner/result.rb', line 10

def stdout
  @stdout
end

Instance Method Details

#failure?Boolean

Whether the command exited with a non-zero status, was killed by a signal, or timed out. The logical inverse of #success?.

Returns:

  • (Boolean)


48
49
50
# File 'lib/philiprehberger/task_runner/result.rb', line 48

def failure?
  !success?
end

#json?Boolean

Whether stdout contains a parseable JSON document. Empty stdout is treated as non-JSON.

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
# File 'lib/philiprehberger/task_runner/result.rb', line 64

def json?
  return false if @stdout.nil? || @stdout.strip.empty?

  JSON.parse(@stdout)
  true
rescue JSON::ParserError
  false
end

#parse_jsonObject

Parse stdout as JSON.

Returns:

  • (Object)

    the parsed JSON document (Hash, Array, String, Numeric, true, false, or nil)

Raises:

  • (ParseError)

    if stdout is empty or not valid JSON



77
78
79
80
81
82
83
# File 'lib/philiprehberger/task_runner/result.rb', line 77

def parse_json
  raise ParseError, 'stdout is empty' if @stdout.nil? || @stdout.strip.empty?

  JSON.parse(@stdout)
rescue JSON::ParserError => e
  raise ParseError, "stdout is not valid JSON: #{e.message}"
end

#success?Boolean

Whether the command exited successfully.

Returns:

  • (Boolean)


40
41
42
# File 'lib/philiprehberger/task_runner/result.rb', line 40

def success?
  @exit_code.zero?
end

#timed_out?Boolean

Whether the command was terminated by the task runner because it exceeded its timeout (SIGTERM or SIGKILL).

Returns:

  • (Boolean)


56
57
58
# File 'lib/philiprehberger/task_runner/result.rb', line 56

def timed_out?
  %i[TERM KILL].include?(@signal)
end

#to_hHash

Hash representation of the result.

Returns:

  • (Hash)


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/philiprehberger/task_runner/result.rb', line 88

def to_h
  {
    stdout: @stdout,
    stderr: @stderr,
    exit_code: @exit_code,
    duration: @duration,
    signal: @signal,
    success: success?,
    timed_out: timed_out?
  }
end