Class: LogsCfPlugin::TailingLogsClient

Inherits:
Object
  • Object
show all
Includes:
MessageWriter
Defined in:
lib/lds-cf-plugin/loggregator/tailling_logs_client.rb

Instance Method Summary collapse

Methods included from MessageWriter

#write

Constructor Details

#initialize(config) ⇒ TailingLogsClient

Returns a new instance of TailingLogsClient.



9
10
11
# File 'lib/lds-cf-plugin/loggregator/tailling_logs_client.rb', line 9

def initialize(config)
  @config = config
end

Instance Method Details

#logs_for(app, recent) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/lds-cf-plugin/loggregator/tailling_logs_client.rb', line 13

def logs_for(app, recent)
  protocol = use_ssl ? "wss" : "ws"
  websocket_address = "#{protocol}://#{loggregator_host}#{loggregator_port ? ":#{loggregator_port}" : ""}/#{recent ? 'dump' : 'tail'}/?app=#{app.guid}"

  output.puts "websocket_address: #{websocket_address}" if trace

  make_websocket_request(app, websocket_address)
end

#make_websocket_request(app, websocket_address) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# File 'lib/lds-cf-plugin/loggregator/tailling_logs_client.rb', line 22

def make_websocket_request(app, websocket_address)
  redirect_uri = nil
  @em_client_thread = Thread.new do
    EM.run {
      ws = Faye::WebSocket::Client.new(websocket_address, nil, :headers => {"Origin" => "http://localhost", "Authorization" => user_token})
      ws.on :open do |event|
        output.puts("Connected to server.")
        EventMachine.add_periodic_timer(keep_alive_interval) do
          ws.send("I'm alive!")
        end
      end

      ws.on :message do |event|
        begin
          received_message = LogMessage.decode(event.data.pack("C*"))
          write(app, output, received_message)
        rescue Exception => e
          output.puts e.message
          output.puts e.backtrace
          output.puts("Error parsing loggregator data. Please make sure you have the latest lds-cf-plugin.")
          ws.close
        end
      end

      ws.on :error do |event|
        if !redirect_uri
          if event.current_target.status == 302
            redirect_uri = event.current_target.headers["location"]
            ws.close
            @em_client_thread.kill
            ws = nil
          else
            output.puts("Server error: #{websocket_address}")
            output.puts(event.data.inspect) if trace
          end
        end
      end

      ws.on :close do |event|
        unless redirect_uri
          ws.close
          case event.code
            when 4000
              output.puts("Error: No space given.")
            when 4001
              output.puts("Error: No authorization token given.")
            when 4002
              output.puts("Error: Not authorized.")
          end
          output.puts("Server connection closed.")
          @em_client_thread.kill
          ws = nil
        end
      end
    }
  end

  wait_for_em_thread_to_finish

  make_websocket_request(app, redirect_uri) if redirect_uri
end

#wait_for_em_thread_to_finishObject



84
85
86
87
88
# File 'lib/lds-cf-plugin/loggregator/tailling_logs_client.rb', line 84

def wait_for_em_thread_to_finish
  while @em_client_thread.alive? do
    sleep 0.1
  end
end