37

I applied cache to my heroku rails app and it works well. But everytime I deploy to heroku, I also want to clear cache automatically.

so I googled and I found this.

task :after_deploy, :env, :branch do |t, args|
  puts "Deployment Complete"
  puts "Cleaning all cache...."
  FileUtils.cd Rails.root do
    sh %{heroku run console}
    sh %{Rails.cache.clear}
  end
end

but when I raked this script, it just show the heroku console command line but the Rails.cache.clear command does not typed. (I guess that is because heroku console is interactive)

and

system "heroku console Rails.cache.clear"

doesn't work for cedar apps.

How can I solve this problem?

Thanks.

braX
  • 11,506
  • 5
  • 20
  • 33
kikiki.blue
  • 541
  • 1
  • 4
  • 12
  • possible duplicate of [Clear Memcached on Heroku Deploy](http://stackoverflow.com/questions/7732985/clear-memcached-on-heroku-deploy) – Jonathan Tran Feb 11 '13 at 17:48

8 Answers8

43

Rails has a built in rake task:

rake tmp:clear
gabeodess
  • 2,006
  • 21
  • 13
39

The following should work on cedar:

heroku run console

..then wait 5 seconds for heroku console to boot

Rails.cache.clear

Then you should see the cache clear, and you can quit console. Remember you might have to refresh a few times because your local machine will often cache assets and such in your browser until it makes a fresh request.

If it happens to be assets that you're caching though, you don't need to go through a manual clear every time you push, you just need to have the asset pipeline set up and make sure all your js/css(less/sass)/static images are being compiled with hashes at the end of their filenames.

a.ross.cohen
  • 399
  • 2
  • 2
  • but as you know, the script what I wrote is rake script. I know that the command 'heroku run console' returns after 5 seconds and I can type 'Rails.cache.clear'. but I want to automate it with my rake script everytime I deploy to heroku. – kikiki.blue Aug 20 '12 at 14:08
  • follow this pattern http://michaeldwan.com/writings/customize-your-heroku-deployment.html and then add an :after_deploy hook that just calls Rails.cache.clear. – a.ross.cohen Aug 20 '12 at 18:37
  • Yes I saw that pattern. but I don't know how can I connect to heroku console and call Rails.cache.clear. – kikiki.blue Aug 21 '12 at 14:07
31

You should be able to create a cache clearing rake task that looks something like this:

namespace :cache do
  desc "Clears Rails cache"
  task :clear => :environment do
    Rails.cache.clear
  end
end

and invoke it directly in one command that you can use in your post deploy hook - like so:

heroku run rake cache:clear
Andrew Hubbs
  • 9,338
  • 9
  • 48
  • 71
Ivar
  • 5,102
  • 2
  • 31
  • 43
15

Ruby on Rails has a magical ENV variable called 'RAILS_CACHE_ID'. I set this to the git commit id whenever I deploy: heroku config:set RAILS_CACHE_ID=$CIRCLE_SHA1

nthj
  • 445
  • 4
  • 7
  • 1
    TIL `RAILS_CACHE_ID` is a thing. Very cool. Adjusted for those of us not using circle ci: `heroku config:set RAILS_CACHE_ID=`git rev-parse HEAD`` – steve Aug 24 '16 at 18:33
4

If you want to simply run a rake task after deploy, I would recommend checking out:

https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks

We've been using it in production for over 6 months and it's been rock solid.

First, add the buildpack AFTER you already have the Ruby buildpack set. This should happen after your first deploy to the server

heroku buildpacks:add https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks

Second, set an environment variable called DEPLOY_TASKS with just the names of the rake tasks you want to run separated by spaces.

heroku config:set DEPLOY_TASKS='cache:clear

rvirani
  • 49
  • 1
  • 2
  • 2
    This is the best approach and should be marked as accepted answer. This solution does not imply manual action nor does it imply to modify the project itself. – foobar443 Apr 11 '17 at 07:45
  • 1
    As a note, this has been deprecated in favour of Heroku's "Release Phase" functionality – Richard Peck Jun 27 '17 at 15:02
0

heroku run rake tmp:cache:clear

http://guides.rubyonrails.org/command_line.html#tmp

0

Heroku doesn't currently support a pipeline of actions to occur after deployment. You'll need something like Codeship or TravisCI to create a recipe of steps that happen during deployment

Disclosure: I am a customer of Codeship.

Jonathan Mui
  • 2,471
  • 3
  • 19
  • 27
0

Heroku has a release phase, which means you can put any deploy-time tasks inside your Procfile:

release: bundle exec rake tmp:clear

You might want to set up your own custom rake task to encapsulate any other tasks that need to happen at deploy, e.g.: db:migrate and so forth. Keep in mind that if the task fails for any reason, Heroku will halt the deploy and the previously deployed version of the code will remain active.

lobati
  • 9,284
  • 5
  • 40
  • 61