Module: Efflux::Stream

Includes:
ActionController::Live
Defined in:
lib/efflux/stream.rb

Instance Method Summary collapse

Instance Method Details

#console_to_html(string) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/efflux/stream.rb', line 28

def console_to_html(string)
    string.gsub!("\n", '<br/>')
    if string =~ /\[[0-9]{1,2}/
        string.gsub!('[32m', '<span class="foreground-green">')
        string.gsub!('[0m','</span>')
        string = %Q{<span class="foreground-green">#{string}</span>}
    end
    return string
end

#stream_cmd(cmd) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/efflux/stream.rb', line 5

def stream_cmd(cmd)
    response.headers["Content-Type"] = "text/event-stream"
    Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr| 
        output = ""
        last_output = Time.current
        while line = stdout.gets
            output += console_to_html(line)
            if last_output < 1.seconds.ago 
                response.stream.write "event: command.data\n" 
                response.stream.write "data: #{output}\n\n"
                last_output = Time.current
                output = ""
            end
        end
        response.stream.write "event: command.data\n" 
        response.stream.write "data: #{output}<br/><div class=\"command-finished\">Command Finished at: #{Time.current}</div>\n\n"
    end
rescue IOError => e
    response.stream.close
ensure
    response.stream.close
end