Skip to content
Kward Search API index

Class: Kward::LocalCommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/local_command_runner.rb

Overview

Low-level local process runner with bounded capture, timeout, cancellation, and optional output streaming. Callers own command semantics and formatting.

Defined Under Namespace

Classes: Result

Constant Summary collapse

READ_SIZE =
4096

Instance Method Summary collapse

Constructor Details

#initialize(timeout_seconds:, max_output_bytes:, terminate_on_output_limit: false) ⇒ LocalCommandRunner

Returns a new instance of LocalCommandRunner.



15
16
17
18
19
# File 'lib/kward/local_command_runner.rb', line 15

def initialize(timeout_seconds:, max_output_bytes:, terminate_on_output_limit: false)
  @timeout_seconds = timeout_seconds.to_i.positive? ? timeout_seconds.to_i : 30
  @max_output_bytes = max_output_bytes.to_i.positive? ? max_output_bytes.to_i : 128 * 1024
  @terminate_on_output_limit = terminate_on_output_limit
end

Instance Method Details

#run(*command, env: {}, cwd: Dir.pwd, cancellation: nil, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kward/local_command_runner.rb', line 21

def run(*command, env: {}, cwd: Dir.pwd, cancellation: nil, &block)
  cancellation&.raise_if_cancelled!
  stdout_buffer = +""
  stderr_buffer = +""
  captured_bytes = 0
  truncated = false
  timed_out = false
  queue = Queue.new

  Open3.popen3(env.to_h, *command, chdir: cwd.to_s, pgroup: true) do |stdin, stdout, stderr, wait_thread|
    stdin.close
    readers = [
      read_stream(stdout, :stdout, queue),
      read_stream(stderr, :stderr, queue)
    ]
    cancellation&.on_cancel { terminate_process_group(wait_thread.pid) }

    status = wait_for_process(wait_thread, readers, queue, cancellation: cancellation) do |stream, chunk|
      captured_bytes, truncated, captured_chunk = capture_chunk(
        stream,
        chunk,
        stdout_buffer,
        stderr_buffer,
        captured_bytes,
        truncated
      )
      block&.call(stream, captured_chunk) unless captured_chunk.empty?
      terminate_process_group(wait_thread.pid) if truncated && @terminate_on_output_limit
    end

    join_readers(readers)
    drain_queue(queue) do |stream, chunk|
      captured_bytes, truncated, captured_chunk = capture_chunk(
        stream,
        chunk,
        stdout_buffer,
        stderr_buffer,
        captured_bytes,
        truncated
      )
      block&.call(stream, captured_chunk) unless captured_chunk.empty?
    end

    Result.new(stdout: stdout_buffer, stderr: stderr_buffer, exit_status: status.exitstatus || 1, timed_out: false, truncated: truncated)
  rescue Timeout::Error
    timed_out = true
    terminate_process_group(wait_thread.pid)
    join_readers(readers)
    Result.new(stdout: stdout_buffer, stderr: stderr_buffer, exit_status: nil, timed_out: timed_out, truncated: truncated)
  ensure
    readers&.each { |reader| reader.kill if reader&.alive? }
  end
end