Method: #run_command_and_stream

Defined in:
lib/easel/websocket.rb

#run_command_and_stream(socket, cmd_id) ⇒ Object

run_command_and_stream

Run a command and stream the stdout and stderr through the websocket.



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
# File 'lib/easel/websocket.rb', line 62

def run_command_and_stream(socket, cmd_id)

  cmd = get_command cmd_id
  if cmd.nil?
    log_error "Client requested command ID #{cmd_id} be run, but that ID does not exist."
    return
  end
  Open3::popen3(cmd) do |stdin, stdout, stderr, cmd_thread|

    continue = true

    while ready_fds = IO.select([stdout, stderr])[0]
      ready_fds.each{ |fd|
        resp = fd.gets
        if resp.nil?
          continue = false
          break
        end
        if fd == stdout
          send_msg(socket, cmd_id, "OUT", resp, )
        elsif fd == stderr
          send_msg(socket, cmd_id, "ERR", resp, )
        else
          raise "Received output from popen3(#{cmd}) that was not via stdout or stderr."
        end
      }
      break unless continue
    end

    cmd_thread.join
    send_msg(socket, cmd_id, "FINISHED")
  end
end