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:
@@ -37,4 +37,4 @@ if [[ "${cmd}" == "clear" ]]; then
|
||||
else
|
||||
# this make not succeed if volume is a mount point
|
||||
rmdir "${volume_dir}" || true
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -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}"
|
||||
|
||||
|
||||
@@ -19,7 +19,11 @@ fi
|
||||
|
||||
task_id="$1"
|
||||
|
||||
service_name="cloudron-task-${task_id}"
|
||||
|
||||
systemctl kill --signal=SIGTERM "${service_name}"
|
||||
if [[ "${task_id}" == "all" ]]; then
|
||||
systemctl list-units --full --no-legend cloudron-task-* # just to show who was running
|
||||
systemctl kill --signal=SIGTERM cloudron-task-* || true
|
||||
else
|
||||
readonly service_name="cloudron-task-${task_id}"
|
||||
systemctl kill --signal=SIGTERM "${service_name}" || true
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user