How to Run Rake Tasks from Within Rake Tasks

Rails how to run rake task

You can run Rake tasks from your shell by running:

rake task_name

To run from from Ruby (e.g., in the Rails console or another Rake task):

Rake::Task['task_name'].invoke

To run multiple tasks in the same namespace with a single task, create the following new task in your namespace:

task :runall => [:iqmedier, :euroads, :mikkelsen, :orville] do
# This will run after all those tasks have run
end

Running gems rake tasks from application rake tasks

so there are several things to be taken into consideration here.

for the record, this is how you call a task with arguments

Rake::Task["taskname"].execute(args)

in your case, i did not realize first, that it actually uses environment variables instead of task arguments that are read as ENV['CLASS'].

that would answer your question, so you can either set it ENV['CLASS'] = 'ClassName' or pass it along to your call to the rake task rake geolocal CLASS=ProposedAccomodation.

which brings me to a following question: why are you not just calling the original rake task, there is nothing you add to it.

How do I run rake tasks within my rails application

If you wish this rake code to run during the request cycle then you should avoid running rake via system or any of the exec family (including backticks) as this will start a new ruby interpreter and reload the rails environment each time it is called.

Instead you can call the Rake commands directly as follows :-

require 'rake'

class SomeModel <ActiveRecord::Base

def self.run_rake(task_name)
load File.join(RAILS_ROOT, 'lib', 'tasks', 'custom_task.rake')
Rake::Task[task_name].invoke
end
end

Note: in Rails 4+, you'll use Rails.root instead of RAILS_ROOT.

And then just use SomeModel.run_rake("ts:reindex")

The key parts here are to require rake and make sure you load the file containing the task definitions.

Most information obtained from http://railsblogger.blogspot.com/2009/03/in-queue_15.html

How do I run Rake tasks within a Ruby script?

from timocracy.com:

require 'rake'

def capture_stdout
s = StringIO.new
oldstdout = $stdout
$stdout = s
yield
s.string
ensure
$stdout = oldstdout
end

Rake.application.rake_require 'metric_fetcher', ['../../lib/tasks']
results = capture_stdout {Rake.application['metric_fetcher'].invoke}

Call methods from a task in Rake files

Yes it is absolutely possible. Just define those methods after the namespace ;)

namespace :populate do
desc "ETC"
task :db => :environment do

Report.where(link: "").each do |word|
url = get_search_url(word.name)
doc = Nokogiri::HTML(open(url))
word.update_columns(link: link)
end
end

end

def get_search_url(keyword)
return "URL/keyword"
end

Combining many rake tasks into one rake task

You want invoke not execute. A little excerpt from my own code showing how to pass variables:

namespace :clients do

task :create, [:client] => ["clients:creation:checks"] do |t, args|
Rake::Task["clients:creation:git"].invoke(client, password)
Rake::Task["server:virtualhost:create"].invoke(client)
Rake::Task["server:virtualhost:enable"].invoke(client)
Rake::Task["server:reload"].invoke
Rake::Task["db:roles:create"].invoke(client, password)
Rake::Task["db:create"].invoke(client, client)
Rake::Task["db:migrate"].invoke(client)
end

end

Alternatively, you can make the task depend upon another task as I have done above with :create depending upon clients:creation:checks.

Just to clarify, a namespace is for grouping tasks, so you must actually define the tasks within the namespace as I have above. You can't simply call tasks from within a namespace.

So your code above should be:

desc 'This rebuilds development db'
task :rebuild_dev do
Rake::Task["db:drop"].invoke
Rake::Task["db:create"].invoke
Rake::Task["db:migrate"].invoke
Rake::Task["db:load"].invoke
end


Related Topics



Leave a reply



Submit