Gandalf StormCrow Gandalf StormCrow - 1 day ago 3
Ruby Question

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

Answer

You can pipe the content of your file to your rake task:

cat my_file | heroku run rake 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 !

Comments