Rails应用程序维护,而不会阻碍访问者 [英] Rails App Maintenence Without Hindering Visitors

查看:117
本文介绍了Rails应用程序维护,而不会阻碍访问者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Phusion Passenger错误消息不是我希望我的访问者在更新后端时看到他们是否降落在我的网站上。



那么我该如何解决呢?我的部署过程从一开始就有缺陷吗?或者有什么我错过了吗?



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




  • 提交新的git repo更新并推送到远程

  • cap deploy

  • ssh [ip] li>
  • rake gems:install

  • rake db:migrate

  • cucumber



    • 在cap部署和db:migrate或gems:install之间的时间是出现错误消息或更长时间的维护时。



      我在写这篇文章时,一个想法让我感到头疼:我可以把这些命令放在我的部署配方中吗?



      但是如果维护是要花30分钟或者一个小时,这些命令就不会解决问题。



      提前感谢您的访问者维护启动页面。

      解决方案

      如果应用程序不可用一段时间,您应该安装维护页面。我使用这个Capistrano任务:

       命名空间:deploy do 
      命名空间:web do
      desc<< ; -DESC
      向访问者呈现维护页面。通过向每个Web服务器写一个maintenance.html文件来禁用应用程序的Web \
      接口。必须配置\
      服务器来检测此文件的存在,如果\
      存在,则始终显示它,而不是执行该请求。

      默认情况下,维护页面只会表示网站已关闭\
      维护,并将短返回,但您可以自定义\
      页面指定REASON和UNTIL环境变量:

      $ cap deploy:web:disable \\
      REASON =硬件升级\\
      UNTIL =中午12点

      进一步的自定义将要求您编写自己的任务。
      DESC
      任务:disable,:roles => :web do
      require'erb'
      on_rollback {runrm#{shared_pa​​th} /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页面,#{shared_pa​​th} /system/maintenance.html,:mode => 0644
      end
      end
      end

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

        p>我们目前正在离线,为<%=原因?原因:'维护'%>从<%= Time.now.utc.strftime('%H:%M%Z')%>。< / p> 
      < p>抱歉给您带来不便。我们会回到<%=截止日期? by#{deadline}:'shortly'%>。。< / p>

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

       < IfModule mod_rewrite.c> 
      RewriteEngine On

      #将所有请求重定向到维护页面(如果存在)
      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


      A Phusion Passenger error message isn't what I want my visitors to see if they landed on my site while I'm updating the back end.

      So how do I get around this? Is my deployment process flawed from the start? or is there something I'm missing out?

      Here's my process of deployment, so you get the picture:

      • commit new updates to a git repo and push to remote
      • cap deploy
      • ssh [ip]
      • rake gems:install
      • rake db:migrate
      • cucumber

      The time in between the cap deploy and the db:migrate or gems:install is when there is the error message or during longer maintenance.

      An idea struck me around the head while I have been writing this: can I put these commands into my deployment recipe?

      But what if maintenance is going to take an 30 mins or an hour, those commands won't solve the problem. how can I serve up a maintenance splash page to the visitor for this period of time?

      thanks in advance.

      解决方案

      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
      

      The app/views/admin/maintenance.html.erb file should contain:

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

      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.

      这篇关于Rails应用程序维护,而不会阻碍访问者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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