Class: ProcessExecuter::Result

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/process_executer/result.rb

Overview

A decorator for Process::Status that adds the following attributes:

  • command: the command that was used to spawn the process
  • options: the options that were used to spawn the process
  • elapsed_time: the seconds the command ran
  • timed_out?: true if the process timed out

API:

  • public

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, command:, options:, timed_out:, elapsed_time:) ⇒ Result

Create a new Result object

Examples:

command = ['sleep 1']
options = ProcessExecuter::Options::SpawnOptions.new
pid = Process.spawn(*command, **options.spawn_options)
_pid, status = Process.wait2(pid)
timed_out = false
elapsed_time = 0.01

ProcessExecuter::Result.new(status, command:, options:, timed_out:, elapsed_time:)

Parameters:

  • the status to delegate to

  • the command that was used to spawn the process

  • the options that were used to spawn the process

  • true if the process timed out

  • the seconds the command ran

API:

  • public



38
39
40
41
42
43
44
# File 'lib/process_executer/result.rb', line 38

def initialize(status, command:, options:, timed_out:, elapsed_time:)
  super(status)
  @command = command
  @options = options
  @timed_out = timed_out
  @elapsed_time = elapsed_time
end

Instance Attribute Details

#commandArray (readonly)

The command that was used to spawn the process

Examples:

result.command #=> [{ 'GIT_DIR' => '/path/to/repo' }, 'git', 'status']

See Also:

  • Process.spawn

Returns:

API:

  • public



51
52
53
# File 'lib/process_executer/result.rb', line 51

def command
  @command
end

#elapsed_timeNumeric (readonly)

The seconds the command ran

Examples:

result.elapsed_time #=> 10.0

Returns:

API:

  • public



70
71
72
# File 'lib/process_executer/result.rb', line 70

def elapsed_time
  @elapsed_time
end

#optionsProcessExecuter::Options::Base (readonly)

The options that were used to spawn the process

Examples:

# Looks like a hash, but is actually an object that derives from
# ProcessExecuter::Options::Base
result.options #=> { chdir: '/path/to/repo', timeout_after: 0.5 }

See Also:

  • Process.spawn

Returns:

API:

  • public



64
65
66
# File 'lib/process_executer/result.rb', line 64

def options
  @options
end

#timed_outBoolean (readonly) Also known as: timed_out?

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

Examples:

result = ProcessExecuter.spawn_with_timeout('sleep 10', timeout_after: 0.01)
result.timed_out? # => true

Returns:

API:

  • public



79
80
81
# File 'lib/process_executer/result.rb', line 79

def timed_out
  @timed_out
end

Instance Method Details

#success?true, ...

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:

result = ProcessExecuter.spawn_with_timeout('sleep 10', timeout_after: 0.01)
result.success? # => nil

Returns:

API:

  • public



91
92
93
94
95
# File 'lib/process_executer/result.rb', line 91

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

  super
end

#to_sString

Return a string representation of the result

Examples:

result = ProcessExecuter.spawn_with_timeout('sleep 10', timeout_after: 1)
# This message is platform dependent, but will look like this on Linux:
result.to_s #=> "pid 70144 SIGKILL (signal 9) timed out after 1s"

Returns:

API:

  • public



104
105
106
# File 'lib/process_executer/result.rb', line 104

def to_s
  "#{super}#{" timed out after #{options.timeout_after}s" if timed_out?}"
end