Take the 2-minute tour ×

I have a rake task that connects to an ftp server, downloads a file, and then gunzips it. This works locally, but when I run the task on my heroku cedar server I get no feed back after executing net::ftp.getbinaryfile.

Here is the code to download the file:

tempfile = "#{Rails.root}/tmp/#{Process.pid}_#{MyApp::Application.config.products_bbcom_file}"
      ftp = Net::FTP.new()
      puts "connecting to ftp server #{MyApp::Application.config.products_bbcom_host}"
      puts "logging in"
      puts "changing directory"
      files = ftp.chdir(MyApp::Application.config.products_bbcom_path)
      #files = ftp.list('n*')
      puts "downloading file #{MyApp::Application.config.products_bbcom_file} to #{tempfile}"
      ftp.getbinaryfile(MyApp::Application.config.products_bbcom_file, tempfile, 1024)

When I execute the task on heroku: heroku run rake db:import and tail the logs I see:

2012-09-10T16:26:24+00:00 heroku[run.1]: Awaiting client
2012-09-10T16:26:24+00:00 heroku[run.1]: Starting process with command `bundle exec rake db:import`
2012-09-10T16:26:25+00:00 heroku[run.1]: State changed from starting to up

And the out put from the task gets to:

== Starting ==
Connecting to database specified by DATABASE_URL
connecting to ftp server datatransfer.cj.com
logging in
changing directory
downloading file

The script gets to the point where it is trying to download the file to #{Rails.root}/tmp/ but then never responds again. This only takes a couple seconds locally, but I waited a few minutes and the task doesn't do anything.

From the heroku dev site it seems like you can save files to #{Rails.root}/tmp/ on cedar. Is this possible? If so am I taking the wrong approach?

share|improve this question
Your code snippet is missing the tempfile definition. Can you update to include that? –  bensie Sep 10 '12 at 18:07
Added the temp file. I also am only using the one web dyno as this is the first time I've used heroku. I'm not sure if I need to add a worker dyno? I think when I use the heroku run command it runs as a different type of dyno (not web or worker). –  spyderman4g63 Sep 10 '12 at 19:18
add comment

3 Answers

up vote 1 down vote accepted

It is possible to download a file vit NET::FTP, but heroku only supports PASSIVE mode FTP. I needed to specify that I wanted to use passive mode. Here is a simplified example:

ftp = Net::FTP.new()
ftp.passive = true
files = ftp.chdir(path_to_file)
ftp.getbinaryfile(filename, tempfile, 1024)
share|improve this answer
Thanks for this, been banging my head on this for hours.... –  macarthy Apr 13 '13 at 9:59
add comment

You can save files to ./tmp, but know that if the dyno gets restarted that data will be gone. Additionally, one dyno cannot access another dyno's tmp directory.

For data processing and short-term storage, the tmp directory is a great spot.

share|improve this answer
I'm trying to store a csv in tmp. I only need to access while importing the csv rows into my database. Also, I'm attempting to use rails_env/tmp, I should be able to write to that dir? –  spyderman4g63 Sep 10 '12 at 19:16
add comment

Are you trying to backup/import your database?

Use PGBACKUPS. The dump goes onto S3. You can download or import from there.


I used to do the same thing you are via FTP before switching to heroku. pgbackups makes it so much easier.

share|improve this answer
No. I I'm importing a csv file to the db. –  spyderman4g63 Sep 10 '12 at 19:14
I'd import locally and then push it. devcenter.heroku.com/articles/taps#import-push-to-heroku –  mc6688 Sep 19 '12 at 17:42
add comment

Your Answer


