Class: ProcessExecuter::Status

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/process_executer/status.rb

Overview

A simple delegator for Process::Status that adds a timeout? attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, timeout, timeout_duration) ⇒ Status

Create a new Status object from a Process::Status and timeout flag

Examples:

status = Process.wait2(pid).last
timeout = false
ProcessExecuter::Status.new(status, timeout)

Parameters:

  • status (Process::Status)

    the status to delegate to

  • timeout (Boolean)

    true if the process timed out

  • timeout_duration (Numeric, nil)

    The secs the command ran before being killed OR o or nil for no timeout



27
28
29
30
31
# File 'lib/process_executer/status.rb', line 27

def initialize(status, timeout, timeout_duration)
  super(status)
  @timeout = timeout
  @timeout_duration = timeout_duration
end

Instance Attribute Details

#timeout?Boolean (readonly)

True if the process timed out and was sent the SIGKILL signal

Examples:

status = ProcessExecuter.spawn('sleep 10', timeout: 0.01)
status.timeout? # => true

Returns:

  • (Boolean)


46
# File 'lib/process_executer/status.rb', line 46

def timeout? = @timeout

#timeout_durationNumeric? (readonly)

The secs the command ran before being killed OR o or nil for no timeout

Examples:

status.timeout_duration #=> 10

Returns:

  • (Numeric, nil)


37
38
39
# File 'lib/process_executer/status.rb', line 37

def timeout_duration
  @timeout_duration
end

Instance Method Details

#success?Boolean?

Overrides the default success? method to return nil if the process timed out

This is because when a timeout occurs, Windows will still return true

Examples:

status = ProcessExecuter.spawn('sleep 10', timeout: 0.01)
status.success? # => nil

Returns:

  • (Boolean, nil)


56
57
58
59
60
# File 'lib/process_executer/status.rb', line 56

def success?
  return nil if timeout? # rubocop:disable Style/ReturnNilInPredicateMethodDefinition

  super
end

#to_sString

Return a string representation of the status

Examples:

status.to_s #=> "pid 70144 SIGKILL (signal 9) timed out after 10s"

Returns:

  • (String)


66
67
68
# File 'lib/process_executer/status.rb', line 66

def to_s
  "#{super}#{timeout? ? " timed out after #{timeout_duration}s" : ''}"
end