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
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
Run Rake task programmatically with specified environment
I managed to find a solution to this. I believe the reason is that .invoke
will not always invoke the task, but it will first determine whether it is necessary. Given that rake db:create
is run on several occasions within the same task, .invoke
deems the subsequent invocations as unnecessary and therefore does not run them. For the desired behaviour, .execute
should be used instead.
# remove db:create from the list of rake tasks in order to override it
db_create = Rake.application.instance_variable_get('@tasks').delete('db:create')
namespace :db do
task :create do
if Rails.env == "development"
Rails.env = "secondary_development"
Rake::Task["db:create"].execute # execute rather than invoke
end
# Reset the Rails env to 'development', otherwise it remains as 'secondary_development', which is not what we want (or move this above the if)
Rails.env = "development"
db_create.execute
end
end
Related Topics
Split Ruby Regex Over Multiple Lines
Rails: How to Make Date Strftime Aware of the Default Locale
How to Remove Carriage Returns with Ruby
When Do We Use the "||=" Operator in Rails? What Is Its Significance
Ruby: Create a String from Bytes
Why Is Safe Navigation Better Than Using Try in Rails
Relative Path to Your Project Directory
Active Admin: Customize Only New Form
What's the Opposite of Chr() in Ruby
Attr_Accessor on Class Variables
Installing the Postgresql Gem with 'Gem Pq' Fails with Error: Failed to Build Gem Native Extension
Engine's Assets with Rails 3.1
Using Htaccess Password Protection on Rails
Capybara: Select an Option by Value Not Text
Tzinfo-Data Present But Not Seen
Error Installing Gems That Use Native Extensions on Ubuntu, Ruby 1.9.2 via Rvm