如何重写此 Capistrano 2 代码以使用 Capistrano 3 语法? (Rails)

How to rewrite this Capistrano 2 code to use Capistrano 3 syntax? (Rails)

我需要为我的 rails 应用部署带有守护程序和 capistrano 的 mailman。我遇到了这个 article 并按照他写的步骤进行了操作。

但是当我尝试部署它时,我的本地机器给了我这个错误:

The deploy has failed with an error: undefined local variable or method `mailman'.

我知道失败的原因是因为我使用的是最新版本的 capistrano(版本 3.4.0),而代码是针对它的旧版本(可能是版本 2+)

代码如下:

# Mailman configuration
namespace :mailman do
  desc "Mailman::Start"
  task :start, :roles => [:app] do
      run "cd #{current_path};RAILS_ENV=#{rails_env} script/mailman_daemon.rb start"
  end

  desc "Mailman::Stop" 
  task :stop, :roles => [:app] do
      run "cd #{current_path};RAILS_ENV=#{rails_env} script/mailman_daemon.rb stop"
  end 

  desc "Mailman::Restart" 
  task :restart, :roles => [:app] do
      mailman.stop mailman.start
  end
end 
before "deploy:cleanup", "mailman:restart"

那么,我可以知道如何重写上面的代码以遵循最新版本的 capistrano 吗?

谢谢!

##############################更新######### ###############

下面是我的 deploy.rb 文件:

server '178.62.16.69', port: 22, roles: [:web, :app, :db], primary: true

set :repo_url,        'git@github.com:ryzalyusoff/xxxx.git'
set :application,     'xxxx'
set :user,            'deploy'
set :puma_threads,    [4, 16]
set :puma_workers,    0

set :pty,             false
set :use_sudo,        false
set :stage,           :production
set :deploy_via,      :remote_cache
set :deploy_to,       "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :ssh_options,     { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true  # Change to false when not using ActiveRecord


set :whenever_identifier, ->{ "myapp_#{fetch(:stage)}" }

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
        exit
      end
    end
  end

  desc 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      invoke 'puma:restart'
    end
  end

  before :starting,     :check_revision
  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
  after  :finishing,    :restart

end


# Mailman configuration
namespace :mailman do
  desc "Mailman::Start"
  task :start do
    on roles(:app) do
      within release_path do
        with default_env: fetch(:default_env) do
          execute "script/mailman_daemon.rb", "start"
        end
      end
    end
  end

  desc "Mailman::Stop"
  task :stop do
    on roles(:app) do
      within release_path do
        with default_env: fetch(:default_env) do
          execute "script/mailman_daemon.rb", "stop"
        end
      end
    end
  end

  desc "Mailman::Restart"
  task :restart do
    on roles(:app) do
      invoke("mailman:stop")
      invoke("mailman:start")
    end
  end
end

before "deploy:cleanup", "mailman:restart"

Capistrano 3 语法变化很大,试试这个:

# Mailman configuration
namespace :mailman do
  desc "Mailman::Start"
  task :start do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute "script/mailman_daemon.rb", "start"
        end
      end
    end
  end

  desc "Mailman::Stop"
  task :stop do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute "script/mailman_daemon.rb", "stop"
        end
      end
    end
  end

  desc "Mailman::Restart"
  task :restart do
    on roles(:app) do
      invoke("mailman:stop")
      invoke("mailman:start")
    end
  end
end

before "deploy:cleanup", "mailman:restart"

有关更多选项,请参阅 Capistrano upgrade guide