Class: Scalingo::Realtime::Logs

Inherits:
Object
  • Object
show all
Defined in:
lib/scalingo/realtime/logs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Logs

Returns a new instance of Logs.



9
10
11
12
# File 'lib/scalingo/realtime/logs.rb', line 9

def initialize(app)
  @app = app
  @callbacks = []
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



7
8
9
# File 'lib/scalingo/realtime/logs.rb', line 7

def app
  @app
end

Instance Method Details

#each_line(&block) ⇒ Object



14
15
16
# File 'lib/scalingo/realtime/logs.rb', line 14

def each_line(&block)
  @callbacks << block
end

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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