Class: Pytty::Daemon::ProcessYield

Inherits:
Object
  • Object
show all
Defined in:
lib/pytty/daemon/process_yield.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#cmdObject (readonly)

Returns the value of attribute cmd.



18
19
20
# File 'lib/pytty/daemon/process_yield.rb', line 18

def cmd
  @cmd
end

#idObject (readonly)

Returns the value of attribute id.



18
19
20
# File 'lib/pytty/daemon/process_yield.rb', line 18

def id
  @id
end

#pidObject (readonly)

Returns the value of attribute pid.



18
19
20
# File 'lib/pytty/daemon/process_yield.rb', line 18

def pid
  @pid
end

#stdinObject

Returns the value of attribute stdin.



19
20
21
# File 'lib/pytty/daemon/process_yield.rb', line 19

def stdin
  @stdin
end

#stdoutsObject

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

#contObject



79
80
81
# File 'lib/pytty/daemon/process_yield.rb', line 79

def cont
  Process.kill("CONT", @pid)
end

#killObject



83
84
85
# File 'lib/pytty/daemon/process_yield.rb', line 83

def kill
  Process.kill("KILL", @pid)
end

#running?Boolean

Returns:

  • (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

#spawnObject



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

#termObject



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

#tstpObject



75
76
77
# File 'lib/pytty/daemon/process_yield.rb', line 75

def tstp
  Process.kill("TSTP", @pid)
end