Class: ProcessExecuter::Commands::SpawnWithTimeout Private

Inherits:
Object
  • Object
show all
Defined in:
lib/process_executer/commands/spawn_with_timeout.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Spawns a subprocess, waits until it completes, and returns the result

Wraps Process.spawn to provide the core functionality for ProcessExecuter.spawn_with_timeout.

It accepts all Process.spawn execution options plus the additional option timeout_after.

Direct Known Subclasses

Run

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, options) ⇒ SpawnWithTimeout

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new SpawnWithTimeout instance

Examples:

options = ProcessExecuter::Options::SpawnWithTimeoutOptions.new(timeout_after: 5)
result = ProcessExecuter::Commands::SpawnWithTimeout.new('echo hello', options).call
result.success? # => true
result.exitstatus # => 0

Parameters:



30
31
32
33
# File 'lib/process_executer/commands/spawn_with_timeout.rb', line 30

def initialize(command, options)
  @command = command
  @options = options
end

Instance Attribute Details

#commandArray<String> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The command to be run in the subprocess

Examples:

spawn.command #=> ['echo', 'hello']

Returns:

  • (Array<String>)

See Also:

  • Process.spawn


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

def command
  @command
end

#elapsed_timeNumeric (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The elapsed time in seconds that the command ran

Examples:

spawn.elapsed_time #=> 1.234

Returns:

  • (Numeric)


108
109
110
# File 'lib/process_executer/commands/spawn_with_timeout.rb', line 108

def elapsed_time
  @elapsed_time
end

#optionsProcessExecuter::Options::SpawnWithTimeoutOptions (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The options that were used to spawn the process

Examples:

spawn.options #=> ProcessExecuter::Options::SpawnWithTimeoutOptions

Returns:



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

def options
  @options
end

#pidInteger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The process ID of the spawned subprocess

Examples:

spawn.pid #=> 12345

Returns:

  • (Integer)


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

def pid
  @pid
end

#resultProcessExecuter::Result (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The result of the completed subprocess

Examples:

spawn.result #=> ProcessExecuter::Result

Returns:



117
118
119
# File 'lib/process_executer/commands/spawn_with_timeout.rb', line 117

def result
  @result
end

#statusProcess::Status (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The status returned by Process.wait2

Examples:

spawn.status #=> #<Process::Status: pid 12345 exit 0>

Returns:

  • (Process::Status)


88
89
90
# File 'lib/process_executer/commands/spawn_with_timeout.rb', line 88

def status
  @status
end

#timed_outBoolean (readonly) Also known as: timed_out?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether the process timed out

Examples:

spawn.timed_out? #=> true

Returns:

  • (Boolean)


97
98
99
# File 'lib/process_executer/commands/spawn_with_timeout.rb', line 97

def timed_out
  @timed_out
end

Instance Method Details

#callProcessExecuter::Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run a command and return the result

Examples:

options = ProcessExecuter::Options::SpawnWithTimeoutOptions.new(timeout_after: 5)
result = ProcessExecuter::Commands::SpawnWithTimeout.new('echo hello', options).call
result.success? # => true
result.exitstatus # => 0
result.timed_out? # => false

Returns:

Raises:



49
50
51
52
53
54
55
56
57
# File 'lib/process_executer/commands/spawn_with_timeout.rb', line 49

def call
  begin
    @pid = Process.spawn(*command, **options.spawn_options)
  rescue StandardError => e
    raise ProcessExecuter::SpawnError, "Failed to spawn process: #{e.message}"
  end

  wait_for_process
end