【问题标题】:Starting sidekiq with capistrano使用 capistrano 启动 sidekiq
【发布时间】:2014-07-23 14:14:54
【问题描述】:

我想用 capistrano 开始 sidekiq。下面是代码

namespace :sidekiq do
  task :start do
    run "cd #{current_path} && bundle exec sidekiq -c 10 -e production -L log/sidekiq.log &"
    p capture("ps aux | grep sidekiq | awk '{print $2}' | sed -n 1p").strip!    
  end
end

执行成功,但是sidekiq仍然没有在服务器上启动。

输出:

$ cap sidekiq:start
    triggering load callbacks
  * 2014-06-03 15:03:01 executing `sidekiq:start'
  * executing "cd /home/project/current && bundle exec sidekiq -c 10 -e production -L log/sidekiq.log &"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
    command finished in 1229ms
  * executing "ps aux | grep sidekiq | awk '{print $2}' | sed -n 1p"
    servers: ["x.x.x.x"]
    [x.x.x.x] executing command
    command finished in 1229ms
"19291"

【问题讨论】:

  • 那么你是如何杀死之前的sidekiq进程的呢?
  • sidekiq_process_id = capture("ps aux | grep sidekiq | awk '{print $2}' | sed -n 1p").strip!;运行 "kill -15 #{sidekiq_process_id}" 并成功杀死:)
  • 为什么不使用现有的 gem? github.com/seuros/capistrano-sidekiq
  • 我不想要单行代码的 gem。
  • @sagarjunnarkar - 它会让你的生活更轻松。无论如何,log/sidekiq.log 中有任何有趣的事情。上次我遇到类似问题时,它似乎可以正确启动,然后就崩溃了。

标签: ruby ruby-on-rails-3 capistrano sidekiq


【解决方案1】:

以防万一以某种方式尝试start/restart/stop 环境与capistrano

bundle exec cap production sidekiq:start
bundle exec cap production sidekiq:stop
bundle exec cap production sidekiq:restart
#staging
bundle exec cap staging sidekiq:start
bundle exec cap staging sidekiq:stop
bundle exec cap staging sidekiq:restart
#same with other dependencies
bundle exec cap production puma:restart
bundle exec cap staging puma:stop

简要说明

(如果您在线访问 repo,例如 github,请记住运行您的 ssh 代理以通过 repo 中的 ssh 连接并拉取最新版本的代码/分支)

  • 在本地设置您自己的 github ssh 密钥
  • 使用密钥eval $(ssh-agent) && ssh-add ~/.ssh/id_rsa 运行ssh 代理
  • ssh -T git@github.com检查代理

之后我总是用这个来部署

  • 针对环境运行 capistrano bundle exec cap staging deploy

当您已经在生产中并且遇到问题但特别是对于暂存时,这些非常方便,您可以根据您的Capfile 执行单独的执行(例如,大多数时候我使用puma 作为rack middleware 服务器和sidekiqscheculed-jobs)

Capfile

require "capistrano/setup"

# Include default deployment tasks
require "capistrano/deploy"

# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
# install_plugin Capistrano::SCM::Hg
# or
# require "capistrano/scm/svn"
# install_plugin Capistrano::SCM::Svn
# or
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#   https://github.com/capistrano/passenger
#
require "capistrano/rvm"
# require "capistrano/rbenv"
# require "capistrano/chruby"
require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require "capistrano/yarn"
require "capistrano/puma"
install_plugin Capistrano::Puma  # Default puma tasks
require 'capistrano/sidekiq'
require 'slackistrano/capistrano'
require_relative 'lib/capistrano/slack_deployment_message'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

所以最终在capistrano启用/安装/配置的这些功能上执行start|stop|restart

我总是可以在生产中使用 capistrano 重新启动 puma:

  • bundle exec cap production sidekiq:restart
  • bundle exec cap production puma:restart

以及在舞台上:

  • bundle exec cap staging sidekiq:restart
  • bundle exec cap staging puma:restart

希望这会有所帮助! :D

【讨论】:

    【解决方案2】:

    在不使用任何 gem 的情况下,这是我与 Capistrano 3.4.0 完美配合的解决方案

    namespace :sidekiq do
    
      task :restart do
        invoke 'sidekiq:stop'
        invoke 'sidekiq:start'
      end
    
      before 'deploy:finished', 'sidekiq:restart'
    
      task :stop do
        on roles(:app) do
          within current_path do
            pid = p capture "ps aux | grep sidekiq | awk '{print $2}' | sed -n 1p"
            execute("kill -9 #{pid}")
          end
        end
      end
    
      task :start do
        on roles(:app) do
          within current_path do
            execute :bundle, "exec sidekiq -e #{fetch(:stage)} -C config/sidekiq.yml -d"
          end
        end
      end
    end
    

    【讨论】:

    • 当 sidekiq 尚未运行时,这会不会造成一些问题?
    • 使用pgrep sidekiq 而不是grep sidekiq | awk '{print $2}' 更安全。这确保您不会使用 grep 命令 pid 而不是 sidekiq。此外,如果有多个 sidekiq 进程,你最好杀死你找到的所有进程
    【解决方案3】:

    你的问题在这里:

      cd /home/project/current && bundle exec sidekiq -c 10 -e production -L log/sidekiq.log &
    

    当您在末尾添加& 时,命令正在单独的进程中执行,但该进程仍然是当前进程的子进程,并在当前进程停止时终止。相反,您需要将 sidekiq 作为守护进程运行。

    bundle exec sidekiq -c 10 -e production -L log/sidekiq.log -d
    

    注意额外的-d 选项

    【讨论】:

    • @sagarjunnarkar - 你的 sidekiq 版本是什么?
    • Sidekiq 版本为 2.6.0
    • @sagarjunnarkar - -d 选项已在 2.7.0 中添加。您可以尝试运行sidekiq help 并检查是否有任何等价物(如--deamon)
    • 感谢它的工作。只是最后一个问题。如果我只是在 Gemfile 中更改 sidekiq 版本可以吗?还是我必须对代码进行大量更改?
    • @sagarjunnarkar - 改用 2.7?我不完全确定这两个版本之间还有什么变化,但我不会说太多。试一试几周,寻找工人的问题,但应该会很顺利。
    猜你喜欢
    • 2016-11-08
    • 2018-06-15
    • 2016-10-31
    • 2017-04-30
    • 2016-06-11
    • 2014-10-10
    • 2016-08-19
    • 2012-10-03
    • 2014-10-21
    相关资源
    最近更新 更多