问题描述
好,所以我想在Rails中创建一个动作以重新启动自身.我做了一些搜索,发现:
Ok, so I would like to create an action in Rails to restart itself. I did a little searching and found:
http://snippets.dzone.com/posts/show/5002
建议使用2条命令,一个停止并重新启动.以下是杀死人数:
Which suggests 2 commands, one to stop and another to restart. The following kills:
ps -a|grep "/usr/local/bin/ruby script/server"|grep -v "grep /usr"|cut -d " " -f1|xargs -n 1 kill -KILL $1
-HUP信号对我而言没有重新启动,因此我尝试破坏了上面的命令(已进行调整,因此该命令与我在Ubuntu下启动服务器的方式配合得很好):
The -HUP signal doesn't restart for me, so I tried to mangle the above command (adjusted so the command worked fine with how I was starting the server under Ubuntu):
ps -eaf|grep "ruby script/server"|grep -v grep|cut -d " " -f3|xargs -n 1 kill -KILL $1;script/server
这在我的环境中可以正常工作,因此我尝试设置一个动作来执行它:
This works fine in my environment, so I tried to set up an action to execute it:
def restart
fork { exec "ps -eaf|grep \"ruby script/server\"|grep -v grep|cut -d \" \" -f3|xargs -n 1 kill -KILL $1;script/server" }
redirect_to "/server_maintenance"
end
该操作可以使服务器正常运行,但实际上并没有启动服务器备份:
The action kills the server fine, but doesn't actually start the server back up:
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/tcphack.rb:12:in `initialize_without_backlog': Address already in use - bind(2) (Errno::EADDRINUSE)
from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/tcphack.rb:12:in `initialize'
from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:93:in `new'
from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:93:in `initialize'
from /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb:10:in `new'
from /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb:10:in `run'
from /usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:111
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/server:3
我不太了解为什么Mongrel似乎刚刚退出时地址已经被使用.
I'm not quite understanding why the address is already in use when Mongrel seems to have just exited.
我发现了这个问题:
如何重新启动Rails在Mongrel下,无需停止并启动Mongrel
但是这些信号不会在我的环境中引起重启,而只是终止了该过程.
but the signals don't cause the restart in my environment, they just end up killing the process.
任何人都对可行的方法有任何想法?有关我的环境的一些说明:我从新版本的RubyGems和Mongrel安装了Rails.我使用脚本/服务器启动服务器,当然使用Mongrel.我在Ubuntu Hardy Heron上.
Anyone have any ideas on what may work? For some notes on my environment: I installed Rails from a new version of RubyGems, and Mongrel. I use script/server to start the server, which of course uses Mongrel. I'm on Ubuntu Hardy Heron.
推荐答案
好,我找到了解决方法...我将启动rails的方式更改为:
Ok I found a fix... I changed how I start rails to:
mongrel_rails start -d
,现在将执行以下操作:
and now the following action will do it:
def restart
fork { exec "mongrel_rails restart" }
redirect_to "/server_maintenance"
end
作为警告,redirect_to将导致失败的加载,因为服务器将关闭.但是暂停后重新加载将显示重新启动成功.可以通过更改重新启动以使用AJAX完成,然后重新加载javascript来解决此问题,但是我将其作为练习留给读者.
As a caveat, the redirect_to will cause a failed load because the server will be down... however a reload after a pause will show that the restart was successful. This could be fixed by changing the restart to be done with AJAX, followed by a javascript reload... but I will leave that as an exercise to the reader.
这篇关于如何从Rails内部重新启动Rails?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!