I have multiple instances of Rails servers and there is a need for each one of them to know its own listening port in environment.rb. request.port will work in the controllers but not in the context of the environment.rb. Is there a way? Thanks!
Asked
Active
Viewed 1,063 times
3
-
Out of curiosity, why does it need to know this? – Andrew Marshall Mar 05 '12 at 21:19
-
@andrew, Thanks for asking. I figured I would be asked given the collective cursorily of the community here :) The short answer is that we intend to open a matching UDP socket on the same port from within each server instance. Ask if you are curious about the need of the UDP port :) – timeon Mar 05 '12 at 21:49
-
I just don't think that's a particularly good idea. Your actual web server (nginx, apache, etc.) should be handling this, IMO, but I don't have much experience with that sort of setup anyway. – Andrew Marshall Mar 05 '12 at 21:53
-
Further to @AndrewMarshall's point, how is the port for each instance being set? – Jordan Running Mar 05 '12 at 21:56
-
We have 10 server instances each listening on a port between 8000 and 8009. HaProxy is sitting at the front to balance traffic to each of them. These are for TCP requests. We'd like to have each of the server instances to open a matching UDP port. Again, we'll have haproxy balancing the UDP traffic as well. – timeon Mar 06 '12 at 00:06
1 Answers
0
You can get the running rails port using this code:
Rails::Server.new.options[:Port]
You can take a look at the options parser in the Rails documentation to see what hash parameters are available.

Paulo Fidalgo
- 21,709
- 7
- 99
- 115
-
I'm having mixed results with this. In one place I was able to use it. In another place I got `uninitialized constant Rails::Server`...still trying to figure this one out. – Andrew May 11 '15 at 20:04
-
Even when this works, this only returns the default options, not the current options. – Andrew May 11 '15 at 20:13