Skip to content

Instantly share code, notes, and snippets.

@it-can
Created December 21, 2014 10:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save it-can/31059375f53719b65f4a to your computer and use it in GitHub Desktop.
Save it-can/31059375f53719b65f4a to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Queue manager script, to make sure the required number of worker instances
# are running. Schedule this in cron to make sure any crashed workers will
# be restarted.
#
# Get the full path to ourselfs
SCRIPTPATH="$( dirname "$( which "$0" )" )"
# how many workers do we need to start?
if [ $# -gt 0 ]; then
WORKERS=$1
else
WORKERS=1;
fi
# where does the php binary live?
PHP=`which php`
# command that starts the worker
COMMAND="$SCRIPTPATH/../../../oil refine queueworker"
# loop to process the worker instantiation
while :
do
# bail out if we have started enough workers
PROCESS_COUNT=`pgrep -f "$COMMAND" | wc -l`
if [ $PROCESS_COUNT -eq $WORKERS ]; then
exit 0
fi
# too many workers running?
if [ $PROCESS_COUNT -gt $WORKERS ]; then
# kill a worker off nicely
PROCESS=`pgrep -f -n "$COMMAND"`
kill -15 $PROCESS
sleep 1
else
# start a new worker
$PHP $COMMAND &
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment