Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run multiple Rake tasks programmatically at once?

Tags:

ruby

rake

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
like image 949
codefinger Avatar asked Jan 13 '12 21:01

codefinger


People also ask

How to invoke a Rake task?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.

When to use Rake tasks?

Rake enables you to define a set of tasks and the dependencies between them in a file, and then have the right thing happen when you run any given task. The combination of convenience and flexibility that Rake provides has made it the standard method of job automation for Ruby projects.

What is a Ruby Rake task?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.


2 Answers

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
like image 135
knut Avatar answered Oct 20 '22 06:10

knut


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
like image 31
iwasrobbed Avatar answered Oct 20 '22 06:10

iwasrobbed