2020-08-06 14:36:25 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
set -eu -o pipefail
|
|
|
|
|
|
2020-08-06 22:04:46 -07:00
|
|
|
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
|
readonly task_worker="${script_dir}/../taskworker.js"
|
|
|
|
|
|
2020-08-06 14:36:25 -07:00
|
|
|
if [[ ${EUID} -ne 0 ]]; then
|
|
|
|
|
echo "This script should be run as root." > /dev/stderr
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
|
|
|
echo "No arguments supplied"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ "$1" == "--check" ]]; then
|
|
|
|
|
echo "OK"
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
readonly task_id="$1"
|
|
|
|
|
readonly logfile="$2"
|
|
|
|
|
readonly nice="$3"
|
2024-02-19 17:01:54 +01:00
|
|
|
readonly memory_limit_mb="$4" # SI units
|
2024-07-19 13:05:09 +02:00
|
|
|
readonly oom_score_adjust="$5"
|
2020-08-06 14:36:25 -07:00
|
|
|
|
2025-06-16 18:19:30 +02:00
|
|
|
readonly id=$(id -u $SUDO_USER)
|
|
|
|
|
|
2020-08-09 07:07:19 -07:00
|
|
|
readonly service_name="box-task-${task_id}"
|
2020-08-06 14:36:25 -07:00
|
|
|
|
2025-06-16 18:19:30 +02:00
|
|
|
systemctl reset-failed "${service_name}" 2>/dev/null || true
|
2020-08-08 11:10:02 -07:00
|
|
|
|
2025-06-16 14:20:07 +02:00
|
|
|
options="-p TimeoutStopSec=10s -p MemoryMax=${memory_limit_mb}M -p OOMScoreAdjust=${oom_score_adjust} --wait"
|
2020-08-07 00:15:24 -07:00
|
|
|
|
2025-07-17 08:30:51 +02:00
|
|
|
# systemd-run is used to create resource limited tasks. such tasks are in separate cgroup and won't get affected by box start/stop.
|
|
|
|
|
# However, we want task to not run when box code is stopped. If dashboard is down, nothing should run in the background.
|
|
|
|
|
# Besides, if tasks do continue running, box code needs complex state reconcilation logic on start up.
|
|
|
|
|
# To stop tasks on box stop, we could use BindsTo=box.service. While this stops all tasks on systemctl stop, it restarts tasks on systemctl restart!
|
2025-06-17 22:30:34 +02:00
|
|
|
# Another approach was to use --scope but this is incompatible with --wait (and then we have to start polling status to get exit code)
|
2025-07-17 08:30:51 +02:00
|
|
|
# So, the implemented solution is to kill the tasks manually on exit by handing SIGTERM with KillMode=mixed which calls stopAllTasks
|
|
|
|
|
# As an additional precaution, box start also calls stopAllTasks
|
2025-06-17 22:30:34 +02:00
|
|
|
|
|
|
|
|
# it seems systemd-run does not return the exit status of the process despite --wait but atleast it waits
|
|
|
|
|
if ! systemd-run --unit "${service_name}" --wait --uid=${id} --gid=${id} \
|
|
|
|
|
-p TimeoutStopSec=2s -p MemoryMax=${memory_limit_mb}M -p OOMScoreAdjust=${oom_score_adjust} --nice "${nice}" \
|
|
|
|
|
--setenv HOME=${HOME} --setenv USER=${SUDO_USER} --setenv DEBUG=box:* --setenv BOX_ENV=${BOX_ENV} --setenv NODE_ENV=production \
|
|
|
|
|
"${task_worker}" "${task_id}" "${logfile}"; then
|
2022-11-23 12:53:21 +01:00
|
|
|
echo "Service ${service_name} failed to run" # this only happens if the path to task worker itself is wrong
|
2022-11-02 22:39:25 +01:00
|
|
|
fi
|
|
|
|
|
|
2025-07-17 09:53:29 +02:00
|
|
|
# ExecMainCode=0 means killed by signal in ExecMainStatus. ExecMainCode=1 means exited cleanly with code in ExecMainStatus
|
2022-11-02 22:39:25 +01:00
|
|
|
exit_code=$(systemctl show "${service_name}" -p ExecMainCode | sed 's/ExecMainCode=//g')
|
2025-07-17 09:53:29 +02:00
|
|
|
exit_status=$(systemctl show "${service_name}" -p ExecMainStatus | sed 's/ExecMainStatus=//g')
|
2020-08-08 11:10:02 -07:00
|
|
|
|
2025-07-17 09:53:29 +02:00
|
|
|
echo "Service ${service_name} finished with exit code ${exit_code} and status ${exit_status}"
|
|
|
|
|
exit "${exit_status}"
|