#!/bin/sh # Start Garmin Health Lab. Safe to run repeatedly: an already-running # instance is stopped first. Intended for DSM Task Scheduler (boot-up). APP="$(cd "$(dirname "$0")/.." && pwd)" cd "$APP/backend" || exit 1 GUNICORN="$APP/backend/.venv/bin/gunicorn" # Stop whatever is already running, then WAIT for the port to actually be # free. Killing only the pid in app.pid left orphaned workers holding :8124; # the new master then started, reported success, and served nothing — the # site was down while every log line looked normal. "$APP/deploy/stop.sh" >/dev/null 2>&1 i=0 while [ $i -lt 20 ]; do pgrep -f "$GUNICORN" >/dev/null 2>&1 || break sleep 1 i=$((i + 1)) done pkill -9 -f "$GUNICORN" 2>/dev/null sleep 1 mkdir -p "$APP/logs" # --timeout 300: an AI generation against the reasoning model can run for # minutes, and gunicorn kills a worker that looks stuck before then. nohup "$GUNICORN" \ --workers 2 --threads 4 --timeout 300 \ --bind 0.0.0.0:8124 \ --access-logfile "$APP/logs/access.log" \ --error-logfile "$APP/logs/error.log" \ wsgi:app > "$APP/logs/stdout.log" 2>&1 & echo $! > "$APP/app.pid" # Confirm it is actually serving rather than just running. i=0 while [ $i -lt 25 ]; do if curl -sf -m 2 -o /dev/null http://127.0.0.1:8124/api/health/status; then echo "started pid $(cat "$APP/app.pid") on :8124" exit 0 fi sleep 1 i=$((i + 1)) done echo "FAILED to serve on :8124 — see $APP/logs/error.log" >&2 exit 1