How do I properly manage the rails tmp directory?
Sophia Terry
After I deploy a rails application in production mode, do I need to schedule a periodic cleanup of the rails tmp directory? aka: rake tmp:clear (or its sub-parts tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear).
I know a few major revisions of rails back this was something that was needed to be done. I'm currently using Rails 4.1.x. Thanks.
22 Answers
Add one or more of those to your crontab file and that should do it for you...
rake tmp:cache:clear
rake tmp:clear
rake tmp:create
rake tmp:sessions:clear
rake tmp:sockets:clear Keep in mind, clearing sessions will kill all active sessions to. I don't recommend that. You could create a model called:
Periodic with something like this in it:
def self.run CGI::Session::ActiveRecordStore::Session. destroy_all( ['updated_at <?', 48.hours.ago] ) endthen cron your script/runner like this
script/runner -e production Periodic.run Here's what Rails 6 spits out for rails --tasks in terms of clearing the tmp directory.
rails tmp:clear # Clear cache, socket and screenshot files from tmp/ (narrow w/ tmp:cache:clear, tmp:sockets:clear, tmp:screenshots:clear)
rails tmp:create # Creates tmp directories for cache, sockets, and pidsNote: it does mention tmp:cache:clear, tmp:sockets:clear, tmp:screenshots:clear in the tmp:clear comment.
For me my cache was filling up my Docker container and running out of inodes, thus looking like it ran out of disk space. I'm going to try and configure caching settings for production to help this out.
It sounds like if you would like to have it auto expire with file caching you can do something like this: Rails: control file store cache size.
They mention to use the :expires_in for file caches, otherwise you can set a memory size limit with something like config.cache_store = :memory_store, { size: 64.megabytes }
Also here's the current documentation on caching, which also mentions :expires_in