简体   繁体   中英

Rails 5, Capistrano 3 how to clear cache after deploy

I want to execute the cap equivalent of

Rails.cache.clear

after deploy, but cannot get it to work. Here's my attempt in the deploy.rb file

namespace :deploy do
    after :restart, :clear_cache do
        on release_roles(fetch(:assets_roles)) do
            within release_path do
                with rails_env: fetch(:rails_env) do
                    Rails.cache.clear
                end
            end
        end
    end
end

But this doesn't work:

SSHKit::Runner::ExecuteError: Exception while executing as deploy@hostname.com  uninitialized constant Rails

If not this, what?

Thanks for any help, Kevin

update:

This is the right syntax:

namespace :deploy do
    task :clear_cache do
        on roles(:app) do |host|
            with rails_env: fetch(:rails_env) do
                within current_path do
                    execute :rake, "cache:clear"
                end
            end
        end
    end
end

I suggest that you should create a rake task for clearing cache and invoke them using capistrano hooks. For example:

lib/tasks/cache.rb

namespace :cache do
  desc 'clear rails cache'
  task clear: :environment do
    Rails.cache.clear
  end
end

config/deploy.rb

namespace :cache do
  task :clear do
    on roles(:app) do |host|
      with rails_env: fetch(:rails_env) do
        within current_path do
          execute :bundle, :exec, "rake cache:clear"
        end
      end
    end
  end
end

after 'deploy:update', 'cache:clear'

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