Capistrano 部署 rails 应用程序——如何处理长时间的迁移? [英] Capistrano to deploy rails application - how to handle long migrations?

查看:27
本文介绍了Capistrano 部署 rails 应用程序——如何处理长时间的迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用 Capistrano 将 Rails 应用程序部署到我的生产服务器(apache+passenger),目前部署通常是这样的:

So I am using Capistrano to deploy a rails application to my production server (apache+passenger) and at the moment deployment usually goes along the lines:

$cap deploy
$cap deploy:migrations

这让我想知道,假设我的 db:migrations 在生产服务器上执行需要很长时间(数据库架构的一次重大重构)——在这种情况下,Capistrano 的最佳实践是什么?如果用户在部署时连接到我的应用程序,会发生什么情况?我应该在更新数据库时优雅地将用户发送到静态占位符页面吗?Capistrano 会自动处理吗?我需要编写一个食谱来帮助解决这个问题吗?或者rails/passenger的内部机制是否意味着我完全不必担心这种特殊情况?

It got me wondering, let's say my db:migrations took a long time to execute on the production server (a big refactor of the db schema) - in this case what is best practice with Capistrano? What happens if users are connected to my application at the time of deployment? Should I gracefully send users to a static placeholder page while the database is being updated? Does Capistrano handle this automagically? Do I need to code up a recipe to help with this? Or does the internal mechanisms of rails / passenger mean that I don't have to worry at all about this particular case?

谢谢.

推荐答案

如果应用程序暂时不可用,您应该建立一个维护页面.我使用这个 Capistrano 任务:

You should put up a maintenance page if the application is not going to be available for a while. I use this Capistrano task:

namespace :deploy do
  namespace :web do
    desc <<-DESC
      Present a maintenance page to visitors. Disables your application's web \
      interface by writing a "maintenance.html" file to each web server. The \
      servers must be configured to detect the presence of this file, and if \
      it is present, always display it instead of performing the request.

      By default, the maintenance page will just say the site is down for \
      "maintenance", and will be back "shortly", but you can customize the \
      page by specifying the REASON and UNTIL environment variables:

        $ cap deploy:web:disable \\
              REASON="a hardware upgrade" \\
              UNTIL="12pm Central Time"

      Further customization will require that you write your own task.
    DESC
    task :disable, :roles => :web do
      require 'erb'
      on_rollback { run "rm #{shared_path}/system/maintenance.html" }

      reason = ENV['REASON']
      deadline = ENV['UNTIL']      
      template = File.read('app/views/admin/maintenance.html.erb')
      page = ERB.new(template).result(binding)

      put page, "#{shared_path}/system/maintenance.html", :mode => 0644
    end
  end
end

app/views/admin/maintenance.html.erb 文件应包含:

<p>We’re currently offline for <%= reason ? reason : 'maintenance' %> as of <%= Time.now.utc.strftime('%H:%M %Z') %>.</p>
<p>Sorry for the inconvenience. We’ll be back <%= deadline ? "by #{deadline}" : 'shortly' %>.</p>

最后一步是使用一些指令配置 Apache 虚拟主机以查找 maintenance.html 文件并将所有请求重定向到它(如果存在):

The final step is to configure the Apache virtual host with some directives to look for the maintenance.html file and redirect all requests to it if it's present:

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Redirect all requests to the maintenance page if present
  RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png)$
  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /system/maintenance.html [L]
</IfModule>

要将应用程序置于维护模式,请运行 cap deploy:web:disable 并使其再次生效,请执行 cap deploy:web:enable.

To put the application into maintenance mode, run cap deploy:web:disable and to make it live again do cap deploy:web:enable.

这篇关于Capistrano 部署 rails 应用程序——如何处理长时间的迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆