Skip to content

Commit

Permalink
Log request start and finish
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo committed Apr 7, 2015
1 parent 265f1f1 commit de83909
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions lib/action_cable/connection/base.rb
Expand Up @@ -16,7 +16,9 @@ def initialize(server, env)
end

def process
if Faye::WebSocket.websocket?(@env)
logger.info "[ActionCable] #{started_request_message}"

if websocket?
@subscriptions = {}

@websocket = Faye::WebSocket.new(@env)
Expand All @@ -40,6 +42,8 @@ def process
end

@websocket.on(:close) do |event|
logger.info "[ActionCable] #{finished_request_message}"

worker_pool.async.invoke(self, :cleanup_subscriptions)
worker_pool.async.invoke(self, :cleanup_internal_redis_subscriptions)
worker_pool.async.invoke(self, :disconnect) if respond_to?(:disconnect)
Expand Down Expand Up @@ -75,7 +79,7 @@ def cleanup_subscriptions
end

def broadcast(data)
logger.info "Sending data: #{data}"
logger.info "[ActionCable] Sending data: #{data}"
@websocket.send data
end

Expand Down Expand Up @@ -133,13 +137,39 @@ def unsubscribe_channel(data)
end

def invalid_request
logger.info "[ActionCable] #{finished_request_message}"
[404, {'Content-Type' => 'text/plain'}, ['Page not found']]
end

def websocket_alive?
@websocket && @websocket.ready_state == Faye::WebSocket::API::OPEN
end

def request
@request ||= ActionDispatch::Request.new(env)
end

def websocket?
@is_websocket ||= Faye::WebSocket.websocket?(@env)
end

def started_request_message
'Started %s "%s"%s for %s at %s' % [
request.request_method,
request.filtered_path,
websocket? ? ' [Websocket]' : '',
request.ip,
Time.now.to_default_s ]
end

def finished_request_message
'Finished "%s"%s for %s at %s' % [
request.filtered_path,
websocket? ? ' [Websocket]' : '',
request.ip,
Time.now.to_default_s ]
end

end
end
end

0 comments on commit de83909

Please sign in to comment.