Class: Pytty::Daemon::ProcessYield
- Inherits:
-
Object
- Object
- Pytty::Daemon::ProcessYield
- Defined in:
- lib/pytty/daemon/process_yield.rb
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#stderr_path ⇒ Object
readonly
Returns the value of attribute stderr_path.
-
#stdin ⇒ Object
Returns the value of attribute stdin.
-
#stdout_path ⇒ Object
readonly
Returns the value of attribute stdout_path.
Instance Method Summary collapse
- #add_stderr(stderr) ⇒ Object
- #add_stdout(stdout) ⇒ Object
- #cleanup ⇒ Object
-
#initialize(cmd, id: nil, env: {}) ⇒ ProcessYield
constructor
A new instance of ProcessYield.
- #running? ⇒ Boolean
- #signal(sig) ⇒ Object
- #spawn(tty: false, interactive: false) ⇒ Object
- #to_json(json_generator_state = nil) ⇒ Object
Constructor Details
#initialize(cmd, id: nil, env: {}) ⇒ ProcessYield
Returns a new instance of ProcessYield.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pytty/daemon/process_yield.rb', line 9 def initialize(cmd, id:nil, env:{}) @cmd = cmd @env = env @pid = nil @status = nil @id = id || SecureRandom.uuid @stdouts = {} @stderrs = {} @stdout_path = File.join Pytty::Daemon.pytty_path, "#{@id}.stdout" @stderr_path = File.join Pytty::Daemon.pytty_path, "#{@id}.stderr" @stdin = Async::Queue.new end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
26 27 28 |
# File 'lib/pytty/daemon/process_yield.rb', line 26 def cmd @cmd end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
26 27 28 |
# File 'lib/pytty/daemon/process_yield.rb', line 26 def id @id end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
26 27 28 |
# File 'lib/pytty/daemon/process_yield.rb', line 26 def pid @pid end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
26 27 28 |
# File 'lib/pytty/daemon/process_yield.rb', line 26 def status @status end |
#stderr_path ⇒ Object (readonly)
Returns the value of attribute stderr_path.
27 28 29 |
# File 'lib/pytty/daemon/process_yield.rb', line 27 def stderr_path @stderr_path end |
#stdin ⇒ Object
Returns the value of attribute stdin.
28 29 30 |
# File 'lib/pytty/daemon/process_yield.rb', line 28 def stdin @stdin end |
#stdout_path ⇒ Object (readonly)
Returns the value of attribute stdout_path.
27 28 29 |
# File 'lib/pytty/daemon/process_yield.rb', line 27 def stdout_path @stdout_path end |
Instance Method Details
#add_stderr(stderr) ⇒ Object
35 36 37 38 39 |
# File 'lib/pytty/daemon/process_yield.rb', line 35 def add_stderr(stderr) notification = Async::Notification.new @stderrs[notification] = stderr notification end |
#add_stdout(stdout) ⇒ Object
30 31 32 33 34 |
# File 'lib/pytty/daemon/process_yield.rb', line 30 def add_stdout(stdout) notification = Async::Notification.new @stdouts[notification] = stdout notification end |
#cleanup ⇒ Object
57 58 59 60 61 62 |
# File 'lib/pytty/daemon/process_yield.rb', line 57 def cleanup @status = nil File.unlink @stdout_path if File.exist? @stdout_path File.unlink @stderr_path if File.exist? @stderr_path end |
#running? ⇒ Boolean
41 42 43 44 |
# File 'lib/pytty/daemon/process_yield.rb', line 41 def running? # test by killing? !@pid.nil? end |
#signal(sig) ⇒ Object
217 218 219 220 221 222 |
# File 'lib/pytty/daemon/process_yield.rb', line 217 def signal(sig) return unless @pid Process.kill(sig.upcase, @pid) rescue Errno::ESRCH => ex raise ex unless ex. == "No such process" end |
#spawn(tty: false, interactive: false) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/pytty/daemon/process_yield.rb', line 64 def spawn(tty: false, interactive: false) return false if running? cleanup stdout_appender = Async::IO::Stream.new( File.open @stdout_path, "a" ) stderr_appender = Async::IO::Stream.new( File.open @stderr_path, "a" ) executable, *args = @cmd # @env.merge!({ # "TERM" => "xterm" # }) Async::Task.current.async do |task| real_stdout, real_stdin, real_stderr, @pid, wait_thr = begin if tty stderr_reader, stderr_writer = IO.pipe p ["spawn", "PTY"] #TODO: if bash is started, no prompt is written in stderr or stdout p_stdout, p_stdin, pid = PTY.spawn @env, executable, *args, err: stderr_writer [p_stdout, p_stdin, stderr_reader, pid] else p_stdin, p_stdout, p_stderr, p_wait_thr = Open3.popen3 @env, executable, *args, {} [p_stdout, p_stdin, p_stderr, p_wait_thr.pid, p_wait_thr] end rescue Errno::ENOENT => ex raise unless ex. == "No such file or directory - #{executable}" end next unless @pid #command failed to spawn async_stdout = Async::IO::Generic.new real_stdout async_stdin = Async::IO::Generic.new real_stdin async_stderr = Async::IO::Generic.new real_stderr task_stdin_writer = if interactive task.async do |subtask| p ["task_stdin_writer", "started"] while c = @stdin.dequeue do async_stdin.write c end rescue Async::Stop => ex puts "async_stdin#write Async::Stop" rescue Exception => ex puts "async_stdin#write: #{ex.inspect}" ensure async_stdin.close p ["async_stdin", "closed"] end else nil end task_stderr_writer = task.async do p ["task_stderr_writer", "started"] while c = async_stderr.read(1) do stderr_appender.write c stderr_appender.flush @stderrs.each do |notification, stderr| begin stderr.write "2#{c}" rescue Async::HTTP::Body::Writable::Closed puts "signaling error" notification.signal @stderrs.delete notification rescue => ex raise ex end end end p ["task_stderr_writer", "async_stderr has no more read"] rescue Exception => ex p ["async_stderr ex:", ex] ensure stderr_appender.flush stderr_appender.close puts "stderr_appender closed" async_stderr.close puts "async_stderr closed" end task_stdout_writer = task.async do p ["task_stdout_writer", "started"] while c = async_stdout.read(1) stdout_appender.write c stdout_appender.flush @stdouts.each do |notification, stdout| begin stdout.write "1#{c}" rescue Errno::EPIPE, Errno::EPROTOTYPE => ex notification.signal @stdouts.delete notification end end end p ["task_stdout_writer", "stopped"] rescue Async::Stop signal "kill" rescue Exception => ex p ["async_stdout", ex] ensure process_status = if wait_thr wait_thr.value else begin Process.wait(@pid) $? rescue Errno::ECHILD => ex raise ex unless ex. == "No child processes" puts "NO CHILD PROCESS" nil end end @status = if process_status && process_status.exitstatus process_status.exitstatus else Signal.signame process_status.termsig end puts "exited #{@id} with status: #{@status}" @pid = nil task_stdin_writer.stop if task_stdin_writer @stdouts.each do |notification, stdout| notification.signal @stdouts.delete notification end @stderrs.each do |notification, stderr| notification.signal @stdouts.delete notification end Pytty::Daemon.dump end end if @pid p ["spawned", id] return true else p ["failed to spawn"] @status = 127 return false end end |
#to_json(json_generator_state = nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pytty/daemon/process_yield.rb', line 46 def to_json(json_generator_state=nil) { id: @id, pid: @pid, status: @status, cmd: @cmd, env: @env, running: running? }.to_json end |