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 !
Related
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 ...)
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.
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
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
I want to generate pdf using rake task running through whenever cron job rails.When i running this rake task by this command on console it is generating pdf fine
bundle exec rake auto_invoice_email
if i give this command
which wkhtmltopdf
then i have this path
"/usr/bin/wkhtmltopdf"
But when i run it through cron job then i have this error
rake aborted!
Failed to execute:
"/usr/bin/wkhtmltopdf" -q - -
Error: PDF could not be generated!
Tasks: TOP => auto_invoice_email
(See full trace by running task with --trace)
how and where i can specify path for wkhtmltopdf for cron job
Any help?
here is rake task
require 'rubygems'
require 'wicked_pdf'
require 'erb'
require 'date'
task :auto_invoice_email => :environment do
#$PATH = '/usr/bin/wkhtmltopdf'
include ApplicationHelper
users = User.find_subscribers
unless users.blank?
users.each do |user|
#user = user
content = File.read "#{Rails.root}/app/views/accountings/generate_invoice_pdf_rake.html.erb"
template = ERB.new(content)
html_content = template.result(binding)
pdf= WickedPdf.new.pdf_from_string(html_content, :wkhtmltopdf => '/usr/bin/wkhtmltopdf')
name = "#{Rails.root}/public/#{user.subscriber.downcase+" "+Time.now.to_s}.pdf"
File.open(name, 'wb') do |file|
file << pdf
end
end
end
end
Thanks