Class: AgentJail::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_jail/runner.rb

Overview

Orchestrates the fork + pipe + monitor pattern. Forks a child, applies a wall-clock timeout monitor thread, reads the result.

Instance Method Summary collapse

Constructor Details

#initialize(options, &block) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
14
15
# File 'lib/agent_jail/runner.rb', line 7

def initialize(options, &block)
  cfg = AgentJail.configuration
  @timeout     = options.fetch(:timeout, cfg.default_timeout)
  @cpu_timeout = options.fetch(:cpu_timeout, @timeout)
  @memory_mb   = options.fetch(:memory_mb, cfg.default_memory_mb)
  @fs_allow    = options.fetch(:fs_allow, cfg.default_fs_allow)
  @fs_read_allow = options.fetch(:fs_read_allow, [])
  @block = block
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/agent_jail/runner.rb', line 17

def call
  read, write = IO.pipe

  child_pid = fork do
    read.close
    Child.new(
      write: write,
      cpu_timeout: @cpu_timeout,
      memory_mb: @memory_mb,
      fs_allow: @fs_allow,
      fs_read_allow: @fs_read_allow,
      block: @block
    ).run
    exit!(0)
  end

  write.close
  raw, status = wait_for_child(read, child_pid)
  read.close

  parse_result(raw, status)
end