更新生产代码的正确方法?

时间:2012-02-20 13:52:38

标签: ruby-on-rails

我担心必须停止服务器,使用控件版本更新代码,然后运行

$ rake assets:precompile
,等待,然后启动服务器。当我这样做时,在线的用户会因为网站损坏而烦恼。

有更好的方法吗?也许是一种进入“维护模式”的方式,以便用户知道发生了什么?

3 个答案:

答案 0 :(得分:2)

您永远不应该中断要部署的生产服务器。部署应该是无缝的。查看像capistrano这样的部署解决方案。

答案 1 :(得分:2)

Capistrano是最常用的部署解决方案之一。它允许您将站点设置为维护模式。我强烈建议使用它。

如果您正在运行多个应用程序实例,那么您可以一次更新一个应用程序,以防止停机,但某些数据库更改除外。

如果必须手动执行此操作(假设您的Web服务器设置为从公用文件夹提供静态文件),您可以在公共目录中创建index.html文件并再次将其删除完成更新后。

答案 2 :(得分:1)

使用独角兽和Capistrano,您可以优雅地重启您的应用程序。

set(:unicorn_config) {"#{current_path}/config/unicorn.rb"}
set(:unicorn_pid) {"#{current_path}/../../shared/pids/unicorn.pid"}

namespace :deploy do
  task :start, :roles => :app, :except => { :no_release => true } do 
    run "cd #{current_path} && RAILS_ENV=#{rails_env} bundle exec unicorn_rails -c #{unicorn_config} -E #{rails_env} -D"
  end

  task :stop, :roles => :app, :except => { :no_release => true } do 
    begin
      run "kill `cat #{unicorn_pid}`"
    rescue
      puts "Can't kill unicorn - probably not running"
    end  
  end

  task :graceful_stop, :roles => :app, :except => { :no_release => true } do
    run "kill -s QUIT `cat #{unicorn_pid}`"
  end

  task :reload, :roles => :app, :except => { :no_release => true } do
    run "kill -s USR2 `cat #{unicorn_pid}`"
  end

  task :restart, :roles => :app, :except => { :no_release => true } do
    run "if [ -f #{unicorn_pid} ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{deploy_to}/current && RAILS_ENV=#{rails_env} bundle exec unicorn_rails -c #{unicorn_config} -E #{rails_env} -D; fi"
  end

end