Command for running a rails task - ruby-on-rails

What's the command if I wanted to run a task within test.rb under the following directory:
lib/tasks/lib/data/test.rb

To add a rake task you need to create a .rake file instead of .rb file in the lib/tasks directory as
lib/tasks/sample_task.rake
or in your case
lib/tasks/lib/data/test.rake
rake tasks are defined using namespace
Example test.rake file
# Namespace declaration
namespace :sample do
# Task Description
desc "Sample task"
# Here sample_task is the name of the task
task sample_task: :environment do
# Your task
5.times do |t|
puts "Hello world"
end
end
end
Now run your task with rake sample:sample_task
where sample is namespace declaration and sample_task is the name of the task
If you are using rails 2 then you can use runner to run your tasks like
script/runner sample_task

Related

Invoking rake task for specific environment

Hello I'm writing a deploy script(rake task) for my mini project. And I have this part when I invoke the db seed :
Rake::Task['db:seed'].invoke
And also compiling assets :
Rake::Task['assets:precompile'].invoke
So I was wondering is there a way to invoke these tasks in production environment like you would do from a console like this :
RAILS_ENV=production rake db:seed
In Rails, you can do as :
[arup#app (master)]$ rails g task my_namespace my_task1
create lib/tasks/my_namespace.rake
[arup#app (master)]$ cat lib/tasks/my_namespace.rake
namespace :my_namespace do
desc "TODO"
task my_task1: :environment do
end
end
[arup#app (master)]$
Now see the Rakefile is ready for you.
Just open the Rakefile you just created, and define your tasks.
namespace :my_namespace do
task my_task1: :environment do
Rake::Task['db:seed'].invoke
Rake::Task['assets:precompile'].invoke
end
end
Hints is 2.10 Custom Rake Tasks.

Using Rake with Rufus

I'm trying to user rake and rufus, both of which I am new to. I want to have Rufus call my rake task but I am getting the following error. Don't know how to build task 'inbox:process_inbox'
lib/tasks/inbox_tasks.rb
namespace :inbox do
task :process_inbox do
logger = Logger.new(Rails.root.to_s + "/log/scheduler.log")
logger.info "Rufus Here!"
end
end
rufus_scheduler.rb
require 'rufus-scheduler'
require 'rake'
scheduler = Rufus::Scheduler.new
scheduler.every '10s', :first_at => Time.now + 3 do
Rake::Task["inbox:process_inbox"]
end
As #jmettraux (the creator of rufus-scheduler!) has already answered, the problem is that the rake task is defined in a .rb file instead of .rake file.
Adding some more details to help in the future.
While creating a new rake task, you could get the rails generator to automatically create the file with appropriate structure.
Example: Running
> rails g task inbox process_inbox
create lib/tasks/inbox.rake
will create a file named lib/tasks/inbox.rake with content:
namespace :inbox do
desc "TODO"
task process_inbox: :environment do
end
end
Having a DESC in the task definition is important; that allows for verifying that the rake task is defined and available, by running either rake -T inbox or rake -T | grep inbox
> rake -T inbox
rake inbox:process_inbox # TODO
Could this one help?
How to build task 'db:populate' (renaming inbox_tasks.rb to inbox_tasks.rake)
(did a simple https://www.google.com/?#q=rails+don%27t+know+how+to+build+task ...)

Rake doesn't find '*.rake' file in 'lib/tasks' directory

I have a foo.rake file in lib/tasks directory of a Rails project.
namespace :foo do
desc 'rake task example'
def bar
p "foo bar"
end
end
But the rake command can't find the task, the following command outputs nothing.
bundle exec rake -T -A | grep foo
How can I run a rake task from command line?
Rake tasks are defined like so:
namespace :foo do
desc 'rake task example'
task :bar do
# Your code here
end
end
Notice task :bar do instead of a usual method definition style def bar.
Add the line
Rake.add_rakelib('lib/tasks')
to your Rakefile.

Add sub-task rake

I'm using rails 4 and I need to add a subtask for seeding our database using demo data (for product demo's). I want to make it a subtask called rake db:seed:demo, how can I do this?
I tried to subtask using this code, but I got an error from rake saying task not found.
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
API::Application.load_tasks
task :demo => :seed do
end
task :seed => :db
Use the namespace directive:
namespace :db do
namespace :seed do
task :demo do
end
end
end

Adding a custom seed file

I want to populate a new feature with dummy data, but don't want to use the db/seeds.rb file as it already has seeds other data irrelevant for this feature.
To run the default seeds.rb file, you run the command rake db:seed.
If I create a file in the db directory called seeds_feature_x.rb, what would the rake command look like to run (only) that file?
Start by creating a separate directory to hold your custom seeds – this example uses db/seeds. Then, create a custom task by adding a rakefile to your lib/tasks directory:
# lib/tasks/custom_seed.rake
namespace :db do
namespace :seed do
Dir[Rails.root.join('db', 'seeds', '*.rb')].each do |filename|
task_name = File.basename(filename, '.rb')
desc "Seed " + task_name + ", based on the file with the same name in `db/seeds/*.rb`"
task task_name.to_sym => :environment do
load(filename) if File.exist?(filename)
end
end
end
end
This rakefile accepts the name of a seed file in the db/seeds directory (excluding the .rb extension), then runs it as it would run seeds.rb. You can execute the rake task by issuing the following from the command line:
rake db:seed:file_name # Name of the file EXCLUDING the .rb extension
Update: Now it should also list the seed tasks when running rake --tasks or rake -T.
I tried out zeantsoi's answer but it didn't give me what I wanted, it did all files in a directory. Hacked away at it and got this.
namespace :db do
namespace :seed do
task :single => :environment do
filename = Dir[File.join(Rails.root, 'db', 'seeds', "#{ENV['SEED']}.seeds.rb")][0]
puts "Seeding #{filename}..."
load(filename) if File.exist?(filename)
end
end
end
And to use this do the following:
rake db:seed:single SEED=<seed_name_without_.seeds.rb>
This will look in the Rails.root/db/seeds folder for a file name without the .seeds.rb (it adds those for you).
Working example:
rake db:seed:single SEED=my_custom_seed
The above would seed the Rails.root/db/seeds/my_custom_seed.seeds.rb file
Too complicated!
I just wanted a simple task to execute every file under db/seeds directory without passing in any file names.
# lib/tasks/seed.rake
desc "Run all files in db/seeds directory"
namespace :db do
task seed: :environment do
Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].each do |filename|
puts "seeding - #{filename}. for reals, yo!"
load(filename)
end
end
end

Resources