使用 Capistrano 将 Sidekiq 部署到 Ubuntu 时出错

Error deploying Sidekiq to Ubuntu using Capistrano

我正在按照此处的说明在 Ubuntu 上将 Sidekiq 与 Capistrano 集成:https://github.com/mperham/sidekiq/wiki/Deploying-to-Ubuntu

我的 Capistrano 部分有问题。

我把这段代码放在deploy.rb的末尾:

# For capistrano 3
namespace :sidekiq do
  task :quiet do
    # Horrible hack to get PID without having to use terrible PID files
    puts capture("kill -USR1 $(sudo initctl status workers | grep /running | awk '{print $NF}') || :") 
  end
  task :restart do
    execute :sudo, :initctl, :restart, :workers
  end
end

after 'deploy:starting', 'sidekiq:quiet'
after 'deploy:reverted', 'sidekiq:restart'
after 'deploy:published', 'sidekiq:restart'

当我 运行 cap production deploy:

时出现此错误
cap aborted!
NoMethodError: undefined method `capture' for main:Object
config/deploy.rb:67:in `block (2 levels) in <top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2@project/gems/capistrano-3.2.1/lib/capistrano/dsl/task_enhancements.rb:12:in `block in after'
/Users/user/.rvm/gems/ruby-2.1.2@project/gems/capistrano-3.2.1/lib/capistrano/dsl.rb:15:in `invoke'
/Users/user/.rvm/gems/ruby-2.1.2@project/gems/capistrano-3.2.1/lib/capistrano/tasks/framework.rake:65:in `block (2 levels) in <top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2@project/gems/capistrano-3.2.1/lib/capistrano/tasks/framework.rake:64:in `each'
/Users/user/.rvm/gems/ruby-2.1.2@project/gems/capistrano-3.2.1/lib/capistrano/tasks/framework.rake:64:in `block in <top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2@project/gems/capistrano-3.2.1/lib/capistrano/application.rb:15:in `run'
/Users/user/.rvm/gems/ruby-2.1.2@project/gems/capistrano-3.2.1/bin/cap:3:in `<top (required)>'
/Users/user/.rvm/gems/ruby-2.1.2@project/bin/cap:23:in `load'
/Users/user/.rvm/gems/ruby-2.1.2@project/bin/cap:23:in `<main>'
/Users/user/.rvm/gems/ruby-2.1.2@project/bin/ruby_executable_hooks:15:in `eval'
/Users/user/.rvm/gems/ruby-2.1.2@project/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => sidekiq:quiet
(See full trace by running task with --trace)
The deploy has failed with an error: #<NoMethodError: undefined method `capture' for main:Object>

很明显是capture函数没有定义,但是有什么问题呢?我是否将代码放在正确的文件中 deploy.rb?我需要什么吗?

尝试要求 sidekiq gem,或查看 capistrano with sidekiq integration

您必须指定 运行 代码所在的服务器。例如:

namespace :sidekiq do
  task :quiet do
    # Horrible hack to get PID without having to use terrible PID files
    on roles(:app) do
      puts capture("kill -USR1 $(sudo initctl status workers | grep /running | awk '{print $NF}') || :") 
    end
  end
  task :restart do
    on roles(:app) do
      execute :sudo, :initctl, :restart, :workers
    end
  end
end