Search code examples
ruby-on-railsrake

Run all rake tasks inside a shared namespace in Rails?


I'm trying to run a list of rake task within a shared :namespace following this post: How do I run all rake tasks?.

But its not working.

Recommendation per post

desc "perform all scraping"
task :scrape do
  Rake::Task[:scrape_nytimes].execute 
  Rake::Task[:scrape_guardian].execute 
end

The difference in my case is that all rake tasks are in a namespace.

Rake tasks

namespace :get_ready do
  task check_weather: :environment do
    p 1
  end
  task make_lunch: :environment do
    p 2
  end
  task start_car: :environment do
    p 3
  end
end

Attempting to create a rake task that runs all rake tasks as below.

desc "Run all tasks"
task run_all: :environment do
  Rake::Task[:check_weather].execute 
  Rake::Task[:make_lunch].execute    
  Rake::Task[:start_car].execute       
end 

And then running with rake run_all or rake get_ready. The below variations I tried also didn't work.

  • Rake::Task[run_all:check_weather].execute
  • Rake::Task[:run_all, :check_weather].execute

Does anyone have experience running a batch of rake tasks in a shared namespace and knows how to do this?


Solution

  • It should be:

    desc "Run all tasks"
    task run_all: :environment do
      Rake::Task['get_ready:check_weather'].execute 
      Rake::Task['get_ready:make_lunch'].execute    
      Rake::Task['get_ready:start_car'].execute       
    end
    

    The namespace is get_ready and check_weather, make_lunch, start_car are task in that namespace.

    More elegant solution is:

    desc "Run all tasks"
    task run_all_elegantly: [:environment, 'get_ready:check_weather', 'get_ready:make_lunch', 'get_ready:start_car']