Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 57 lines (44 sloc) 2.26 KB
#!/usr/bin/env ruby
### TO TEST : sudo ruby restart-dj-worker-yaml CRITICAL SOFT/HARD 3 ###
require 'yaml'
RAILS_ROOT = '/home/deploy/www/taskrabbit/current'
LOG_PATH = '/usr/local/nagios/var'
#BUNDLE_EXEC = '/home/deploy/opt/ree/bin/bundle'
env = `su - deploy -c 'source /home/deploy/.ssh/environment ; . /home/deploy/.ssh/environment ; source /home/deploy/.profile ; . /home/deploy/.profile ; echo $RAILS_ENV'`.strip
### READING YAML CONFIG FILE TO FIGURE WHATS THE HOST CONFIG ###
dj = YAML.load_file("#{RAILS_ROOT}/config/delayed_jobs.yml")[env]['servers'][`hostname`.strip]
wks = dj.collect { |wkr| wkr =~ /-i (\d{1,2})/; $1.to_s }
min_prio = dj.collect { |min| min =~ /--min-priority (\d{1,})/; $1 }
max_prio = dj.collect { |max| max =~ /--max-priority (\d{1,})/; $1 }
current_workers =`ps awwx | grep delayed | grep -v grep`.split("\n").map { |x| x.split("\s")[7].to_s }
#current_workers = [ "14", "15" ] ### FAKE A DEAD WORKER
dj_config = {}
wks.each_with_index do |value, index|
dj_config[value] ||= {}
dj_config[value][:worker] = value
dj_config[value][:min_priority] = min_prio[index]
dj_config[value][:max_priority] = max_prio[index]
end
### FIGURING WHICH ONES ARE DEAD AND LETS RESTART THEM ###
#ARGV.each_with_index { |x,i| puts "#{x} : #{i}" }
dead_wks = wks - current_workers
exec_cmd = []
dead_wks.each do |dead_worker|
cmd = "-i #{dj_config[dead_worker][:worker]}"
cmd << " --min-priority #{dj_config[dead_worker][:min_priority]} start" if dj_config[dead_worker][:min_priority]
cmd << " --max-priority #{dj_config[dead_worker][:max_priority]} start" if dj_config[dead_worker][:max_priority]
exec_cmd << "su - deploy -c 'source /home/deploy/.ssh/environment ; . /home/deploy/.ssh/environment ; source /home/deploy/.profile ; . /home/deploy/.profile ; echo $RAILS_ENV > /tmp/env.log ; cd #{RAILS_ROOT}/ && RAILS_ENV=#{env} bundle exec script/delayed_job #{cmd} ' & ; "
end
### RESTART HANDLER LOGIC ###
if ARGV[0] == "CRITICAL"
if ARGV[1] == "SOFT"
if ARGV[2] == "2"
%x[echo "`date` - DJ - About to restart dead DJ workers - SOFT" >> #{LOG_PATH}/eventhandlers.log]
end
elsif ARGV[1] == "HARD"
%x[echo "`date` - DJ - Restarting dead DJ workers - HARD" >> #{LOG_PATH}/eventhandlers.log]
exec_cmd.each do |x|
`#{x}`
end
end
end