Skip to content

Instantly share code, notes, and snippets.

@mikz
Last active January 8, 2016 16:36
Show Gist options
  • Save mikz/3b0bc2ac97bfdf6263b4 to your computer and use it in GitHub Desktop.
Save mikz/3b0bc2ac97bfdf6263b4 to your computer and use it in GitHub Desktop.
Upstart config for resque workers
manual
instance ${N}
kill timeout 10800 # 3 hours in seconds
setuid user
stop on stopping resque
env N
env QUEUE="*"
env TASK="environment resque:work"
env RAKE="/usr/local/bin/rake"
env BUNDLER="/usr/local/bin/bundle"
env COUNT=1
env RAILS_ENV="staging"
env DEFAULTFILE=/etc/default/resque
chdir /home/user/system_staging/current
script
if [ -f "$DEFAULTFILE" ]; then
. "$DEFAULTFILE"
fi
exec ${BUNDLER} exec ${RAKE} ${TASK}
end script
post-start script
# wait for resque to boot and change its command name to resque-version
while [ -z "${pid}" ] || ! ps -p ${pid} -o args | grep '^resque-'
do
pid=$(status ${UPSTART_JOB} N=${UPSTART_INSTANCE} | grep 'start/' | awk '{print $NF}')
sleep 1
done
end script
#!upstart
#manual
start on startup
# stop on stopping resque-worker
env QUEUE_1="*"
env QUEUE_2="*"
env QUEUE_3="*"
env QUEUE_4="*"
env QUEUE_5="*"
env QUEUE_6="default"
env WORKERS=6
kill timeout 60 # resque workers have higher timeout
script
start_worker () {
start resque-worker N=${1} QUEUE=$(eval echo \$QUEUE_${1}) || :
}
start_workers () {
for n in $(seq ${WORKERS})
do
start_worker ${n} &
done
wait
}
restart_worker () {
restart resque-worker N=${1}
}
stop_worker () {
stop resque-worker N=${1} || :
}
reload_workers () {
for n in $(seq ${WORKERS})
do
restart_worker ${n} &
done
wait
}
stop_workers () {
for n in $(seq ${WORKERS})
do
stop_worker ${n} &
done
wait
exit
}
trap reload_workers HUP
trap stop_workers TERM
start_workers
while :
do
sleep 1 # just 1, so it can trap signals
done
end script
post-start script
while [ $(initctl list | grep resque-worker | grep start/running | wc -l) -ne ${WORKERS} ]
do
sleep 1 # wait until all the workers start
done
end script
post-stop script
for n in `initctl list|grep "^resque-worker "|awk '{print $2}'|tr -d ')'|tr -d '('`
do
stop resque-worker N=$n &
done
wait
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment