Class: Miniswen::Local

Inherits:
Environment show all
Defined in:
lib/miniswen/local.rb

Overview

Local execution environment (current machine)

Constant Summary collapse

TIMEOUT_EXIT_CODE =
124

Instance Method Summary collapse

Instance Method Details

#exec(command, timeout: nil, env: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/miniswen/local.rb', line 12

def exec(command, timeout: nil, env: nil)
  Open3.popen2e(env || {}, command, pgroup: true) do |stdin, io, wait_thr|
    stdin.close
    reader = Thread.new { io.read }

    if timeout&.positive? && wait_thr.join(timeout).nil?
      kill_group(wait_thr.pid)
      wait_thr.join
      output = "#{scrub(reader.value)}\n<command timed out after #{timeout} seconds>"
      return ExecResult.new(exit_code: TIMEOUT_EXIT_CODE, output:)
    end

    ExecResult.new(exit_code: exit_code(wait_thr.value), output: scrub(reader.value))
  end
end