deblan.io-murph/bin/messenger
2023-09-23 00:03:48 +02:00

155 lines
2.6 KiB
Bash
Executable file

#!/bin/sh
set -eu
usage() {
printf "Usage: %s [-l DEBUG_LEVEL] [-h] start|stop|restart\n" "$0"
}
help() {
cat << EOH
SYNOPSIS
$0 [-l DEBUG_LEVEL] [-h] -a start|stop|restart
DESCRIPTION
$0 manages symfony messenger
OPTIONS
-h Show this help
-l debug|info|notice|warning|error
Debug level
-a start|stop|restart
EOH
}
on_interrupt() {
log -l notice ""
log -l notice "Process aborted!"
exit 130
}
start_messenger() {
started=0
pid=
if [ -f "$pid_file" ]; then
pid="$(cat "$pid_file")"
fi
if [ -n "$pid" ]; then
if [ -d "/proc/$pid" ]; then
log -t -l warning "Already running"
started=1
fi
fi
if [ "$started" -eq 0 ]; then
nohup php bin/console messenger:consume 2>/dev/null >/dev/null &
echo -n $! > "$pid_file"
log -t -l notice "Started"
fi
}
stop_messenger() {
if [ -f "$pid_file" ]; then
pid="$(cat "$pid_file")"
fi
if [ -n "$pid" ]; then
if [ ! -d "/proc/$pid" ]; then
log -t -l warning "Not started"
else
kill -9 "$pid"
log -t -l notice "Stopped"
fi
rm "$pid_file"
else
log -t -l warning "Not started"
fi
}
main() {
cd "$(dirname "0")"
pid_file=var/messenger.pid
while getopts "l:ha:" option; do
case "${option}" in
h) help; exit 0;;
l) LOG_VERBOSE="$OPTARG";;
a) ACTION="$OPTARG";;
?) log -l error "$(usage)"; exit 1;;
esac
done
if [ "$ACTION" = "start" ]; then
start_messenger
elif [ "$ACTION" = "stop" ]; then
stop_messenger
elif [ "$ACTION" = "restart" ]; then
stop_messenger
start_messenger
fi
# log [-t] [-l debug|info|notice|warning|error] message
exit 0
}
log() {
LOG_VERBOSE="${LOG_VERBOSE:-info}"
LEVEL=info
TIME=
while getopts "tl:" option; do
case "${option}" in
l) LEVEL="$OPTARG"; shift $((OPTIND-1));;
t) TIME="$(printf "[%s] " "$(date +'%Y-%m-%dT%H:%M:%S.%s')")"; shift $((OPTIND-1));;
*) exit 1;;
esac
done
if [ -t 2 ] && [ -z "${NO_COLOR-}" ]; then
case "${LEVEL}" in
debug) COLOR="$(tput setaf 3)";;
notice) COLOR="$(tput setaf 4)";;
warning) COLOR="$(tput setaf 5)";;
error) COLOR="$(tput setaf 1)";;
*) COLOR="$(tput sgr0)";;
esac
fi
case "${LEVEL}" in
debug) LEVEL=100;;
notice) LEVEL=250;;
warning) LEVEL=300;;
error) LEVEL=400;;
*) LEVEL=200;;
esac
case "${LOG_VERBOSE}" in
debug) LOG_VERBOSE_VALUE=100;;
notice) LOG_VERBOSE_VALUE=250;;
warning) LOG_VERBOSE_VALUE=300;;
error) LOG_VERBOSE_VALUE=400;;
*) LOG_VERBOSE_VALUE=200;;
esac
if [ $LEVEL -ge $LOG_VERBOSE_VALUE ]; then
printf "%s\n" "$*" | while IFS='' read -r LINE; do
printf "%s%s%s\n" "${COLOR:-}" "${TIME:-}" "$LINE" >&2
done
fi
}
trap on_interrupt INT
main "$@"