Files
fnos-agent-checkin/AgentCheckin/cmd/main
T

100 lines
2.8 KiB
Bash
Raw Normal View History

#!/bin/bash
# AgentCheckin service control.
# The scheduler daemon runs as a detached background process and manages its
# own pidfile in the persistent data dir (/vol{n}/@appdata/AgentCheckin).
PYBIN=""
for candidate in \
/var/apps/python312/target/bin/python3 \
/vol1/@appcenter/python312/target/bin/python3 \
/var/apps/python311/target/bin/python3 \
/var/apps/python310/target/bin/python3 \
/usr/bin/python3 \
/usr/local/bin/python3; do
if [ -x "$candidate" ]; then PYBIN="$candidate"; break; fi
done
APPDEST="${TRIM_APPDEST:-$(cd "$(dirname "$0")/.." && pwd)}"
BACKEND_DIR="${APPDEST}/target/backend"
if [ ! -d "$BACKEND_DIR" ]; then
BACKEND_DIR="${APPDEST}/backend"
fi
DAEMON="${BACKEND_DIR}/daemon.py"
# Resolve the persistent data dir (mirrors backend/common.py).
if [ -n "${TRIM_PKGVAR}" ] && [ -d "${TRIM_PKGVAR}" ]; then
DATA_DIR="${TRIM_PKGVAR}"
else
DATA_DIR=""
for candidate in \
"${APPDEST}/../../@appdata/AgentCheckin" \
"${APPDEST}/var" \
"/var/apps/AgentCheckin/var" \
"/vol1/@appdata/AgentCheckin" \
"/vol2/@appdata/AgentCheckin"; do
if [ -d "$candidate" ]; then DATA_DIR="$candidate"; break; fi
done
if [ -z "$DATA_DIR" ]; then
DATA_DIR="${APPDEST}/../../@appdata/AgentCheckin"
fi
fi
PIDFILE="${DATA_DIR}/daemon.pid"
daemon_alive() {
[ -f "$PIDFILE" ] || return 1
local pid
pid="$(cat "$PIDFILE" 2>/dev/null)"
[ -n "$pid" ] || return 1
kill -0 "$pid" 2>/dev/null
}
case "$1" in
start)
if daemon_alive; then
echo "AgentCheckin daemon already running (pid $(cat "$PIDFILE"))"
exit 0
fi
if [ -z "$PYBIN" ]; then
echo "python3 runtime not found (install_dep_apps=python312)" > "${TRIM_TEMP_LOGFILE:-/dev/stderr}"
exit 1
fi
mkdir -p "$DATA_DIR"
AGENTCHECKIN_DATA_DIR="$DATA_DIR" setsid nohup "$PYBIN" "$DAEMON" >> "${DATA_DIR}/daemon.out" 2>&1 &
# wait for the daemon to write its pidfile (max ~5s)
for _ in 1 2 3 4 5 6 7 8 9 10; do
if daemon_alive; then
echo "AgentCheckin daemon started (pid $(cat "$PIDFILE"))"
exit 0
fi
sleep 0.5
done
echo "AgentCheckin daemon failed to start; see ${DATA_DIR}/daemon.out" > "${TRIM_TEMP_LOGFILE:-/dev/stderr}"
exit 1
;;
stop)
if daemon_alive; then
pid="$(cat "$PIDFILE")"
kill "$pid" 2>/dev/null
for _ in 1 2 3 4 5 6 7 8 9 10; do
kill -0 "$pid" 2>/dev/null || break
sleep 0.5
done
kill -9 "$pid" 2>/dev/null
echo "AgentCheckin daemon stopped (pid $pid)"
else
echo "AgentCheckin daemon not running"
fi
rm -f "$PIDFILE"
exit 0
;;
status)
if daemon_alive; then
exit 0
fi
exit 3
;;
*)
exit 1
;;
esac