【问题标题】:Rails 5, Capistrano 3 how to clear cache after deployRails 5,Capistrano 3部署后如何清除缓存
【发布时间】:2018-09-27 23:57:10
【问题描述】:

我想执行相当于

的上限
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

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

感谢您的帮助, 凯文

更新:

这是正确的语法:

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

【问题讨论】:

  • 你可以使用this gem。并在deploy.rb中添加set :clear_cache_after_deploy, true这一行,每次部署后缓存都会被清空。

标签: ruby-on-rails capistrano


【解决方案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'

【讨论】:

  • 语法略有变化,但精神是正确的,我会在我的问题中发布正确的答案。
  • @user1130176 发生了什么变化?
  • @CyrilDuchon-Doris 感谢您的推动,我已更新问题以显示正确的语法
猜你喜欢
  • 2012-08-15
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
相关资源
最近更新 更多