Rake doesn't find '*.rake' file in 'lib/tasks' directory - ruby-on-rails

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.

Related

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 ...)

Running rake task on remote app, use file from local machine

How can I pass the csv file or file stream or something in line of that to the rake task I'm running on the remote app via rake task arguments?
So I can get the contents of that file in the file and do something with it. It's not a big file.
Update
I tried with suggestion from Luc:
desc 'Test task'
namespace :app do
task :pipe_file => [:environment] do |t, args|
puts "START"
File.open('my_temp_file', 'w') do |f2|
while line = STDIN.gets
f2.puts line
end
end
puts "DONE"
end
end
So when I run :
cat tst.csv | bundle exec rake app:pipe_file
Nothing happens, blank line prints
You can pipe the content of your file to your rake task:
cat my_file | heroku run rake --no-tty my_task
Then inside your task you need to start by reading STDIN:
STDIN.binmode
tmp_file = Tempfile.new('temp_file_prefix', Rails.root.join('tmp'))
tmp_file.write(STDIN.read)
tmp_file.close
Process tmp_file here.
puts tmp_file.path
tmp_file.unlink
Hope it helps !

Command for running a rails task

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

How do I pass parameter to a rake task that is invoked using Rake::Task

Here is my rake task
task :lab => :enviroment do
Rake::Task["db:rollback"].invoke('STEP=5')
end
It is not doing what I want. What I want is
rake db:rollback STEP=5
I am using Rails 3.2.1 on ruby 1.9.2.
On the command line I want to execute
rake lab
The real case is much more complicated but this is the jist.
task :lab => :enviroment do
ENV['STEP'] ||= 5
Rake::Task["db:rollback"].invoke
end
Options can be passed into rake by specifying key/value pairs on the rake command:
rake options:show opt1=value1
These command line options are then automatically set as environment variables which can be accessed within your rake task:
namespace :options do
desc "Show how to read in command line options"
task :show do
p "option1 is #{ENV['opt1']}"
end
end
Passing this as an environment variable might be your best bet. Try:
task :lab => :enviroment do
Rake::Task["db:rollback"].invoke(ENV['STEP'])
end
rake db:rollback STEP=5

'rake' "in order to" 'rake'

To prepare database for my Ruby on Rails 3 application I need to run the following steps in the Terminal:
rake db:create
rake db:migrate
rake db:seed
Is it possible to do all those steps in one? Maybe it is possible running a 'rake' command that will "fire" another 'rake' command... but how?!
You can define your own rake tasks which call other tasks as prerequisites:
# lib/tasks/my_tasks.rake
namespace :db do
desc "create, migrate and seed"
task :do_all => [:create,:migrate,:seed] do
end
end
Normally the body of the task would contain Ruby code to do something, but in this case we are just invoking the three prerequisite tasks in turn (db:create,db:migrate,db:seed).
The empty do-end blocks are not needed, e.g. (for zetetic's answer)
$ cat lib/tasks/my_tasks.rake
# lib/tasks/my_tasks.rake
namespace :db do
desc "create, migrate and seed"
task :do_all => [:create,:migrate,:seed]
end
rake db:create db:migrate db:seed will do all that.
zeteitic got it right, but in the event you don't want to namespace this task under "db", you'd want something more like this:
desc "Bootstrap database."
task :bootstrap => ["db:create", "db:migrate", "db:seed"] do; end
And on the command line:
rake bootstrap
# => create, migrate and seed db

Resources