I have an app hosted @ Heroku. The app depends on some feeds which are fetched using a socket listener. The socket listener gets one line of XML per second. Once I detect the end of file signal from the listener, I upload the file to Amazon S3 servers. But, until the end of file signal i开发者_运维技巧s received, is it possible to save the file content as a temporary file in Heroku?
You may be able to use the #{RAILS_ROOT}/tmp/
directory or Rails.root.join('tmp').to_s
:
Aspen & Bamboo
[...]
There are two directories that are writeable:./tmp
and./log
(under your application root).
[...]Cedar
Cedar offers an ephemeral writeable filesystem. You can write out to disk anywhere you would like. Your changes will be lost on dyno restart and spin-up.
RAILS_ROOT
is for older Rails versions, Rails.root
is for newer versions.
You can't depend on anything surviving across requests of course, there's no guarantee that you'll even be working with the same dyno.
As long as you stay within the same process or request, Rails.root.join('tmp')
should be usable. If you need the temporary data to survive across requests or processes then you're better off using something else (such as MongoDB or PostgreSQL) as a collecting ground for your data on its way to S3.
Thanks to Benjamin Wheeler for the heads up about the RAILS_ROOT
to Rails.root
change.
The documentation on Heroku's read-only file system explains that you can use #{RAILS_ROOT}/tmp
but doesn't give any examples for generating a temporary filename. This will guarantee uniqueness of the filename:
prefix = 'mydata'
suffix = '.xml'
Tempfile.new [prefix, suffix], "#{Rails.root}/tmp"
Note that "there is no guarantee that this file will be there on subsequent requests (although it might be), so this should not be used for any kind of permanent storage."
With the newer Heroku-16 stack, you are able to write to both the root and to /tmp
Try writing to root with
f = File.new("filename.txt", 'w')
f << "hi there"
f.close
Dir.entries(Dir.pwd) # see your newly created file
Or to /tmp
with
f = File.new("tmp/filename.txt", 'w')
f << "hi there"
f.close
Dir.entries(Dir.pwd.to_s + ("/tmp"))
You will see your new file among those listed in both cases
Also try running heroku restart
to see your newly created files disappear! This is expected, as heroku storage is ephemeral (will be deleted when the app restarts) - so don't rely on it for anything more than (very) temporary storage
精彩评论