Created
June 3, 2012 03:08
-
-
Save bradfordcp/2861674 to your computer and use it in GitHub Desktop.
Dynamic Site Caching Using Guard
This file contains 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
source 'https://rubygems.org' | |
# Note this was running on a Fedora 16 box | |
gem 'ruby-inotify' | |
gem 'guard' | |
gem 'activerecord' | |
gem 'mysql2' |
This file contains 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
# Guardfile in charge of static assets, pulling dynamic pages, and regenerated db driven pages | |
# Invoked using 'bundle exec guard start -w /data/some_dynamic_site.example.com/ -n false' | |
# After Guard has started up you may FORCE a full publish by pressing 'enter' | |
# More info at https://github.com/guard/guard#readme | |
require 'pp' | |
require 'guard' | |
require 'guard/guard' | |
require 'active_record' | |
DEV_ROOT = '/data/some_dynamic_site.example.com' | |
STATIC_ROOT = '/data/static.some_dynamic_site.example.com' | |
LINK_MAP = {} | |
conn_info = { | |
:adapter => "mysql2", | |
:host => "127.0.0.1", | |
:username => "root", | |
:database => "my_Database" | |
} | |
class Restaurant < ActiveRecord::Base | |
self.table_name = "vendors" | |
def some_id | |
return self.id if self.booth_id == 0 | |
return self.booth_id | |
end | |
end | |
Restaurant.establish_connection conn_info | |
static_dynamic_pages = { | |
"home.php" => { | |
:target => "index.html", | |
:path => "", | |
:regex => %r{^home\.php$}, | |
}, | |
"json/restaurants.php" => { | |
:target => "json/restaurants.json", | |
:path => "json/restaurants.json", | |
:regex => %r{^json/restaurants\.php$}, | |
}, | |
"list.php" => { | |
:target => "search/index.html", | |
:path => "search/", | |
:regex => %r{^list\.php$} | |
}, | |
"map.php" => { | |
:target => "map/index.html", | |
:path => "map/", | |
:regex => %r{^map\.php$}, | |
}, | |
"display.php" => { | |
:target => "display/index.html", | |
:path => "display/", | |
:regex => %r{^display\.php$}, | |
}, | |
"events.php" => { | |
:target => "events/index.html", | |
:path => "events/", | |
:regex => %r{^events\.php$} | |
}, | |
"survey.php" => { | |
:target => "survey/index.html", | |
:path => "survey/", | |
:regex => %r{^survey\.php$} | |
}, | |
"sponsors.php" => { | |
:target => "sponsors/index.html", | |
:path => "sponsors/", | |
:regex => %r{^sponsors\.php$} | |
} | |
} | |
# Add static dynamic pages to the link map | |
static_dynamic_pages.keys.each do |key| | |
LINK_MAP[key] = static_dynamic_pages[key][:path] | |
end | |
# Add all of the info links to the link map | |
Restaurant.order("booth_id DESC").all.each do |restaurant| | |
LINK_MAP["info.php?boothID=#{restaurant.some_id}"] = "booth/#{restaurant.some_id}/" | |
end | |
dynamic_page_trigger_regex = %r{^rebuild_info_pages\.txt$} | |
module ::Guard | |
class StaticSyncr < ::Guard::Guard | |
# Initialize a Guard. | |
# @param [Array<Guard::Watcher>] watchers the Guard file watchers | |
# @param [Hash] options the custom Guard options | |
def initialize(watchers = [], options = {}) | |
super | |
end | |
# Called when just `enter` is pressed | |
# This method should be principally used for long action like running all specs/tests/... | |
# @raise [:task_has_failed] when run_all has failed | |
def run_all | |
affected_paths = [] | |
Dir.glob(File.join(DEV_ROOT, '**', '*')) do |path| | |
watchers.each do |watcher| | |
path = path.gsub(DEV_ROOT + "/", "") | |
if watcher.pattern.match path | |
affected_paths << path | |
end | |
end | |
end | |
run_on_change affected_paths | |
end | |
# Called on file(s) modifications that the Guard watches. | |
# @param [Array<String>] paths the changes files or paths | |
# @raise [:task_has_failed] when run_on_change has failed | |
def run_on_change(paths) | |
paths.each do |path| | |
puts "Copying #{path} to static site" | |
parent_dir = File.dirname path | |
if (not File.exists? File.join(STATIC_ROOT, parent_dir)) && (not File.directory? File.join(STATIC_ROOT, parent_dir)) | |
`mkdir -p "#{File.join STATIC_ROOT, parent_dir}"` | |
end | |
`cp "#{File.join DEV_ROOT, path}" "#{File.join STATIC_ROOT, path}"` | |
end | |
end | |
# Called on file(s) deletions that the Guard watches. | |
# @param [Array<String>] paths the deleted files or paths | |
# @raise [:task_has_failed] when run_on_change has failed | |
def run_on_deletion(paths) | |
paths.each do |path| | |
puts "Removing #{path} from staic site" | |
`rm "#{File.join STATIC_ROOT, path}"` | |
end | |
end | |
end | |
class PullerSyncr < ::Guard::Guard | |
# Initialize a Guard. | |
# @param [Array<Guard::Watcher>] watchers the Guard file watchers | |
# @param [Hash] options the custom Guard options | |
def initialize(watchers = [], options = {}) | |
@map = options[:map] | |
super | |
end | |
# Called when just `enter` is pressed | |
# This method should be principally used for long action like running all specs/tests/... | |
# @raise [:task_has_failed] when run_all has failed | |
def run_all | |
affected_paths = [] | |
Dir.glob(File.join(DEV_ROOT, '**', '*')) do |path| | |
watchers.each do |watcher| | |
path = path.gsub(DEV_ROOT + "/", "") | |
if watcher.pattern.match path | |
affected_paths << path | |
end | |
end | |
end | |
run_on_change affected_paths | |
end | |
# Called on file(s) modifications that the Guard watches. | |
# @param [Array<String>] paths the changes files or paths | |
# @raise [:task_has_failed] when run_on_change has failed | |
def run_on_change(paths) | |
paths.each do |path| | |
puts "Pulling #{path} to static site" | |
target_path = @map[path][:target] | |
parent_dir = File.dirname target_path | |
if (not File.exists? File.join(STATIC_ROOT, parent_dir)) && (not File.directory? File.join(STATIC_ROOT, parent_dir)) | |
`mkdir -p "#{File.join STATIC_ROOT, parent_dir}"` | |
end | |
# Pull the page | |
`curl "http://dev.broadappetit.inmunited.com/#{path}" > "#{File.join STATIC_ROOT, target_path}"` | |
# Read in the page and modify links | |
puts "Translating Links" | |
contents = File.read File.join(STATIC_ROOT, target_path) | |
(LINK_MAP.keys.sort {|x,y| y <=> x }).each do |key| | |
contents.gsub! key, LINK_MAP[key] | |
end | |
File.open File.join(STATIC_ROOT, target_path), 'w' do |io| | |
io.write contents | |
end | |
end | |
end | |
end | |
class InfoSyncr < ::Guard::Guard | |
# Initialize a Guard. | |
# @param [Array<Guard::Watcher>] watchers the Guard file watchers | |
# @param [Hash] options the custom Guard options | |
def initialize(watchers = [], options = {}) | |
super | |
@restaurant = options[:restaurant] | |
@trigger = options[:trigger] | |
end | |
# Called when just `enter` is pressed | |
# This method should be principally used for long action like running all specs/tests/... | |
# @raise [:task_has_failed] when run_all has failed | |
def run_all | |
run_on_change [@trigger] | |
end | |
# Called on file(s) modifications that the Guard watches. | |
# @param [Array<String>] paths the changes files or paths | |
# @raise [:task_has_failed] when run_on_change has failed | |
def run_on_change(paths) | |
@restaurant.all.each do |restaurant| | |
target_path = "booth/#{restaurant.some_id}/index.html" | |
parent_dir = File.dirname target_path | |
if (not File.exists? File.join(STATIC_ROOT, parent_dir)) && (not File.directory? File.join(STATIC_ROOT, parent_dir)) | |
`mkdir -p "#{File.join STATIC_ROOT, parent_dir}"` | |
end | |
# Pull the page | |
`curl "http://dev.broadappetit.inmunited.com/info.php?boothID=#{restaurant.some_id}" > "#{File.join STATIC_ROOT, target_path}"` | |
# Read in the page and modify links | |
puts "Translating Links" | |
contents = File.read File.join(STATIC_ROOT, target_path) | |
(LINK_MAP.keys.sort {|x,y| y <=> x }).each do |key| | |
contents.gsub! key, LINK_MAP[key] | |
end | |
File.open File.join(STATIC_ROOT, target_path), 'w' do |io| | |
io.write contents | |
end | |
end | |
end | |
# Called on file(s) deletions that the Guard watches. | |
# @param [Array<String>] paths the deleted files or paths | |
# @raise [:task_has_failed] when run_on_change has failed | |
def run_on_deletion(paths) | |
end | |
end | |
end | |
guard :static_syncr do | |
watch(%r{^.+\.css$}) | |
watch(%r{^img/.+\.(png|gif|jpg|jpeg)$}) | |
watch(%r{^js/.+\.js$}) | |
watch(%r{^upload/.+\.(png|gif|jpg|jpeg)$}) | |
watch(%r{^sponsors/.+\.(png|gif|jpg|jpeg)$}) | |
end | |
guard :puller_syncr, :map => static_dynamic_pages do | |
static_dynamic_pages.keys.each do |key| | |
watch(static_dynamic_pages[key][:regex]) | |
end | |
end | |
guard :info_syncr, :trigger => dynamic_page_trigger_regex, :restaurant => Restaurant do | |
watch(dynamic_page_trigger_regex) | |
end |
This file contains 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
# Guardfile in charge of rsyncing out to the prod servers | |
# Invoked using 'bundle exec guard start -w /data/static.some_dynamic_site.example.com/ -n false -G PusherGuardfile' | |
# After Guard has started up you may FORCE a full publish by pressing 'enter' | |
# More info at https://github.com/guard/guard#readme | |
require 'pp' | |
require 'guard' | |
require 'guard/guard' | |
require 'active_record' | |
STATIC_ROOT = '/data/static.some_dynamic_site.example.com' | |
SERVERS = ["ip_or_hostname_1", "ip_or_hostname_2"] | |
module ::Guard | |
class PshrSyncr < ::Guard::Guard | |
# Initialize a Guard. | |
# @param [Array<Guard::Watcher>] watchers the Guard file watchers | |
# @param [Hash] options the custom Guard options | |
def initialize(watchers = [], options = {}) | |
super | |
end | |
# Called when just `enter` is pressed | |
# This method should be principally used for long action like running all specs/tests/... | |
# @raise [:task_has_failed] when run_all has failed | |
def run_all | |
# Rsync the entire folder | |
puts "rsync'n the entire site" | |
SERVERS.each do |server| | |
`rsync -rv --delete #{STATIC_ROOT} bradfordcp@#{server}:/data/example.com/` | |
end | |
end | |
# Called on file(s) modifications that the Guard watches. | |
# @param [Array<String>] paths the changes files or paths | |
# @raise [:task_has_failed] when run_on_change has failed | |
def run_on_change(paths) | |
puts "rsync'n the entire site" | |
SERVERS.each do |server| | |
`rsync -rv --delete #{STATIC_ROOT} bradfordcp@#{server}:/data/example.com/` | |
end | |
end | |
# Called on file(s) deletions that the Guard watches. | |
# @param [Array<String>] paths the deleted files or paths | |
# @raise [:task_has_failed] when run_on_change has failed | |
def run_on_deletion(paths) | |
puts "rsync'n the entire site" | |
SERVERS.each do |server| | |
`rsync -rv --delete #{STATIC_ROOT} bradfordcp@#{server}:/data/example.com/` | |
end | |
end | |
end | |
end | |
guard :pshr_syncr do | |
watch(%r{.+\.js$}) | |
watch(%r{.+\.html}) | |
watch(%r{.+\.css$}) | |
watch(%r{.+\.(png|gif|jpg|jpeg)$}) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment