class Scalingo::Realtime::Logs

Attributes

app[R]

Public Class Methods

new(app) click to toggle source
# File lib/scalingo/realtime/logs.rb, line 9
def initialize(app)
  @app = app
  @callbacks = []
end

Public Instance Methods

each_line(&block) click to toggle source
# File lib/scalingo/realtime/logs.rb, line 14
def each_line(&block)
  @callbacks << block
end
start() click to toggle source
# File lib/scalingo/realtime/logs.rb, line 18
def start
  each_line(&Proc.new) if block_given?

  EM.run do
    ws = Faye::WebSocket::Client.new(url)

    ws.on :open do |event|
    end

    ws.on :message do |event|
      data = JSON.parse(event.data)
      @callbacks.each { |c| c.call(data['log']) } if data['event'] == 'log'
    end

    ws.on :close do
      ws = nil
    end

    Signal.trap('INT')  { EM.stop }
    Signal.trap('TERM') { EM.stop }
  end
end

Protected Instance Methods

url() click to toggle source
# File lib/scalingo/realtime/logs.rb, line 43
def url
  url = app.logs_url.gsub(/^http/, 'ws')
  return "#{url}&stream=true"
end