Class: PRCS::Runner
- Inherits:
-
Object
- Object
- PRCS::Runner
- Defined in:
- lib/prcs.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #exit_code ⇒ Object
- #exited? ⇒ Boolean
-
#initialize(command) ⇒ Runner
constructor
A new instance of Runner.
- #kill!(timeout = 15) ⇒ Object
- #run!(stdin = nil) ⇒ Object
- #run_and_wait!(stdin = nil) ⇒ Object
- #stderr ⇒ Object
- #stderr_queue ⇒ Object
- #stdin ⇒ Object
- #stdout ⇒ Object
- #stdout_queue ⇒ Object
Constructor Details
#initialize(command) ⇒ Runner
Returns a new instance of Runner.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/prcs.rb', line 12 def initialize(command) @command = command @process = nil @stdout = nil @stderr = nil @external_queues = {}.tap do |it| it[:stdout] = Queue.new it[:stderr] = Queue.new end @logcollectors = {} end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
10 11 12 |
# File 'lib/prcs.rb', line 10 def command @command end |
Instance Method Details
#alive? ⇒ Boolean
148 149 150 |
# File 'lib/prcs.rb', line 148 def alive? @process.alive? end |
#exit_code ⇒ Object
156 157 158 |
# File 'lib/prcs.rb', line 156 def exit_code @process.exit_code end |
#exited? ⇒ Boolean
152 153 154 |
# File 'lib/prcs.rb', line 152 def exited? @process.exited? end |
#kill!(timeout = 15) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/prcs.rb', line 85 def kill!(timeout = 15) @logcollectors.values.each do |collector_thread| collector_thread[:running] = false end begin @process.poll_for_exit(timeout) rescue ChildProcess::TimeoutError @process.stop end @logcollectors.values.each do |collector_thread| Thread.kill(collector_thread) end @stdout = @logcollectors[:stdout][:log] @stderr = @logcollectors[:stderr][:log] self end |
#run!(stdin = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/prcs.rb', line 25 def run!(stdin = nil) stdout_pipe, stdout_wr = IO.pipe stderr_pipe, stderr_wr = IO.pipe ChildProcess.posix_spawn = true @process = ChildProcess.build(*@command) @process.duplex = true if stdin @process.io.stdout = stdout_wr @process.io.stderr = stderr_wr @process.start stdout_wr.close stderr_wr.close if stdin @process.io.stdin.write(stdin) @process.io.stdin.close end @logcollectors[:stdout] = Thread.new(stdout_pipe) do |pipe| Thread.current[:running] = true Thread.current[:log] = "" while Thread.current[:running] begin output = pipe.readline_nonblock @external_queues[:stdout] << output Thread.current[:log] << output sleep(0.1) rescue IO::EAGAINWaitReadable, EOFError end end end @logcollectors[:stderr] = Thread.new(stderr_pipe) do |pipe| Thread.current[:running] = true Thread.current[:log] = "" while Thread.current[:running] begin output = pipe.readline_nonblock @external_queues[:stderr] << output Thread.current[:log] << output sleep(0.1) rescue IO::EAGAINWaitReadable, EOFError end end end self end |
#run_and_wait!(stdin = nil) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/prcs.rb', line 78 def run_and_wait!(stdin = nil) self.run!(stdin) @process.wait self end |
#stderr ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/prcs.rb', line 140 def stderr if alive? raise "Process still alive, use queue-method instead" else @stderr end end |
#stderr_queue ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/prcs.rb', line 117 def stderr_queue "".tap { |it| begin while(true) it << @external_queues[:stderr].pop(true) end rescue ThreadError end } end |
#stdin ⇒ Object
128 129 130 |
# File 'lib/prcs.rb', line 128 def stdin @process.io.stdin end |
#stdout ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/prcs.rb', line 132 def stdout if alive? raise "Process still alive, use queue-method instead" else @stdout end end |
#stdout_queue ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/prcs.rb', line 106 def stdout_queue "".tap { |it| begin while(true) it << @external_queues[:stdout].pop(true) end rescue ThreadError end } end |