Created
June 8, 2013 07:21
-
-
Save spickermann/5734397 to your computer and use it in GitHub Desktop.
bash script to start and stop resque workers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment')) | |
| require 'optparse' | |
| require 'timeout' | |
| require 'ostruct' | |
| USAGE = "usage: script/resque command [options]" | |
| COMMANDS = %w( start status stop ) | |
| TIMEOUT = 60 | |
| DEFAULTS = { :count => 1, | |
| :queue => '*', | |
| :interval => 5.0, | |
| :background => 'yes', | |
| :verbose => 1 }.freeze | |
| PARAMETERS = { 'count [N]' => 'Number of resque forks', | |
| 'queues [a,b,c]' => 'Queues to observe', | |
| 'interval [Float]' => 'Polling frequency' }.freeze | |
| ################################################################ | |
| # PARAMETER PARSING | |
| ################################################################ | |
| def options | |
| @options ||= parse_options(:pidfile => pidfile) | |
| end | |
| def parse_options(additional = {}) | |
| OpenStruct.new(DEFAULTS.dup.update(additional)).tap do |options| | |
| OptionParser.new do |opts| | |
| opts.banner = USAGE | |
| # optional parameters | |
| PARAMETERS.each do |option, description| | |
| flag = option.first | |
| name = option.split(' ').first | |
| opts.on("-#{flag}", "--#{option}", description) do |value| | |
| options.send("#{name}=", value) if value | |
| end | |
| end | |
| opts.on("-h", "--help", "Show this message") { puts opts; exit } | |
| end.parse!(ARGV) | |
| options.command = ARGV.first | |
| unless COMMANDS.include?(options.command) | |
| puts USAGE | |
| exit(-1) | |
| end | |
| end | |
| end | |
| def pidfile | |
| i = 0 | |
| loop do | |
| file = Rails.root.join('tmp', 'pids', "resque_worker.#{i += 1}.pid") | |
| return file unless File.exists?(file) | |
| end | |
| end | |
| ################################################################ | |
| # HELPER METHODS | |
| ################################################################ | |
| def parameterized_options | |
| options.marshal_dump.map { |p, v| "#{p.to_s.upcase}=#{v}" }.join(' ') | |
| end | |
| def pids | |
| files = Dir[Rails.root.join('tmp', 'pids', 'resque_worker.*.pid')] | |
| pids = files.map do |file| | |
| [File.open(file) { |f| f.read.strip.to_i }, file] rescue nil | |
| end | |
| Hash[*pids.compact.flatten] | |
| end | |
| def kill!(signal) | |
| pids.each do |pid, file| | |
| begin | |
| Process.kill(signal, pid) | |
| rescue Errno::ESRCH | |
| File.delete(file) | |
| end | |
| end | |
| end | |
| ################################################################ | |
| # COMMANDS | |
| ################################################################ | |
| def start | |
| `#{parameterized_options} rake resque:work` | |
| end | |
| def status | |
| pids.each do |pid, file| | |
| puts `ps -p #{pid} | sed -n 2p` | |
| end | |
| end | |
| def stop | |
| kill!('QUIT') | |
| begin | |
| Timeout::timeout(TIMEOUT) do | |
| sleep(1) while pids.present? | |
| end | |
| rescue Timeout::Error | |
| kill!('TERM') | |
| FileUtils.rm_f(pids.values) | |
| end | |
| end | |
| ################################################################ | |
| # RUN COMMAND | |
| ################################################################ | |
| send(options.command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a Ruby script, not a bash script! But yes, it can be run from the shell.