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.
-
#stdin ⇒ Object
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
- #add_stdout(stdout) ⇒ Object
-
#initialize(cmd, id: nil, env: {}) ⇒ ProcessYield
constructor
A new instance of ProcessYield.
- #running? ⇒ Boolean
- #signal(sig) ⇒ Object
- #spawn ⇒ Object
- #to_json(json_generator_state = nil) ⇒ Object
Constructor Details
#initialize(cmd, id: nil, env: {}) ⇒ ProcessYield
Returns a new instance of ProcessYield.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/pytty/daemon/process_yield.rb', line 8 def initialize(cmd, id:nil, env:{}) @cmd = cmd @env = env @pid = nil @status = nil @id = id || SecureRandom.uuid @stdout = nil @stdouts = {} @stdin = Async::Queue.new end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
21 22 23 |
# File 'lib/pytty/daemon/process_yield.rb', line 21 def cmd @cmd end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
21 22 23 |
# File 'lib/pytty/daemon/process_yield.rb', line 21 def id @id end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
21 22 23 |
# File 'lib/pytty/daemon/process_yield.rb', line 21 def pid @pid end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
21 22 23 |
# File 'lib/pytty/daemon/process_yield.rb', line 21 def status @status end |
#stdin ⇒ Object
Returns the value of attribute stdin.
22 23 24 |
# File 'lib/pytty/daemon/process_yield.rb', line 22 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
21 22 23 |
# File 'lib/pytty/daemon/process_yield.rb', line 21 def stdout @stdout end |
Instance Method Details
#add_stdout(stdout) ⇒ Object
24 25 26 27 28 |
# File 'lib/pytty/daemon/process_yield.rb', line 24 def add_stdout(stdout) notification = Async::Notification.new @stdouts[notification] = stdout notification end |
#running? ⇒ Boolean
30 31 32 |
# File 'lib/pytty/daemon/process_yield.rb', line 30 def running? !@pid.nil? end |
#signal(sig) ⇒ Object
111 112 113 114 115 |
# File 'lib/pytty/daemon/process_yield.rb', line 111 def signal(sig) sig_upcased = sig.upcase p ["signaling", sig_upcased, "to", @pid] Process.kill(sig_upcased, @pid) end |
#spawn ⇒ Object
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 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 |
# File 'lib/pytty/daemon/process_yield.rb', line 45 def spawn return false if running? executable, args = @cmd # @env.merge!({ # "TERM" => "xterm" # }) stdout_path = File.join(Pytty::Daemon.pytty_path, @id) File.unlink stdout_path if File.exist? stdout_path stdout_appender = Async::IO::Stream.new( File.open stdout_path, "a" ) @stdout = Async::IO::Stream.new( File.open stdout_path, "r" ) Async::Task.current.async do |task| p ["spawn", executable, args, @env] real_stdout, real_stdin, pid = PTY.spawn @env, executable, *args @pid = pid async_stdout = Async::IO::Generic.new real_stdout async_stdin = Async::IO::Generic.new real_stdin task_stdin_writer = task.async do |subtask| while c = @stdin.dequeue do async_stdin.write c end rescue Exception => ex puts "async_stdin.write: #{ex.inspect}" end task_stdout_writer = task.async do |subtask| while c = async_stdout.read(1) stdout_appender.write c stdout_appender.flush @stdouts.each do |notification, stdout| begin stdout.write c rescue Errno::EPIPE => ex notification.signal @stdouts.delete notification end end end ensure task_stdin_writer.stop Process.wait(@pid) @status = $?.exitstatus @pid = nil stdout_appender.close @stdouts.each do |notification, stdout| p ["notifying: #{notification}"] notification.signal @stdouts.delete notification end p ["exited", @cmd, "status", @status] end end.wait puts "spawned" p ["@stdouts", @stdouts] return true end |
#to_json(json_generator_state = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pytty/daemon/process_yield.rb', line 34 def to_json(json_generator_state=nil) { id: @id, pid: @pid, status: @status, cmd: @cmd, env: @env, running: running? }.to_json end |