简体   繁体   中英

How do I run multiple Rake tasks programmatically at once?

At the command line I can run multiple tasks like this

rake environment task1 task2 task3

How can I do this programmatically? I know that I can run one task like this

Rake::Task['task1'].invoke

You can call two tasks:

require 'rake'

task :task1 do |t|
  p t
end
task :task2 do |t|
  p t
end


Rake::Task["task1"].invoke
Rake::Task["task2"].invoke

I would prefer a new tast with prerequisites:

require 'rake'

task :task1 do |t|
  p t
end
task :task2 do |t|
  p t
end
desc "Common task"
task :all => [ :task1, :task2  ]
Rake::Task["all"].invoke

If I misunderstood your question and you want to execute the same task twice: You can reenable tasks:

require 'rake'

task :task1 do |t|
  p t
end
Rake::Task["task1"].invoke
Rake::Task["task1"].reenable
Rake::Task["task1"].invoke

Make a rake task for it :P

# in /lib/tasks/some_file.rake
namespace :myjobs do 
  desc "Doing work, son" 
  task :do_work => :environment do
    Rake::Task['resque:work'].invoke 
    start_some_other_task
  end

  def start_some_other_task
    # custom code here
  end
end

Then just call it:

rake myjobs:do_work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM