Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
#
# description: A startup script to automatically start workers for delayed_job
APP_NAME = 'worker_startup_script'
APP_DIR = '<%= @node[:delayed_job][:app_path] %>'
DEFAULT_NUM_WORKERS = 4
unless %w(start stop restart status).include? ARGV.first
puts "Usage: #{APP_NAME} start|stop|status|restart 4 (for 4 workers - start and restart only)"
exit
end
require(File.join(APP_DIR, 'config', 'boot'))
@number_of_workers = ARGV.last =~ /\d+/ ? ARGV.last.to_i : DEFAULT_NUM_WORKERS
@log = File.open("#{RAILS_ROOT}/log/rake.log", "w+")
def start
message = "Starting #{@number_of_workers} delayed_job workers"
puts message
@log.puts message
system "RAILS_ENV=production /usr/bin/ruby #{RAILS_ROOT}/script/delayed_job -n #{@number_of_workers} run 2>&1 >> #{RAILS_ROOT}/log/rake.log &"
end
def stop
pids = running_pids
message = "Found #{pids.length} delayed_job worker group. Stopping them..."
puts message
@log.puts message
pids.each do |pid|
system "kill -TERM #{pid}"
end
end
def restart
stop
start
end
def status
if running_pids.empty?
message = "DelayedJob is not running"
puts message
@log.puts message
exit(1)
else
message = "DelayedJob is running"
puts message
@log.puts message
exit(0)
end
end
def running_pids
`ps -ef | grep -v awk | grep -v 'etc/init' | awk '/delayed_job/ {print $2}'`.split("\n")
end
begin
send ARGV.first
ensure
@log.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.