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.



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

#cmdObject (readonly)

Returns the value of attribute cmd.



21
22
23
# File 'lib/pytty/daemon/process_yield.rb', line 21

def cmd
  @cmd
end

#idObject (readonly)

Returns the value of attribute id.



21
22
23
# File 'lib/pytty/daemon/process_yield.rb', line 21

def id
  @id
end

#pidObject (readonly)

Returns the value of attribute pid.



21
22
23
# File 'lib/pytty/daemon/process_yield.rb', line 21

def pid
  @pid
end

#statusObject (readonly)

Returns the value of attribute status.



21
22
23
# File 'lib/pytty/daemon/process_yield.rb', line 21

def status
  @status
end

#stdinObject

Returns the value of attribute stdin.



22
23
24
# File 'lib/pytty/daemon/process_yield.rb', line 22

def stdin
  @stdin
end

#stdoutObject (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

Returns:

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

#spawnObject



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