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.
在我的本地机器上,我用mongrel运行rails。我有一些在启动时运行的东西,通过config / initializers中的文件,它使用puts来告诉我它正在使用哪个数据库,用于发送电子邮件的内容以及其他一些信息。
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?
当我在端口3000,3001和3002上运行一组mongrel时,我只想在端口3000上为mongrel做这个报告。所以,我需要将它包装在if块中,该块测试当前运行的mongrel的哪个端口正在使用。任何人都可以告诉我如何在我的代码中得到这个?
3 个解决方案
#1
2
in an initializer,
在初始化程序中,
puts Rails::Server.new.options[:Port]
can report your port.
可以报告您的端口。
#2
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
我可以使用Process.pid获取当前正在运行的进程的pid。然后我可以做ps afx | grep mongrel给了我这样的结果
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.
然后,我可以grep为pid,并从匹配行中读取端口号,看看它是否为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 :)
顺便说一句,如果有人可以告诉我如何直接获取正在运行的杂种的端口,而不通过ps afx和Process.pid,我仍然会给你赏金:)
#3
1
Does this work in 2.2.2?
这在2.2.2中有效吗?
class SomeController < ApplicationController
def index
@port = request.port
end
end
#1
2
in an initializer,
在初始化程序中,
puts Rails::Server.new.options[:Port]
can report your port.
可以报告您的端口。
#2
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
我可以使用Process.pid获取当前正在运行的进程的pid。然后我可以做ps afx | grep mongrel给了我这样的结果
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.
然后,我可以grep为pid,并从匹配行中读取端口号,看看它是否为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 :)
顺便说一句,如果有人可以告诉我如何直接获取正在运行的杂种的端口,而不通过ps afx和Process.pid,我仍然会给你赏金:)
#3
1
Does this work in 2.2.2?
这在2.2.2中有效吗?
class SomeController < ApplicationController
def index
@port = request.port
end
end