Class: Yobi::IOHandle
- Inherits:
-
Object
- Object
- Yobi::IOHandle
- 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
-
#io ⇒ IO
readonly
The readable end of the pipe Restic's stdout is wired to.
-
#pid ⇒ Integer
readonly
The Restic process's pid.
Instance Method Summary collapse
-
#close ⇒ void
Reaps the Restic process and raises based on its exit code.
-
#closed? ⇒ Boolean
trueonce #close has run. -
#each {|chunk| ... } ⇒ void
Yields binary-safe chunks of #io until EOF, then calls #close.
-
#initialize(io, pid:, output:, argv:) ⇒ IOHandle
constructor
:nodoc:.
- #inspect ⇒ Object
Constructor Details
#initialize(io, pid:, output:, argv:) ⇒ IOHandle
:nodoc:
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
#io ⇒ IO (readonly)
The readable end of the pipe Restic's stdout is wired to.
20 21 22 |
# File 'lib/yobi/io_handle.rb', line 20 def io @io end |
#pid ⇒ Integer (readonly)
The Restic process's pid.
22 23 24 |
# File 'lib/yobi/io_handle.rb', line 22 def pid @pid end |
Instance Method Details
#close ⇒ void
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.
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.
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 |
#inspect ⇒ Object
37 38 39 |
# File 'lib/yobi/io_handle.rb', line 37 def inspect "#<#{self.class} pid=#{pid} closed=#{closed?}>" end |