Class: Ferrum::Browser::Process::Output
- Inherits:
-
Object
- Object
- Ferrum::Browser::Process::Output
- Defined in:
- lib/ferrum/browser/process/output.rb,
sig/ferrum/browser/process/output.rbs
Overview
What the browser process wrote to stdout and stderr: keeps the last
LIMIT lines and writes each one to the :browser log channel. Owns
the pipe the process writes to, which is read on its own thread for as
long as the process lives, so it never fills up and blocks the browser.
Constant Summary collapse
- LIMIT =
100- DRAIN_TIMEOUT =
0.5
Instance Method Summary collapse
-
#<<(chunk) ⇒ self
Adds a chunk of output, which can end in the middle of a line.
-
#close ⇒ void
Gives the reading thread DRAIN_TIMEOUT seconds to take what the process wrote last, then closes the pipe.
-
#follow ⇒ Thread
Keeps reading the pipe on a new thread until it's closed.
-
#initialize(log = nil) ⇒ Output
constructor
A new instance of Output.
-
#lines ⇒ Array<String>
The last lines, oldest first.
-
#pipe {|io| ... } ⇒ void
Opens the pipe and yields its write end, to be given to the process as stdout and stderr.
-
#to_s ⇒ String
The last lines joined by newlines.
-
#wait_for(regexp, timeout) ⇒ MatchData?
Reads the pipe until the output matches, the process exits or the time is up.
Constructor Details
#initialize(log = nil) ⇒ Output
Returns a new instance of Output.
16 17 18 19 20 21 22 |
# File 'lib/ferrum/browser/process/output.rb', line 16 def initialize(log = nil) @log = log @lines = [] @partial = String.new(encoding: Encoding::BINARY) @mutex = Mutex.new @io = @reader = nil end |
Instance Method Details
#<<(chunk) ⇒ self
Adds a chunk of output, which can end in the middle of a line.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ferrum/browser/process/output.rb', line 98 def <<(chunk) lines = @mutex.synchronize do *complete, @partial = (@partial + chunk.b).split("\n", -1) complete.map! { |line| utf8(line) } @lines.concat(complete) @lines.shift(@lines.size - LIMIT) if @lines.size > LIMIT complete end lines.each { |line| @log&.browser(line) } self end |
#close ⇒ void
This method returns an undefined value.
Gives the reading thread DRAIN_TIMEOUT seconds to take what the process wrote last, then closes the pipe. The lines are kept.
85 86 87 88 89 |
# File 'lib/ferrum/browser/process/output.rb', line 85 def close @reader&.join(DRAIN_TIMEOUT) close_io(@io) @io = @reader = nil end |
#follow ⇒ Thread
Keeps reading the pipe on a new thread until it's closed.
71 72 73 74 75 76 77 |
# File 'lib/ferrum/browser/process/output.rb', line 71 def follow @reader = Utils::Thread.spawn do loop { self << @io.readpartial(4096) } rescue IOError self << "\n" unless @partial.empty? end end |
#lines ⇒ Array<String>
The last lines, oldest first.
116 117 118 |
# File 'lib/ferrum/browser/process/output.rb', line 116 def lines @mutex.synchronize { @partial.empty? ? @lines.dup : [*@lines, utf8(@partial)] } end |
#pipe {|io| ... } ⇒ void
This method returns an undefined value.
Opens the pipe and yields its write end, to be given to the process as stdout and stderr. The write end is closed here afterwards, so reading hits the end of the pipe once the process exits.
33 34 35 36 37 38 |
# File 'lib/ferrum/browser/process/output.rb', line 33 def pipe @io, writer = IO.pipe yield writer ensure close_io(writer) end |
#to_s ⇒ String
The last lines joined by newlines.
125 126 127 |
# File 'lib/ferrum/browser/process/output.rb', line 125 def to_s lines.join("\n") end |
#wait_for(regexp, timeout) ⇒ MatchData?
Reads the pipe until the output matches, the process exits or the time is up.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ferrum/browser/process/output.rb', line 50 def wait_for(regexp, timeout) deadline = Utils::ElapsedTime.monotonic_time + timeout while (now = Utils::ElapsedTime.monotonic_time) < deadline begin self << @io.read_nonblock(512) rescue IO::WaitReadable @io.wait_readable(deadline - now) rescue EOFError return else match = to_s.match(regexp) return match if match end end end |