Class: SProc::SProc::TaskRunner

Inherits:
Object
  • Object
show all
Includes:
SProc::ShellType
Defined in:
lib/sproc/core.rb

Overview

Helper class that runs one task using the preconditions given at instantiation. This class is not intended for external use

Constant Summary collapse

SUPPORTED_SPAWN_OPTS =

Restrict the options to Process.spawn that we support to these

i[chdir umask unsetenv_others]
DEFAULT_OPTS =
{
  type: NONE,
  stdout_callback: nil,
  stderr_callback: nil
}.freeze

Constants included from SProc::ShellType

SProc::ShellType::SHELL_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ TaskRunner

Returns a new instance of TaskRunner.



263
264
265
266
# File 'lib/sproc/core.rb', line 263

def initialize(opts)
  @task_info = TaskInfo.new('', nil, 0, nil, nil, String.new, String.new)
  @opts = DEFAULT_OPTS.dup.merge!(opts)
end

Instance Attribute Details

#task_infoObject (readonly)

Returns the value of attribute task_info.



250
251
252
# File 'lib/sproc/core.rb', line 250

def task_info
  @task_info
end

Instance Method Details

#execute(env, cmd, *args, **opts) ⇒ Object

Runs the process and blocks until it is completed or aborted. The stdout and stdin streams are continuously read in parallel with the process execution.



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/sproc/core.rb', line 271

def execute(env, cmd, *args, **opts)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  begin
    shell_out_via_popen(env, cmd, *args, **opts)
  rescue StandardError => e
    @task_info[:exception] = e
  end
  @task_info[:wall_time] = (Process.clock_gettime(
    Process::CLOCK_MONOTONIC
  ) - start_time)
end