Class: WatchedProcess
- Inherits:
-
Object
- Object
- WatchedProcess
- Defined in:
- lib/stream2tracks/process.rb
Overview
A WatchedProcess is a process producing output which can be watched to indicate progress so far and validate output.
-
It supports these behaviours with the following callbacks:
progress[@buffer] => [current,total] validate[@buffer] => true when download is complete and successful - validate may raise if an unrecoverable error occurs before the process is complete
Defined Under Namespace
Classes: Status
Constant Summary collapse
- MAX_READ_LEN =
completely arbitrary
1024
Instance Method Summary collapse
-
#initialize(cmd, env = {}) {|_self| ... } ⇒ WatchedProcess
constructor
A new instance of WatchedProcess.
- #watch(options) ⇒ Object
Constructor Details
#initialize(cmd, env = {}) {|_self| ... } ⇒ WatchedProcess
Returns a new instance of WatchedProcess.
60 61 62 63 64 65 66 |
# File 'lib/stream2tracks/process.rb', line 60 def initialize cmd,env={} @out,out_write=IO.pipe @cmd=cmd @pid=spawn env,@cmd,:in=>:close,:out=>out_write,:err=>[:child,:out] out_write.close yield self if block_given? end |
Instance Method Details
#watch(options) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/stream2tracks/process.rb', line 68 def watch progress,validate=.progress,.validate @buffer||='' output=@out.readpartial MAX_READ_LEN rescue EOFError @buffer << output if output $stderr.print output if .debug and output current,total=progress[@buffer] if progress eof=@out.eof? valid=validate[@buffer] if validate Status.new eof,valid,current,total end |