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.
-
#stdin ⇒ Object
Returns the value of attribute stdin.
-
#stdouts ⇒ Object
Returns the value of attribute stdouts.
Instance Method Summary collapse
- #cont ⇒ Object
-
#initialize(cmd, id: nil, env: {}) ⇒ ProcessYield
constructor
A new instance of ProcessYield.
- #kill ⇒ Object
- #running? ⇒ Boolean
- #signal(sig) ⇒ Object
- #spawn ⇒ Object
- #term ⇒ Object
- #to_json(json_generator_state = nil) ⇒ Object
- #tstp ⇒ Object
Constructor Details
#initialize(cmd, id: nil, env: {}) ⇒ ProcessYield
Returns a new instance of ProcessYield.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/pytty/daemon/process_yield.rb', line 7 def initialize(cmd, id:nil, env:{}) @cmd = cmd @env = env @pid = nil @id = id || SecureRandom.uuid @stdouts = [] @stdin = Async::Queue.new end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
18 19 20 |
# File 'lib/pytty/daemon/process_yield.rb', line 18 def cmd @cmd end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
18 19 20 |
# File 'lib/pytty/daemon/process_yield.rb', line 18 def id @id end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
18 19 20 |
# File 'lib/pytty/daemon/process_yield.rb', line 18 def pid @pid end |
#stdin ⇒ Object
Returns the value of attribute stdin.
19 20 21 |
# File 'lib/pytty/daemon/process_yield.rb', line 19 def stdin @stdin end |
#stdouts ⇒ Object
Returns the value of attribute stdouts.
19 20 21 |
# File 'lib/pytty/daemon/process_yield.rb', line 19 def stdouts @stdouts end |
Instance Method Details
#cont ⇒ Object
79 80 81 |
# File 'lib/pytty/daemon/process_yield.rb', line 79 def cont Process.kill("CONT", @pid) end |
#kill ⇒ Object
83 84 85 |
# File 'lib/pytty/daemon/process_yield.rb', line 83 def kill Process.kill("KILL", @pid) end |
#running? ⇒ Boolean
21 22 23 |
# File 'lib/pytty/daemon/process_yield.rb', line 21 def running? !@pid.nil? end |
#signal(sig) ⇒ Object
71 72 73 |
# File 'lib/pytty/daemon/process_yield.rb', line 71 def signal(sig) Process.kill(sig, @pid) end |
#spawn ⇒ Object
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 |
# File 'lib/pytty/daemon/process_yield.rb', line 35 def spawn executable, args = @cmd @env.merge!({ "TERM" => "vt100" }) 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.async do |subtask| while c = @stdin.dequeue do p c async_stdin.write c end end while c = async_stdout.read(1) @stdouts.each do |s| begin s.write c rescue Errno::EPIPE => ex puts "cannnot write, popping" @stdouts.pop end end end puts "CLOSED" #stdout.close end end |
#term ⇒ Object
87 88 89 |
# File 'lib/pytty/daemon/process_yield.rb', line 87 def term Process.kill("TERM", @pid) end |
#to_json(json_generator_state = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/pytty/daemon/process_yield.rb', line 25 def to_json(json_generator_state=nil) { id: @id, pid: @pid, cmd: @cmd, env: @env, running: running? }.to_json end |
#tstp ⇒ Object
75 76 77 |
# File 'lib/pytty/daemon/process_yield.rb', line 75 def tstp Process.kill("TSTP", @pid) end |