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

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

问题描述

所以我使用Capistrano的部署Rails应用程序到我的生产服务器(Apache +乘客),并在目前部署通常沿线有云:

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模式的一大重构)上执行 - 在这种情况下,什么是Capistrano的最佳做法?如果用户在部署时连接到我的应用程序会发生什么?我应该摆好用户发送到静态预留页在数据库正在更新?是否Capistrano的自动的处理呢?我需要code一个配方,以帮助吗?还是轨/乘客的内部机制意味着我不必在所有担心这种特殊情况下?

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

应用程序/视图/管理/ 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 文件重定向到的所有请求,如果它是present:

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>

要应用进入维护模式,运行上限部署:网址:禁用,并使其重新生活做上限部署:网络:启用

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天全站免登陆