【问题标题】:Rails App Maintenence Without Hindering Visitors不妨碍访问者的 Rails 应用程序维护
【发布时间】:2011-03-22 15:47:33
【问题描述】:

在我更新后端时,如果访问者登陆我的网站,我并不希望看到 Phusion 乘客错误消息。

那么我该如何解决这个问题?我的部署过程从一开始就存在缺陷吗?还是我错过了什么?

这是我的部署过程,所以你得到了图片:

  • 提交新的更新到 git repo 并推送到远程
  • 上限部署
  • ssh [ip]
  • 耙宝石:安装
  • rake db:迁移
  • 黄瓜

cap deploy 和 db:migrate 或 gems:install 之间的时间是出现错误消息时或在较长时间的维护期间。

我在写这篇文章时突然想到一个想法:我可以将这些命令放入我的部署配方中吗?

但是如果维护需要 30 分钟或一个小时,这些命令将无法解决问题。如何在这段时间内向访问者提供维护启动页面?

提前致谢。

【问题讨论】:

    标签: ruby-on-rails deployment capistrano production-environment passenger


    【解决方案1】:

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

    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 文件并将所有请求重定向到它(如果存在):

    <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。

    【讨论】:

    • 哇,你的回答真的让我大吃一惊!太棒了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多