Uninitialized constant Rails when i tried to run Rails.cache.clear in capistrano task when deployed to vps?

I tried to run

Rails.cache.clear

by Capistrano task when i deploy my app to the server. This is my code

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

But i got this error.

The deploy has failed with an error: #<NameError: uninitialized constant Rails>

How can i fix this?

Thanks!

Answers


Your Capistrano tasks are not a rails app - within them Rails does not exist. Even if it did, this is code executing on the machine doing the deploy, not your servers (and memcached should not be externally available)

What you want is to run something on the server, which you do with execute method, or via helpers such as runner or rake that call execute for you. This uses bin/rails runner to execute the code

within release_path do
  with rails_env: fetch(:rails_env) do
    runner "Rails.cache.clear"
  end
end

You could also create a rake task and invoke that via execute. As an aside you only need to run this once - it doesn't need to be repeated on all servers.


Need Your Help

An NSArrayController changes its selection : what is the best way to catch this event?

macos cocoa model-view-controller nsarraycontroller

One can put an observer on the selectedIndex method of NSArrayController. This method has some drawbacks I think :