Module: ForkExec

Defined in:
lib/forkexec.rb

Class Method Summary collapse

Class Method Details

.timeout(timeout) ⇒ Object



6
7
8
9
10
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
# File 'lib/forkexec.rb', line 6

def self.timeout timeout
  raise "Tmpfs in /dev/shm does not exist! Create it..." if not File.exist?("/dev/shm")

  shm = "/dev/shm/forkexec" 

  Dir.mkdir(shm) if not File.exist?(shm)

  pid = fork do
    output = yield
    File.open(File.join("/dev/shm/forkexec",Process.pid.to_s),"w"){|f| f.write(output.to_yaml)}
    exit! 0
  end

  result = nil

  begin
    Timeout::timeout(timeout) do
      Process.waitpid pid
      result = YAML::load_file(File.join(shm,pid.to_s))
    end
  rescue Timeout::Error
    Process.kill(9, pid)
    Process.waitpid pid
    raise Timeout::Error, "PID #{pid} timeouted"
  end
  
  shm_file = File.join("/dev/shm/forkexec/",pid.to_s)
  File.delete(shm_file) if File.exist?(shm_file)
  
  return result
end