Class: Philiprehberger::TaskRunner::Result
- Inherits:
-
Object
- Object
- Philiprehberger::TaskRunner::Result
- Defined in:
- lib/philiprehberger/task_runner/result.rb
Overview
Represents the result of a shell command execution.
Instance Attribute Summary collapse
-
#duration ⇒ Float
readonly
Execution duration in seconds.
-
#exit_code ⇒ Integer
readonly
Process exit code.
-
#signal ⇒ Symbol?
readonly
Signal that killed the process (:TERM, :KILL, or nil).
-
#stderr ⇒ String
readonly
Standard error.
-
#stdout ⇒ String
readonly
Standard output.
Instance Method Summary collapse
-
#failure? ⇒ Boolean
Whether the command exited with a non-zero status, was killed by a signal, or timed out.
-
#initialize(stdout:, stderr:, exit_code:, duration:, signal: nil) ⇒ Result
constructor
A new instance of Result.
-
#json? ⇒ Boolean
Whether stdout contains a parseable JSON document.
-
#parse_json ⇒ Object
Parse stdout as JSON.
-
#success? ⇒ Boolean
Whether the command exited successfully.
-
#timed_out? ⇒ Boolean
Whether the command was terminated by the task runner because it exceeded its timeout (SIGTERM or SIGKILL).
-
#to_h ⇒ Hash
Hash representation of the result.
Constructor Details
#initialize(stdout:, stderr:, exit_code:, duration:, signal: nil) ⇒ Result
Returns a new instance of Result.
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
#duration ⇒ Float (readonly)
Returns execution duration in seconds.
19 20 21 |
# File 'lib/philiprehberger/task_runner/result.rb', line 19 def duration @duration end |
#exit_code ⇒ Integer (readonly)
Returns process exit code.
16 17 18 |
# File 'lib/philiprehberger/task_runner/result.rb', line 16 def exit_code @exit_code end |
#signal ⇒ Symbol? (readonly)
Returns 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 |
#stderr ⇒ String (readonly)
Returns standard error.
13 14 15 |
# File 'lib/philiprehberger/task_runner/result.rb', line 13 def stderr @stderr end |
#stdout ⇒ String (readonly)
Returns 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?.
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.
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_json ⇒ Object
Parse stdout as 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.}" end |
#success? ⇒ Boolean
Whether the command exited successfully.
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).
56 57 58 |
# File 'lib/philiprehberger/task_runner/result.rb', line 56 def timed_out? %i[TERM KILL].include?(@signal) end |
#to_h ⇒ Hash
Hash representation of the result.
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 |