Class: ACE::ForkUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/fork_util.rb

Class Method Summary collapse

Class Method Details

.isolateObject

Forks and calls a function It is expected that the function returns a JSON response Throws an exception if JSON.generate fails to generate



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ace/fork_util.rb', line 11

def self.isolate
  reader, writer = IO.pipe
  pid = fork {
    success = true
    begin
      response = yield
      writer.puts JSON.generate(response)
    rescue StandardError => e
      writer.puts(e)
      success = false
    ensure
      Process.exit! success
    end
  }
  unless pid
    log "Could not fork"
    exit 1
  end
  writer.close
  output = reader.read
  Process.wait(pid)
  if $CHILD_STATUS != 0
    raise output
  else
    JSON.parse(output)
  end
end