Merge pull request #6403 from qzhuyan/backport/william/4.3/systemd-restart-on-failure

Backport/william/4.3/systemd restart on failure
This commit is contained in:
William Yang 2021-12-09 08:38:39 +01:00 committed by GitHub
commit f72002949b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 177 deletions

View File

@ -135,23 +135,6 @@ running_test(){
pytest -v /paho-mqtt-testing/interoperability/test_client/V5/test_connect.py::test_basic
# shellcheck disable=SC2009 # pgrep does not support Extended Regular Expressions
emqx stop || kill "$(ps -ef | grep -E '\-progname\s.+emqx\s' |awk '{print $2}')"
if [ "$(sed -n '/^ID=/p' /etc/os-release | sed -r 's/ID=(.*)/\1/g' | sed 's/"//g')" = ubuntu ] \
|| [ "$(sed -n '/^ID=/p' /etc/os-release | sed -r 's/ID=(.*)/\1/g' | sed 's/"//g')" = debian ] ;then
service emqx start || ( tail /var/log/emqx/emqx.log.1 && exit 1 )
IDLE_TIME=0
while ! emqx_ctl status | grep -E 'Node\s.*@.*\sis\sstarted'
do
if [ $IDLE_TIME -gt 10 ]
then
echo "emqx service error"
exit 1
fi
sleep 10
IDLE_TIME=$((IDLE_TIME+1))
done
service emqx stop
fi
}
relup_test(){

View File

@ -13,6 +13,7 @@ TARGET_PKG := $(EMQX_NAME)-$(PKG_VSN)-otp$(OTP_VSN)-$(SYSTEM)-$(ARCH)
.PHONY: all
all: | $(BUILT)
cp -r debian $(SRCDIR)/
cp emqx.service $(SRCDIR)/debian/${EMQX_NAME}.service
sed -i "s#<DATETIME>#$(shell date -u '+%a, %d %b %Y %T %z')#g" $(SRCDIR)/debian/changelog
sed -i "s#<VERSION>#$(PKG_VSN)#g" $(SRCDIR)/debian/changelog
sed -i "s/emqx-pkg/$(EMQX_NAME)-pkg/g" $(SRCDIR)/debian/rules; \

View File

@ -1,150 +0,0 @@
#! /bin/bash
### BEGIN INIT INFO
# Provides: emqx
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Erlang MQTT Broker
# Description: EMQX, a distributed, massively scalable, highly extensible MQTT message broker written in Erlang/OT
### END INIT INFO
NAME=emqx
DAEMON=/usr/bin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
. /lib/lsb/init-functions
# `service` strips all environmental VARS so
# if no HOME was set in /etc/default/$NAME then set one here
# to the data directory for erlexec's sake
if [ -z "$HOME" ]; then
export HOME=/var/lib/emqx
fi
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
# Startup with the appropriate user
start-stop-daemon --start \
--name emqx \
--user emqx \
--exec $DAEMON -- start \
|| return 2
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Identify the erts directory
ERTS_PATH=`$DAEMON ertspath`
# Attempt a clean shutdown.
$DAEMON stop
# waiting stop done sleep 5
sleep 5
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
# Make sure it's down by using a more direct approach
start-stop-daemon --stop \
--quiet \
--retry=TERM/30/KILL/5 \
--user emqx \
--exec $ERTS_PATH/run_erl
return $?
}
#
# Function that graceful reload the daemon/service
#
do_reload() {
# Restart the VM without exiting the process
$DAEMON restart && return $? || return 2
}
# Checks the status of a node
do_status() {
$DAEMON ping && echo $"$NAME is running" && return 0
echo $"$NAME is stopped" && return 2
}
case "$1" in
start)
log_daemon_msg "Starting $NAME"
$DAEMON ping >/dev/null 2>&1 && echo $"$NAME is already running" && exit 0
do_start
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1
exit 1
;;
esac
;;
stop)
log_daemon_msg "Stopping $NAME"
do_stop
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1
exit 1
;;
esac
;;
ping)
# See if the VM is alive
$DAEMON ping || exit $?
;;
reload|force-reload)
log_daemon_msg "Reloading $NAME"
do_reload
ES=$?
log_end_msg $ES
exit $ES
;;
restart)
log_daemon_msg "Restarting $NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 && exit 1 ;; # Old process is still running
*) log_end_msg 1 && exit 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1 && exit 1
;;
esac
;;
status)
do_status && exit 0 || exit $?
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|ping|restart|force-reload|status}" >&2
exit 3
;;
esac

View File

@ -5,9 +5,6 @@
set -e
# install startup script
update-rc.d emqx defaults >/dev/null
# create group
if ! getent group emqx >/dev/null; then
addgroup --system emqx
@ -38,6 +35,12 @@ chmod -R 0755 /usr/lib/emqx/bin
ln -s /usr/lib/emqx/bin/emqx /usr/bin/emqx
ln -s /usr/lib/emqx/bin/emqx_ctl /usr/bin/emqx_ctl
if systemctl status --no-pager; then
systemctl enable emqx;
else
echo "systemd is not in use, skip enable emqx"
fi
case "$1" in
configure)
;;

View File

@ -23,9 +23,6 @@ case "$1" in
purge)
rm -f /etc/default/emqx
# ensure we remove the rc.d scripts installed by postinst
update-rc.d emqx remove >/dev/null
if [ -d /var/lib/emqx ]; then
rm -r /var/lib/emqx
fi
@ -58,6 +55,11 @@ case "$1" in
if [ -d /usr/lib/emqx ]; then
rm -r /usr/lib/emqx
fi
if systemctl status --no-pager; then
systemctl disable emqx;
rm -f /etc/systemd/emqx.service
fi
;;
remove)

View File

@ -46,8 +46,10 @@ install: build
cp -R releases debian/emqx/usr/lib/emqx
cp -R etc/* debian/emqx/etc/emqx
cp -R data/* debian/emqx/var/lib/emqx
install -m755 debian/init.script debian/emqx/etc/init.d/emqx
install -d debian/emqx/lib/systemd/system
install -p -m0644 debian/emqx.service debian/emqx/lib/systemd/system/emqx.service
dh_shlibdeps
# We have nothing to do by default.

View File

@ -0,0 +1 @@
../emqx.service

View File

@ -0,0 +1,17 @@
[Unit]
Description=emqx daemon
After=network.target
[Service]
User=emqx
Group=emqx
Type=forking
Environment=HOME=/var/lib/emqx
ExecStart=/usr/bin/emqx start
LimitNOFILE=1048576
ExecStop=/usr/bin/emqx stop
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target

View File

@ -7,9 +7,11 @@ User=emqx
Group=emqx
Type=forking
Environment=HOME=/var/lib/emqx
ExecStart=/bin/sh /usr/bin/emqx start
ExecStart=/usr/bin/emqx start
LimitNOFILE=1048576
ExecStop=/bin/sh /usr/bin/emqx stop
ExecStop=/usr/bin/emqx stop
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target