Skip to content
This repository has been archived by the owner on Oct 17, 2021. It is now read-only.

Enable Support for Shared Deployment Environment #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions lib/capistrano-resque/capistrano_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ def self.load_into(capistrano_config)
_cset(:resque_environment_task, false)
_cset(:resque_log_file, "/dev/null")
_cset(:resque_pid_path) { File.join(shared_path, 'tmp', 'pids') }
_cset(:enable_shared_users, false)

def rails_env
fetch(:resque_rails_env, fetch(:rails_env, "production"))
end

def maybe_sudo
!!fetch(:enable_shared_users) ? try_sudo : ''
end

def output_redirection
">> #{fetch(:resque_log_file)} 2>> #{fetch(:resque_log_file)}"
end
Expand All @@ -36,6 +41,13 @@ def for_each_workers(&block)
end
end

def run_and_rescue(command)
run(command)
true
rescue Capistrano::CommandError
false
end

def status_command
"if [ -e #{fetch(:resque_pid_path)}/resque_work_1.pid ]; then \
for f in $(ls #{fetch(:resque_pid_path)}/resque_work*.pid); \
Expand All @@ -51,21 +63,6 @@ def start_command(queue, pid)
resque:work #{output_redirection}"
end

def stop_command
"if [ -e #{fetch(:resque_pid_path)}/resque_work_1.pid ]; then \
for f in `ls #{fetch(:resque_pid_path)}/resque_work*.pid`; \
do \
if kill -0 `cat $f`> /dev/null 2>&1; then \
kill -s #{resque_kill_signal} `cat $f` \
&& rm $f \
;else \
echo 'Resque was not running, cleaning up stale PID file' \
&& rm $f \
;fi \
;done \
;fi"
end

def status_scheduler
"if [ -e #{fetch(:resque_pid_path)}/scheduler.pid ]; then \
ps -p $(cat #{fetch(:resque_pid_path)}/scheduler.pid) | sed -n 2p \
Expand All @@ -80,7 +77,7 @@ def start_scheduler(pid)

def stop_scheduler(pid)
"if [ -e #{pid} ]; then \
kill -s #{resque_kill_signal} $(cat #{pid}) ; rm #{pid} \
#{maybe_sudo} kill -s #{resque_kill_signal} $(cat #{pid}) ; #{maybe_sudo} rm #{pid} \
;fi"
end

Expand Down Expand Up @@ -123,7 +120,20 @@ def create_pid_path
# CONT - Start to process new jobs again after a USR2 (resume)
desc "Quit running Resque workers"
task :stop, :roles => lambda { workers_roles() }, :on_no_matching_servers => :continue do
run(stop_command)
if run_and_rescue "[ -e #{fetch(:resque_pid_path)}/resque_work_1.pid ]"
pids = capture("ls -1 #{fetch(:resque_pid_path)}/resque_work*.pid").lines.map(&:chomp)
pids.each do |pid_file|
pid = capture("cat #{pid_file}")
if run_and_rescue "#{maybe_sudo} kill -0 #{pid}"
run("#{maybe_sudo} kill -s #{fetch(:resque_kill_signal)} #{pid} && #{maybe_sudo} rm #{pid_file}")
else
puts "Process #{pid} from #{pid_file} is not running, cleaning up stale PID file"
run("#{maybe_sudo} rm #{pid_file}")
end
end
else
puts "No resque PID files found"
end
end

desc "Restart running Resque workers"
Expand Down