Class: Yobi::IOHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/yobi/io_handle.rb,
sig/yobi.rbs

Overview

A live Restic process streaming raw bytes to its own stdout, returned by Yobi::Restic#run_dump when no block is given. Satisfies Rack's Body contract (+#each+, #close).

Unlike the block form, nothing closes this automatically. Call #close yourself once done reading, in an ensure, so it still runs if reading raises:

handle = repo.dump(snapshot_id: "latest", file: "/etc/hosts")
begin
IO.copy_stream(handle.io, "/tmp/hosts")
ensure
handle.close
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, pid:, output:, argv:) ⇒ IOHandle

:nodoc:

Parameters:

  • io (IO)
  • pid: (Integer)
  • output: (ResticOutput)
  • argv: (Array[String])


24
25
26
27
28
29
30
# File 'lib/yobi/io_handle.rb', line 24

def initialize(io, pid:, output:, argv:) # :nodoc:
  @io = io
  @pid = pid
  @output = output
  @argv = argv
  @closed = false
end

Instance Attribute Details

#ioIO (readonly)

The readable end of the pipe Restic's stdout is wired to.

Returns:

  • (IO)


20
21
22
# File 'lib/yobi/io_handle.rb', line 20

def io
  @io
end

#pidInteger (readonly)

The Restic process's pid.

Returns:

  • (Integer)


22
23
24
# File 'lib/yobi/io_handle.rb', line 22

def pid
  @pid
end

Instance Method Details

#closevoid

This method returns an undefined value.

Reaps the Restic process and raises based on its exit code. Safe to call more than once.



43
44
45
46
47
48
49
50
# File 'lib/yobi/io_handle.rb', line 43

def close
  return if @closed

  @closed = true
  @io.close unless @io.closed?
  _, status = Process.wait2(@pid)
  Restic.dispatch(exit_code: status.exitstatus, output: @output, argv: @argv)
end

#closed?Boolean

true once #close has run.

Returns:

  • (Boolean)


33
34
35
# File 'lib/yobi/io_handle.rb', line 33

def closed?
  @closed
end

#each {|chunk| ... } ⇒ void

This method returns an undefined value.

Yields binary-safe chunks of #io until EOF, then calls #close.

Yields:

Yield Parameters:

  • chunk (String)

Yield Returns:

  • (void)


53
54
55
56
57
58
59
60
# File 'lib/yobi/io_handle.rb', line 53

def each
  loop do
    yield @io.readpartial(64 * 1024)
  rescue EOFError
    break
  end
  close
end

#inspectObject



37
38
39
# File 'lib/yobi/io_handle.rb', line 37

def inspect
  "#<#{self.class} pid=#{pid} closed=#{closed?}>"
end