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?

wpp
  • 7,093
  • 4
  • 33
  • 65
Max Williams
  • 32,435
  • 31
  • 130
  • 197

3 Answers3

2

in an initializer,

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

can report your port.

Alper Karapınar
  • 2,694
  • 1
  • 25
  • 36
  • 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? – Max Williams May 20 '15 at 15:47
  • that's just the mode, eg "development" – Max Williams May 20 '15 at 16:05
  • "3000" doesn't appear anywhere in `ENV.inspect` either. – Max Williams May 20 '15 at 16:08
  • Max, can you check if this [link](http://apidock.com/rails/v2.2.1/Rails/MongrelServer/Start/run) can help? that `defaults` object, or `RailsConfigurator` class? I m just failing setting rails env. Which ruby version are you on? – Alper Karapınar May 20 '15 at 16:42
  • Ruby 1.8.6 (i know, i know! We're trying to upgrade but it's tough) – Max Williams May 21 '15 at 08:15
  • I can access `Mongrel::Rails::RailsConfigurator` in my script. I can't figure out how to get at the config or port number though. – Max Williams May 21 '15 at 09:02
  • Ok i finally got the environment. I know this won't be the best way, but, maybe you can just try using ARGV[] array? – Alper Karapınar May 21 '15 at 10:16
  • I'm just getting an empty array for ARGV, when i do `puts ARGV.inspect` in my script. – Max Williams May 21 '15 at 10:18
  • Thanks for your help btw, but i can just stick with the solution in my answer if this is turning into a timesink :) – Max Williams May 21 '15 at 10:19
  • 1
    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 :) – Alper Karapınar May 21 '15 at 10:25
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 :)

Max Williams
  • 32,435
  • 31
  • 130
  • 197
1

Does this work in 2.2.2?

class SomeController < ApplicationController

  def index
        @port = request.port
  end
end
Joseph Freivald
  • 396
  • 2
  • 17