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 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:

Examples:

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_file:, argv:) ⇒ IOHandle

Returns a new instance of IOHandle.

Parameters:

  • io (IO)
  • pid (Integer)
  • output_file (File)
  • argv (Array<String>)
  • pid: (Integer)
  • output_file: (File)
  • argv: (Array[String])


29
30
31
32
33
34
35
# File 'lib/yobi/io_handle.rb', line 29

def initialize(io, pid:, output_file:, argv:)
  @io = io
  @pid = pid
  @output_file = output_file
  @argv = argv
  @closed = false
end

Instance Attribute Details

#ioIO (readonly)

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

Returns:

  • (IO)

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



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

def io
  @io
end

#pidInteger (readonly)

Returns the Restic process's pid.

Returns:

  • (Integer)

    the Restic process's pid



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

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.



46
47
48
49
50
51
52
53
# File 'lib/yobi/io_handle.rb', line 46

def close
  return if @closed

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

#closed?Boolean

Returns:

  • (Boolean)


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

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)


59
60
61
62
63
64
65
66
# File 'lib/yobi/io_handle.rb', line 59

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