Created
August 23, 2009 03:28
-
-
Save bryanstearns/173147 to your computer and use it in GitHub Desktop.
Render maintenance pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In spite of the proscription at the end of Recipe 69, "Upload Custom Maintenance | |
# Pages" in Mike Clark's excellent Advanced Rails Recipes, I wanted to generate my | |
# maintenance page with the full awesome power of Rails (ok, well, I wanted to use | |
# a real template, to avoid having to maintain the look of the maintenance page | |
# separately). | |
# | |
# My solution was to render the page using this rake task, which goes in: | |
# lib/tasks/render_maintenance.rake | |
task :render_maintenance => :environment do | |
# Render the maintenance page into tmp/maintenance.html, | |
# where the Capistrano deploy:web:disable task (which calls this) | |
# can find it. (Note: this runs locally, on the machine of the | |
# person doing the deployment!) | |
view = ActionView::Base.new(Rails::Configuration.new.view_path) | |
view.class_eval do | |
include ApplicationHelper | |
end | |
page = view.render(:file => 'home/maintenance', | |
:layout => 'layouts/minimal', | |
:locals => { :reason => ENV['REASON'], | |
:deadline => ENV['UNTIL']}) | |
File.open("#{RAILS_ROOT}/tmp/maintenance.html", 'w') {|f| f.write(page)} | |
end | |
# Then, I override Capistrano's built-in deploy:web:disable with this in: | |
# config/deploy.rb | |
task "deploy:web:disable", :roles => :web do | |
# Put up a "Sorry, we're closed" page for now. | |
# (There's a built-in 'deploy:web:enable' that undoes this; we're | |
# overriding Capistrano's default recipe here.) | |
local_maintenance_file = "tmp/maintenance.html" | |
remote_maintenance_file = "#{shared_path}/system/maintenance.html" | |
on_rollback { run "rm #{remote_maintenance_file}" } | |
# Render the maintenance page to a local temp file | |
FileUtils.rm_f(local_maintenance_file) | |
`rake render_maintenance` | |
page = File.read(local_maintenance_file) | |
FileUtils.rm_f(local_maintenance_file) | |
abort "unable to generate maintenance page" \ | |
unless page.size > 900 | |
# Put it on the server where Apache will see it. | |
put page, remote_maintenance_file, :mode => 0644 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment