Created
June 9, 2014 08:22
-
-
Save eimermusic/43f80f435378eda852f4 to your computer and use it in GitHub Desktop.
Multi-tenant switching rake task
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
# runs other rake tasks with domain scoping applied. | |
# Like so: | |
# bundle exec rake domain:one TASK=namespace:task DOMAIN=customer1.example.com | |
# bundle exec rake domain:all TASK=namespace:task | |
namespace :domain do | |
desc 'Run a rake task ENV[TASK] on a single domain ENV[DOMAIN]' | |
task :one => :environment do | |
raise Exception.new, "You must provide ENV['TASK'] and ENV['DOMAIN']" if ENV['TASK'].nil? || ENV['DOMAIN'].nil? | |
set_domain(ENV['DOMAIN']) | |
puts "Invoking task #{ENV['TASK']} in #{ENV['DOMAIN']}" | |
Rake::Task[ ENV['TASK'] ].invoke | |
end | |
desc 'Run a rake task ENV[TASK] on all domains' | |
task :all => :environment do | |
raise Exception.new, "You must provide ENV['TASK']" if ENV['TASK'].nil? | |
Domain.all.each do |domain| | |
puts "Invoking task #{ENV['TASK']} in #{domain.name}" | |
set_domain(domain.name) | |
puts "Domain is now: #{domain.name}" | |
Rake::Task[ ENV['TASK'] ].reenable | |
Rake::Task[ ENV['TASK'] ].invoke | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment