简体   繁体   中英

How can I clear rails cache after deploy to heroku?

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.

Rails 有一个内置的 rake 任务:

rake tmp:clear

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.

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

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

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

heroku run rake tmp:cache:clear

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

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.

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, eg: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM