Skip to content

Instantly share code, notes, and snippets.

@hakimkal
Forked from andruby/deploy.rb
Last active September 26, 2018 10:18
Show Gist options
  • Save hakimkal/8513700 to your computer and use it in GitHub Desktop.
Save hakimkal/8513700 to your computer and use it in GitHub Desktop.
Resque and Resque Scheduler Capistrano and rake recipes
def run_remote_rake(rake_cmd)
rake_args = ENV['RAKE_ARGS'].to_s.split(',')
cmd = "cd #{fetch(:latest_release)} && bundle exec #{fetch(:rake, "rake")} RAILS_ENV=#{fetch(:rails_env, "production")} #{rake_cmd}"
cmd += "['#{rake_args.join("','")}']" unless rake_args.empty?
run cmd
set :rakefile, nil if exists?(:rakefile)
end
namespace :resque_worker do
desc "Restart Resque Workers"
task :restart_workers, :roles => :worker do
run_remote_rake "resque:restart_workers"
end
end
after "deploy:restart", "resque_worker:restart_workers"
namespace :resque_scheduler do
desc "Restart Resque Scheduler"
task :restart_workers, roles: :worker do
run_remote_rake "resque:restart_scheduler"
#resque:scheduler
end
end
after "resque_worker:restart_workers","resque_scheduler:restart_workers"
require "resque/tasks"
require 'resque_scheduler/tasks'
task "resque:setup" => :environment
#do
# ENV['QUEUE'] = '*'
#end
namespace :resque do
task :setup => :environment do
Resque.before_fork = Proc.new{ActiveRecord::Base.establish_connection}
end
desc "Restart running workers"
task :restart_workers => :environment do
Rake::Task['resque:stop_workers'].invoke
Rake::Task['resque:start_workers'].invoke
end
desc "Quit running workers"
task :stop_workers => :environment do
stop_workers("resque")
end
desc "Start workers"
task :start_workers => :environment do
run_worker("*", "resque","resque:work",1)
end
desc "Restart running schedulers"
task :restart_scheduler => :environment do
Rake::Task['resque:stop_schedulers'].invoke
Rake::Task['resque:start_schedulers'].invoke
end
desc "Quit running schedulers"
task :stop_schedulers => :environment do
stop_workers("scheduler")
end
desc "Start Scheduler"
task :start_schedulers => :environment do
run_worker("*", "scheduler","resque:scheduler",1)
end
def store_pids(pids, mode,filename)
pids_to_store = pids
pids_to_store += read_pids(filename) if mode == :append
# Make sure the pid file is writable.
File.open(File.expand_path("tmp/pids/#{filename}", Rails.root), 'w') do |f|
f << pids_to_store.join(',')
end
end
def read_pids(filename)
pid_file_path = File.expand_path("tmp/pids/#{filename}", Rails.root)
return [] if ! File.exists?(pid_file_path)
File.open(pid_file_path, 'r') do |f|
f.read
end.split(',').collect {|p| p.to_i }
end
def stop_workers(filename)
pids = read_pids(filename)
if pids.empty?
puts "No workers to kill"
else
syscmd = "kill -s QUIT #{pids.join(' ')}"
puts "$ #{syscmd}"
`#{syscmd}`
store_pids([], :write,filename)
end
end
# Start a worker with proper env vars and output redirection
def run_worker(queue, filename, cmd,count = 1)
puts "Starting #{count} worker(s) with QUEUE: #{queue}"
if filename == "resque"
## make sure log/resque_err, log/resque_stdout are writable.
ops = {:pgroup => true, :err => [(Rails.root + "log/#{filename}_err").to_s, "a"],
:out => [(Rails.root + "log/#{filename}_stdout").to_s, "a"]}
env_vars = {"QUEUE" => queue.to_s, 'RAILS_ENV' => Rails.env.to_s}
else
ops = {:pgroup => true, :err => [(Rails.root + "log/#{filename}_err").to_s, "a"],
:out => [(Rails.root + "log/#{filename}_stdout").to_s, "a"]}
env_vars = {"BACKGROUND" => "1", 'RAILS_ENV' => Rails.env.to_s}
end
pids = []
count.times do
## Using Kernel.spawn and Process.detach because regular system() call would
## cause the processes to quit when capistrano finishes
pid = spawn(env_vars, "rake #{cmd}", ops)
Process.detach(pid)
pids << pid
end
store_pids(pids, :append,filename)
end
end
@hakimkal
Copy link
Author

Basically I modified the methods to comply to rails DRY as much as I can and hence used this start and stop my resque and scheduler rake tasks using capistrano.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment