Skip to content

Instantly share code, notes, and snippets.

@TrevorS
Created February 27, 2014 16:24
Show Gist options
  • Save TrevorS/9253448 to your computer and use it in GitHub Desktop.
Save TrevorS/9253448 to your computer and use it in GitHub Desktop.
Add Resque and Resque scheduler rake tasks.
require 'resque/tasks'
require 'resque_scheduler/tasks'
def run_worker(queue, count = 1)
puts "Starting #{count} Resque worker(s) with QUEUE: #{queue}."
ops = { pgroup: true, err: [Rails.root.join('log', 'workers_error.log'), 'a'],
out: [Rails.root.join('log', 'workers.log'), 'a']}
env_vars = { 'QUEUE' => queue.to_s }
count.times do
pid = spawn(env_vars, 'rake resque:work', ops)
Process.detach(pid)
end
end
def get_worker_pids(queue)
# make sure we are only getting workers that are working on our queues
workers = Resque.workers.select { |w| w.queues.include?(queue) }
# a hack to grab only workers pids and not accidentally grab capistrano pids
pids = workers.map { |w| w.to_s.sub /.+:(\d+)[-:].+/, '\1' }
end
def run_scheduler
puts 'Starting the Resque Scheduler.'
env_vars = { 'BACKGROUND' => '1',
'PIDFILE' => Rails.root.join('tmp', 'pids', 'resque_scheduler.pid').to_s,
'VERBOSE' => '1' }
ops = { pgroup: true, err: [Rails.root.join('log', 'scheduler_error.log').to_s, 'a'],
out: [Rails.root.join('log', 'scheduler.log').to_s, 'a'] }
pid = spawn(env_vars, 'rake resque:scheduler', ops)
Process.detach(pid)
end
namespace :resque do
task setup: :environment do
require 'resque'
require 'resque_scheduler'
schedule = Rails.root.join('config', 'resque_schedule.yml')
Resque.schedule = YAML.load_file(schedule) if schedule.file?
end
# the list of worker queues to start and how many
@workers = [
{ name: 'queue_1', workers: 3 },
{ name: 'queue_2', workers: 4 },
{ name: 'queue_3', workers: 5 }
]
desc 'Restart running workers'
task restart_workers: :environment do
Rake::Task['resque:stop_workers'].invoke
Rake::Task['resque:start_workers'].invoke
end
desc 'Stop running workers'
task stop_workers: :environment do
queues = @workers.map { |w| w[:name] }
pids = Array.new
queues.each do |q|
pids.concat(get_worker_pids(q))
end
if pids.empty?
puts 'No workers to kill.'
else
syscmd = "kill -s QUIT #{pids.join(' ')}"
puts "Running syscmd: #{syscmd}"
system(syscmd)
end
end
desc 'Start workers'
task start_workers: :environment do
@workers.each { |w| run_worker(w[:name], w[:workers]) }
end
desc 'Restart scheduler'
task restart_scheduler: :environment do
Rake::Task['resque:stop_scheduler'].invoke
Rake::Task['resque:start_scheduler'].invoke
end
desc 'Quit scheduler'
task stop_scheduler: :environment do
pidfile = Rails.root.join('tmp', 'pids', 'resque_scheduler.pid')
if !File.exists?(pidfile)
puts 'Scheduler not running.'
else
pid = File.read(pidfile).to_i
# check if the scheduler is still running before killing it
syscmd = "ps aux | grep #{pid} | grep -v grep"
if system(syscmd)
syscmd = "kill -s QUIT #{pid}"
puts "Running syscmd: #{syscmd}"
system(syscmd)
end
FileUtils.rm_f(pidfile)
end
end
desc 'Start scheduler'
task start_scheduler: :environment do
run_scheduler
end
desc 'Reload schedule'
task reload_schedule: :environment do
pidfile = Rails.root.join('tmp', 'pids', 'resque_scheduler.pid')
if !File.exists?(pidfile)
puts 'Scheduler not running'
else
pid = File.read(pidfile).to_i
syscmd = "kill -s USR2 #{pid}"
puts "Running syscmd: #{syscmd}"
system(syscmd)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment