Class: Profiler::Api::EventsController

Inherits:
Profiler::ApplicationController show all
Includes:
ActionController::Live
Defined in:
app/controllers/profiler/api/events_controller.rb

Instance Method Summary collapse

Instance Method Details

#subscribeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/profiler/api/events_controller.rb', line 9

def subscribe
  response.headers["Content-Type"]      = "text/event-stream"
  response.headers["Cache-Control"]     = "no-cache"
  response.headers["X-Accel-Buffering"] = "no"

  sse = ActionController::Live::SSE.new(response.stream, retry: 3000, event: "profile_update")
  id  = Profiler::SSE.current.subscribe(params[:token], Array(params[:collectors]))

  begin
    loop do
      event = Profiler::SSE.current.wait_for_event(id, timeout: 30)
      if event
        sse.write(event)
      else
        sse.write({}, event: "heartbeat")
      end
    end
  rescue ActionController::Live::ClientDisconnected, IOError
    # Client disconnected — normal exit
  ensure
    Profiler::SSE.current.unsubscribe(id)
    sse.close
  end
end