Skip to content

Instantly share code, notes, and snippets.

@mrenvoize
Created February 8, 2016 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrenvoize/87389f99609f1ac2b13f to your computer and use it in GitHub Desktop.
Save mrenvoize/87389f99609f1ac2b13f to your computer and use it in GitHub Desktop.
init.d script for starting multiple minion workers (Mojolicious)
#!/bin/bash
### BEGIN INIT INFO
# Provides: rebus-minion
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Provides Minion Workers for rebus:list jobs
# Description: This script with start/stop minion worker
# processes for each enabled rebus:list instance
### END INIT INFO
# Author: Martin Renvoize <martin.renvoize@ptfs-europe.com>
# Add local perl to $PATH
PATH=/home/rebus/.plenv/versions/5.20.2/bin/perl5.20.2:$PATH
DESC="Minion worker services"
DAEMON=/home/rebus/rebus-list/script/rebus
RUNDIR=/var/run/rebus-minion
test -x $DAEMON || exit 0
set -e
FILES=(/etc/rebus/list/*.conf)
if [ -r "${FILES[0]}" ]
then
CONFIGS=()
for FILE in "${FILES[@]}"
do
# remove prefix
NAME=${FILE#/etc/rebus/list}
# remove suffix
NAME=${NAME%.conf}
# check for optional second parameter
if [ "$#" -ne 2 ]
then
# add to config array
CONFIGS+=($NAME)
elif [ "$2" == "$NAME" ]
then
# just one minion
CONFIGS=($NAME)
break
fi
done
if [ ${#CONFIGS[@]} == 0 ];
then
echo "Config missing for: $2" >&2
exit 1
fi
else
echo "No configs found" >&2
exit 1
fi
CONFIG_NUM=${#CONFIGS[@]}
for ((i=0; i < $CONFIG_NUM; i++))
do
NAME=${CONFIGS[${i}]}
PIDFILE1="${RUNDIR}/${NAME}-default.pid"
PIDFILE2="${RUNDIR}/${NAME}-buffer.pid"
case "$1" in
start)
echo -n "Starting $DESC: "
mkdir -p $RUNDIR
touch "$PIDFILE1"
touch "$PIDFILE2"
chown rebus:rebus "$RUNDIR" "$PIDFILE1" "$PIDFILE2"
chmod 755 $RUNDIR
if [ -n "$ULIMIT" ]
then
ulimit -n "$ULIMIT"
fi
if start-stop-daemon --start --quiet --background --umask 007 --pidfile "$PIDFILE1" --chuid rebus:rebus --exec /usr/bin/env MOJO_CONFIG="/etc/rebus/list/${NAME}.conf" $DAEMON -- minion worker -m production -j 4 -q default
then
echo "$NAME-default"
else
echo "failed"
fi
if start-stop-daemon --start --quiet --background --umask 007 --pidfile "$PIDFILE2" --chuid rebus:rebus --exec /usr/bin/env MOJO_CONFIG="/etc/rebus/list/${NAME}.conf" $DAEMON -- minion worker -m production -j 1 -q buffer
then
echo "$NAME-buffer"
else
echo "failed"
fi
;;
stop)
echo -n "Stopping $DESC: "
if start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo "$PIDFILE1" --exec $DAEMON
then
echo "$NAME-default"
else
echo "failed"
fi
rm -f "$PIDFILE1"
if start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo "$PIDFILE2" --exec $DAEMON
then
echo "$NAME-buffer"
else
echo "failed"
fi
rm -f "$PIDFILE2"
sleep 1
;;
restart|force-reload)
ARGS=($@)
CONFIG=${ARGS[@]:1}
${0} stop $CONFIG
${0} start $CONFIG
exit 0;
;;
status)
echo -n "$DESC ($NAME) is "
if start-stop-daemon --stop --quiet --signal 0 --pidfile "$PIDFILE1" --exec $DAEMON
then
echo "default worker running"
else
echo "default worker not running"
fi
if start-stop-daemon --stop --quiet --signal 0 --pidfile "$PIDFILE2" --exec $DAEMON
then
echo "buffer worker running"
else
echo "buffer worker not running"
fi
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload|status" >&2
exit 1
;;
esac
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment