Kill all tasks on shutdown and startup

BindsTo will kill all the tasks when systemctl stop box is executed.
But when restarted, it keeps the tasks running. Because of this behavior,
we kill the tasks on startup and stop of the box code.
This commit is contained in:
Girish Ramakrishnan
2020-08-06 22:04:46 -07:00
parent 648d42dfe4
commit b1b6f70118
9 changed files with 43 additions and 31 deletions

View File

@@ -2,6 +2,9 @@
set -eu -o pipefail
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly task_worker="${script_dir}/../taskworker.js"
if [[ ${EUID} -ne 0 ]]; then
echo "This script should be run as root." > /dev/stderr
exit 1
@@ -21,24 +24,20 @@ readonly task_id="$1"
readonly logfile="$2"
readonly nice="$3"
readonly id=$(id -u yellowtent)
readonly service_name="cloudron-task-${task_id}"
systemctl reset-failed "${service_name}" || true
# keep the env vars in sync with box.service
systemd-run --unit "${service_name}" \
-p BindsTo=box.service \
--pipe \
--wait \
--property=OOMScoreAdjust=-1000 \
--nice "${nice}" \
--uid=${id} \
--gid=${id} \
-E HOME=/home/yellowtent \
-E USER=yellowtent \
-E DEBUG=box:* \
-E BOX_ENV=cloudron \
-E NODE_ENV=production \
/home/yellowtent/box/src/taskworker.js "${task_id}" "${logfile}"
readonly id=$(id -u $SUDO_USER)
# Note: BindsTo will kill this task when the box is stopped. but will not kill this task when restarted!
# For this reason, we have code to kill the tasks both on shutdown and startup
if [[ "$BOX_ENV" == "cloudron" ]]; then
binds_to="-p BindsTo=box.service"
else
binds_to=""
fi
systemd-run --unit "${service_name}" --pipe --wait --property=OOMScoreAdjust=-1000 --nice "${nice}" --uid=${id} --gid=${id} \
${binds_to} -E HOME=${HOME} -E USER=${SUDO_USER} -E DEBUG=${DEBUG} -E BOX_ENV=${BOX_ENV} -E NODE_ENV=production \
"${task_worker}" "${task_id}" "${logfile}"