Class: ProcessExecuter::Options

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

Overview

Validate ProcessExecuter::Executer#spawn options and return Process.spawn options

Valid options are those accepted by Process.spawn plus the following additions:

  • :timeout:

Constant Summary collapse

SPAWN_OPTIONS =

These options should be passed to Process.spawn

Additionally, any options whose key is an Integer or an IO object will be passed to Process.spawn.

%i[
  in out err unsetenv_others pgroup new_pgroup rlimit_resourcename umask
  close_others chdir
].freeze
NON_SPAWN_OPTIONS =

These options are allowed by ProcessExecuter.spawn but should NOT be passed to Process.spawn

%i[
  timeout
].freeze
NOT_SET =

Any SPAWN_OPTIONS set to NOT_SET will not be passed to Process.spawn

:not_set
DEFAULTS =

The default values for all options

Returns:

  • (Hash)
{
  in: NOT_SET,
  out: NOT_SET,
  err: NOT_SET,
  unsetenv_others: NOT_SET,
  pgroup: NOT_SET,
  new_pgroup: NOT_SET,
  rlimit_resourcename: NOT_SET,
  umask: NOT_SET,
  close_others: NOT_SET,
  chdir: NOT_SET,
  timeout: nil
}.freeze
ALL_OPTIONS =

All options allowed by this class

(SPAWN_OPTIONS + NON_SPAWN_OPTIONS).freeze

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Options

Create a new Options object

Examples:

options = ProcessExecuter::Options.new(out: $stdout, err: $stderr, timeout: 10)

Parameters:

  • options (Hash)

    Process.spawn options plus additional options listed below.

    See Process.spawn for a list of valid options that can be passed to Process.spawn.

Options Hash (**options):

  • :timeout (Integer, Float, nil)

    Number of seconds to wait for the process to terminate. Any number may be used, including Floats to specify fractional seconds. A value of 0 or nil will allow the process to run indefinitely.



86
87
88
89
90
# File 'lib/process_executer/options.rb', line 86

def initialize(**options)
  assert_no_unknown_options(options)
  @options = DEFAULTS.merge(options)
  assert_timeout_is_valid
end

Instance Method Details

#spawn_optionsHash

Returns the options to be passed to Process.spawn

Examples:

options = ProcessExecuter::Options.new(out: $stdout, err: $stderr, timeout: 10)
options.spawn_options # => { out: $stdout, err: $stderr }

Returns:

  • (Hash)


100
101
102
103
104
105
106
# File 'lib/process_executer/options.rb', line 100

def spawn_options
  {}.tap do |spawn_options|
    options.each do |option, value|
      spawn_options[option] = value if include_spawn_option?(option, value)
    end
  end
end