Sidekiq未能在runit下停止

时间:2014-02-27 06:23:34

标签: sidekiq runit

我们在runit下运行很多服务,而且效果很好。

最近开始使用Sidekiq,这太棒了。它在runit下运行它。问题是当运行“sv stop”时。它并没有停止这个过程。调用重启实际上会带来另一个sidekiq实例,让旧的实例继续运行。

这是我们的示例运行文件:

#!/bin/sh
cd /PATH_TO_SIDEKIQ
exec 2>&1
export DB_POOL_SIZE=25
exec bundle exec sidekiq -e production 2>&1 | logger -p local2.info -t sidekiq

supervise / pid中的pid文件匹配正在运行的pid。

谢谢,杰里米

2 个答案:

答案 0 :(得分:0)

好的,我不确定确切的原因,但我认为这是SideKiq的启动方式。它得到了它自己的pid和runit也分配了一个。无论如何我通过修改内置的capistrano配方解决了这个问题。

基本思路是使用sidekiqctl发送:相当信号,以便工人停止接受新工作。然后开启:停止我们停止运行然后发送sidekiqctl:停止信号。一开始我们使用runit再次启动该过程。

Capistrano::Configuration.instance.load do

  _cset(:sidekiq_default_hooks) { true }
  _cset(:sidekiq_cmd) { "#{fetch(:bundle_cmd, "bundle")} exec sidekiq" }
  _cset(:sidekiqctl_cmd) { "#{fetch(:bundle_cmd, "bundle")} exec sidekiqctl" }
  _cset(:sidekiq_timeout)   { 10 }
  _cset(:sidekiq_role)      { :app }
  _cset(:sidekiq_pid)       { "#{current_path}/tmp/pids/sidekiq.pid" }
  _cset(:sidekiq_processes) { 1 }

  if fetch(:sidekiq_default_hooks)
    before "deploy:update_code", "sidekiq:quiet"
    after "deploy:stop",    "sidekiq:stop"
    after "deploy:start",   "sidekiq:start"
    before "deploy:restart", "sidekiq:restart"
  end

  namespace :sidekiq do
    def for_each_process(&block)
      fetch(:sidekiq_processes).times do |idx|
        yield((idx == 0 ? "#{fetch(:sidekiq_pid)}" : "#{fetch(:sidekiq_pid)}-#{idx}"), idx)
      end
    end

    desc "Quiet sidekiq (stop accepting new work)"
    task :quiet, :roles => lambda { fetch(:sidekiq_role) }, :on_no_matching_servers => :continue do
      for_each_process do |pid_file, idx|
        run "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} quiet #{pid_file} ; else echo 'Sidekiq is not running'; fi"
      end
    end

    desc "Stop sidekiq"
    task :stop, :roles => lambda { fetch(:sidekiq_role) }, :on_no_matching_servers => :continue do
      for_each_process do |pid_file, idx|
        sidekiq_dir = idx == 0 ? "/SERVICE_DIR/sidekiq" : "/SERVICE_DIR/sidekiq-#{idx}"
        run "sv stop #{sidekiq_dir}"
        run "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} stop #{pid_file} #{fetch :sidekiq_timeout} ; else echo 'Sidekiq is not running'; fi"
      end
    end

    desc "Start sidekiq"
    task :start, :roles => lambda { fetch(:sidekiq_role) }, :on_no_matching_servers => :continue do
      rails_env = fetch(:rails_env, "production")
      for_each_process do |pid_file, idx|
        sidekiq_dir = idx == 0 ? "/SERVICE_DIR/sidekiq" : "/SERVICE_DIR/sidekiq-#{idx}"
        run "sv start #{sidekiq_dir}"
      end
    end

    desc "Restart sidekiq"
    task :restart, :roles => lambda { fetch(:sidekiq_role) }, :on_no_matching_servers => :continue do
      stop
      start
    end

  end
end

答案 1 :(得分:0)

我对 Capistrano 3的解决方案

set :sidekiq_runit_service_name, "sidekiq"
set :sidekiq_default_hooks, ->{ true }
set :sidekiq_cmd, ->{ "#{fetch(:bundle_cmd, "bundle")} exec sidekiq" }
set :sidekiqctl_cmd, ->{ "#{fetch(:bundle_cmd, "bundle")} exec sidekiqctl" }
set :sidekiq_timeout, ->{ 10 }
set :sidekiq_role,    ->{ :app }
set :sidekiq_pid,     ->{ "#{current_path}/tmp/pids/sidekiq.pid" }

namespace :sidekiq do

  desc "Quiet sidekiq (stop accepting new work)"
  task :quiet do
    on roles(fetch(:sidekiq_role)) do
      execute "if [ -d #{current_path} ] && [ -f #{fetch(:sidekiq_pid)} ] && kill -0 `cat #{fetch(:sidekiq_pid)}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} quiet #{fetch(:sidekiq_pid)} ; else echo 'Sidekiq is not running'; fi"
    end
  end

  desc "Stop sidekiq"
  task :stop do
    on roles(fetch(:sidekiq_role)) do
      execute "sv stop #{fetch(:sidekiq_runit_service_name)}; true"
      execute "if [ -d #{current_path} ] && [ -f #{fetch(:sidekiq_pid)} ] && kill -0 `cat #{fetch(:sidekiq_pid)}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} stop #{fetch(:sidekiq_pid)} #{fetch :sidekiq_timeout} ; else echo 'Sidekiq is not running'; fi"
    end
  end

  desc "Start sidekiq"
  task :start do
    on roles(fetch(:sidekiq_role)) do
      execute "sv start #{fetch(:sidekiq_runit_service_name)}"
    end
  end

  desc "Restart sidekiq"
  task :restart do
    invoke 'sidekiq:stop'
    invoke 'sidekiq:start'
  end

end

if fetch(:sidekiq_default_hooks)
  before 'deploy:started',   "sidekiq:quiet"
  after  'deploy:updated',   "sidekiq:stop"
  after  'deploy:reverted',  "sidekiq:stop"
  after  'deploy:published', "sidekiq:start"
  after  'deploy:restart',   "sidekiq:restart"
end