[英]Rails.cache.clear fails with undefined method `clear' for nil:NilClass when running in a rake task
[英]Uninitialized constant Rails when i tried to run Rails.cache.clear in capistrano task when deployed to vps?
我試着跑
Rails.cache.clear
當我將我的應用程序部署到服務器時,通過Capistrano任務。 這是我的代碼
namespace :deploy do
desc 'Clear memcache'
task :clear_memcache do
on roles(:app) , in: :sequence, wait: 2 do
Rails.cache.clear
CACHE.flush
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :copy_missing_css
after :finishing, :clear_cache
after :finishing, :clear_memcache
after :finishing, :restart
end
但我得到了這個錯誤。
The deploy has failed with an error: #<NameError: uninitialized constant Rails>
我怎樣才能解決這個問題?
謝謝!
你的Capistrano任務不是一個rails應用程序 - 在它們內部Rails
不存在。 即使它確實如此,這是在執行部署的機器上執行的代碼,而不是您的服務器(並且memcached不應該是外部可用的)
你想要的是在服務器上運行一些東西,你可以通過execute
方法運行,或者通過諸如為你調用execute的runner
或rake
幫助器運行。 這使用bin/rails runner
來執行代碼
within release_path do
with rails_env: fetch(:rails_env) do
runner "Rails.cache.clear"
end
end
您還可以創建一個rake任務並通過execute
調用它。 另外,您只需要運行一次 - 它不需要在所有服務器上重復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.