Module: EY::Backup::Spawner

Extended by:
Spawner, Forwardable
Included in:
Engine, GZipper, Spawner, Splitter
Defined in:
lib/ey_backup/spawner.rb

Constant Summary collapse

CHUNK_SIZE =
4096

Instance Method Summary collapse

Instance Method Details

#ioify(stdout, stdin, stderr, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ey_backup/spawner.rb', line 33

def ioify(stdout, stdin, stderr, &block)
  if String === stdin
    File.open(stdin, 'r') do |f|
      ioify(stdout, f, stderr, &block)
    end
  elsif String === stdout
    File.open(stdout, 'w') do |f|
      ioify(f, stdin, stderr, &block)
    end
  else
    yield stdout, stdin, stderr
  end
end

#run(command) ⇒ Object



21
22
23
24
25
# File 'lib/ey_backup/spawner.rb', line 21

def run(command)
  unless runs?(command)
    raise "Failed to run #{command.inspect}"
  end
end

#runs?(command) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/ey_backup/spawner.rb', line 27

def runs?(command)
  pid, *_ = Open4.popen4(command)
  pid, status = Process::waitpid2(pid)
  status.success?
end

#spawn(command, stdout = nil, stdin = nil, stderr = logger.stderr) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/ey_backup/spawner.rb', line 11

def spawn(command, stdout = nil, stdin = nil, stderr = logger.stderr)
  ioify(stdout, stdin, stderr) do |o, i, e|
    ios = {:stderr => e}
    ios[:stdout] = o if o
    ios[:stdin] = i if i
    result = Open4.spawn([command], ios)

  end
end