mongrel_rails - programatically report which port it's running on
Asked Answered
H

3

8

On my local machine, i run rails with mongrel. I have some stuff which runs when it starts, via a file in config/initializers, which uses puts to tell me which database it's using, what is being used to send emails, and a few other bits of info.

When I run a cluster of mongrels, on ports 3000, 3001 and 3002, I only want to do this reporting stuff for the mongrel on port 3000. So, I need to wrap it in an if block which tests which port the currently running mongrel is using. Can anyone tell me how I can get this in my code?

Hackler answered 13/5, 2015 at 9:12 Comment(0)
D
2

in an initializer,

puts Rails::Server.new.options[:Port]

can report your port.

Dip answered 20/5, 2015 at 15:30 Comment(10)
Thanks @Alper - sorry, i should have said that the app in question is running Rails 2.2.2. Your code is erroring for me with uninitialized constant Rails::Server - i don't suppose you know the Rails 2 equivalent?Hackler
that's just the mode, eg "development"Hackler
"3000" doesn't appear anywhere in ENV.inspect either.Hackler
Max, can you check if this link can help? that defaults object, or RailsConfigurator class? I m just failing setting rails env. Which ruby version are you on?Nicolis
Ruby 1.8.6 (i know, i know! We're trying to upgrade but it's tough)Hackler
I can access Mongrel::Rails::RailsConfigurator in my script. I can't figure out how to get at the config or port number though.Hackler
Ok i finally got the environment. I know this won't be the best way, but, maybe you can just try using ARGV[] array?Nicolis
I'm just getting an empty array for ARGV, when i do puts ARGV.inspect in my script.Hackler
Thanks for your help btw, but i can just stick with the solution in my answer if this is turning into a timesink :)Hackler
np :) yesterday i was happy feeling like returned back to good old days, but now looking again at the rails 2 code.. this is just something totally different then what we are using now.. :) good luck on your upgrade process :)Nicolis
H
1

Ok, i'm answering my own question as i just figured it out after setting a bounty!

I can get the pid of the currently running process with Process.pid. Then i can do ps afx | grep mongrel which gives me a result like this

 pid                                                                                 port
  |                                                                                    |
  V                                                                                    V
10761 pts/1    S      0:20  |   \_/usr/local/bin/ruby /path/to/mongrel_rails start -p 3000
10762 pts/1    S      0:18  |   \_/usr/local/bin/ruby /path/to/mongrel_rails start -p 3001
10763 pts/1    S+     0:23  |   \_/usr/local/bin/ruby /path/to/mongrel_rails start -p 3002

which i can then grep for the pid, and read the port number out of the matching line, and see if it's 3000.

So, my code is

if `ps afx | grep mongrel_rails`.split("\n").detect{|line| line =~ /^#{Process.pid}.+\-p\s3000/}
  #this is a mongrel running on port 3000 - do the extra stuff
  ....
end

BTW, if someone can tell me how to directly get the port of the running mongrel, without going via ps afx and Process.pid i'll still give you the bounty :)

Hackler answered 20/5, 2015 at 14:48 Comment(0)
S
1

Does this work in 2.2.2?

class SomeController < ApplicationController

  def index
        @port = request.port
  end
end
Stanza answered 24/5, 2015 at 20:41 Comment(2)
He is trying to get it inside an initializer. So this wont work.Nicolis
Ah. Sorry, I missed that. I'm new to rails and glossed over it.Stanza

© 2022 - 2024 — McMap. All rights reserved.