jondot / sneakers

A fast background processing framework for Ruby and RabbitMQ

Home Page:https://github.com/jondot/sneakers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What is the proper way to shut down or reboot Sneakers?

alilland opened this issue · comments

I have not found any docs on the subject and I have not found any sensible ways of stopping my sneaker workers except to run ps aux | grep sneakers and then kill -9 *pid* on 5+ workers by individual PID. This sucks a lot, and means that if I push any new code I have to ssh into my production server, kill processes individually, then restart each worker for each app. Since I run several apps on the same OS and several of them use sneakers, I have had to kill ALL workers in order to push new code instead of rebooting only workers belonging to the app.

I have a sneakers process running with the following config
worker is run using bundle exec ruby sneakers.rb

# ./sneakers.rb
Sneakers.configure(
  heartbeat: 30,
  amqp: "amqp://#{ENV['RABBITMQ_USER']}:#{ENV['RABBITMQ_PASS']}@#{ENV['RABBITMQ_HOST']}",
  daemonize: ENV['RACK_ENV'] == 'production',
  pid_path: './sneakers.pid',
  vhost: '/',
  exchange: 'sneakers',
  exchange_type: :direct,
  timeout_job_after: 2.minutes,
  log: './log/sneakers.log'
)
Sneakers.logger.level = Logger::INFO
workers = Sneakers::Runner.new([Workers::JD])
workers.run

and a worker module with the following

# ./worker.rb
module Workers
  class JD
    include Sneakers::Worker
    from_queue :some_queue
    def work(msg)
      # ...
      ack!
    end
  end
end
$ ps aux | grep sneakers
alilland      69091   0.0  0.3  4398944  58044   ??  S     2:47PM   0:01.30 ruby sneakers.rb
alilland      73846   0.0  0.0  4268296    684 s000  S+    3:12PM   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn sneakers
alilland      69107   0.0  0.1  4410552  12876   ??  S     2:47PM   0:00.28 ruby sneakers.rb
alilland      69104   0.0  0.1  4410552  12720   ??  S     2:47PM   0:00.24 ruby sneakers.rb
alilland      69103   0.0  0.1  4410552  12864   ??  S     2:47PM   0:00.24 ruby sneakers.rb
alilland      69092   0.0  0.1  4410552  12512   ??  S     2:47PM   0:00.26 ruby sneakers.rb
$ kill -9 69091; kill -9 69107; kill -9 69104; kill -9 69103; kill -9 69092

there is no way that I can add ^ that to a bash script and execute it in a git hook :(

I find it best to send TERM signal to parent process.

https://github.com/jondot/sneakers/wiki/Handling-different-workloads

Found slightly better way to kill all the processes with one line. Hope it helps.

ps ax | grep sneakers | grep -v grep | awk '{print $1}' | xargs kill

oh thats pretty good @vadimeremeev

Sometimes I use a similar approach

kill -9 $(ps -ef | awk '/sneakers/{print $2}')

Since sneakers has a task called sneakers:run it could have a sneakers:stop, maybe :)

If you are using capistrano you may want to do something like the following snippet (please test before assuming it works in all situations)

desc 'Restart sneakers'
namespace :custom_sneakers do
  task restart: :environment do
    pids = `pgrep -f sneakers`.split " "
    pids.each do |pid|
      begin
        Process.kill('QUIT', pid.to_i)
      rescue Errno::ESRCH
      rescue
        puts "Other error has happened"
      end
    end

    `bundle exec rake sneakers:run`
    exit
  end
end

Signal.trap('SIGQUIT') do
  puts "Received SIGQUIT"
end