Rails App Maintenence Without Hindering Visitors

后端 未结 1 1727
误落风尘
误落风尘 2021-02-04 22:09

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 d

1条回答
  •  情书的邮戳
    2021-02-04 22:43

    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:

    We’re currently offline for <%= reason ? reason : 'maintenance' %> as of <%= Time.now.utc.strftime('%H:%M %Z') %>.

    Sorry for the inconvenience. We’ll be back <%= deadline ? "by #{deadline}" : 'shortly' %>.

    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:

    
      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]
    
    

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

    0 讨论(0)
提交回复
热议问题