Skip to content

Instantly share code, notes, and snippets.

@playerconnect
playerconnect / backend_server_port.rb
Created June 2, 2010 22:36
Resolve thin backend server port from Rails controller
# request.port is unfortunately useless from within the app, as it
# does not reflect the backend bind port, but the frontend proxy.
# So we have to reverse lookup the PID to figure out our port.
def backend_server_port
Thread.current[:backend_server_port] ||= begin
logger.debug "Attempting to reverse-lookup server port from PID file"
port = nil
Dir["#{RAILS_ROOT}/tmp/pids/thin.*.pid"].each do |f|
pid = File.read(f).chomp.to_i
if pid == $$