Method: DTAS::Pipeline#pspawn

Defined in:
lib/dtas/pipeline.rb

#pspawn(env, cmd, rdr = {}) ⇒ Object

Process.spawn wrapper which supports running Proc-like objects in a separate process, not just external commands. Returns the pid of the spawned process



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dtas/pipeline.rb', line 13

def pspawn(env, cmd, rdr = {})
  case cmd
  when Array
    spawn(env, *cmd, rdr)
  else # support running Proc-like objects, too:
    fork do
      ENV.update(env) if env

      # setup redirects
      [ $stdin, $stdout, $stderr ].each_with_index do |io, fd|
        dst = rdr[fd] and io.reopen(dst)
      end

      # close all other pipes, since we can't rely on FD_CLOEXEC
      # (as we do not exec, here)
      rdr.each do |k, v|
        k.close if v == :close
      end
      cmd.call
    end
  end
end