Rails 5,Capistrano 3如何在部署后清除缓存

时间:2018-09-27 23:57:10

标签: ruby-on-rails capistrano

我要执行等价的

Rails.cache.clear

部署后,但无法正常工作。这是我在deploy.rb文件中的尝试

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

但这不起作用:

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

如果不是这样,那是什么?

感谢您的帮助, 凯文

1 个答案:

答案 0 :(得分:1)

我建议您创建一个rake任务以清除缓存,并使用capistrano挂钩调用它们。例如:

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'