Module: Evrone::Common::Spawn::Process

Extended by:
Process
Included in:
Process
Defined in:
lib/evrone/common/spawn/process.rb

Instance Method Summary collapse

Instance Method Details

#spawn(*args, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/evrone/common/spawn/process.rb', line 10

def spawn(*args, &block)
  env     = args.first.is_a?(Hash) ? args.shift : {}
  options = args.last.is_a?(Hash)  ? args.pop   : {}
  cmd     = args.join(" ")

  select_timeout = options.delete(:pool_interval) || Spawn.pool_interval
  timeout        = Spawn::Timeout.new options.delete(:timeout)
  read_timeout   = Spawn::ReadTimeout.new options.delete(:read_timeout)

  r,w = IO.pipe
  r.sync = true

  pid = ::Process.spawn(env, cmd, options.merge(out: w, err: w))
  w.close

  read_loop r, timeout, read_timeout, select_timeout, &block

  ::Process.kill 'KILL', pid
  _, status = ::Process.wait2(pid) # protect from zombies

  compute_exit_code cmd, status, timeout, read_timeout
end