如何一次以编程方式运行多个Rake任务?

时间:2022-08-20 19:17:39

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

2 个解决方案

#1


14  

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

#2


2  

Make a rake task for it :P

为它做一个rake任务: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

#1


14  

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

#2


2  

Make a rake task for it :P

为它做一个rake任务: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