-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·83 lines (63 loc) · 3.11 KB
/
run.sh
File metadata and controls
executable file
·83 lines (63 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env bash
# Starts the now-playing app under screen on port HTTP_PORT (default 9393).
# Safe to run repeatedly — stops any existing session and starts fresh.
set -euo pipefail
SCREEN_NAME="now-playing"
HOST="127.0.0.1"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$SCRIPT_DIR/.env"
LOG_FILE="$SCRIPT_DIR/log/stream.log"
ERROR_FILE="$SCRIPT_DIR/log/err.log"
# Resolve HTTP_PORT from .env if available, fall back to 9393
PORT="$(grep -m1 '^HTTP_PORT=' "$ENV_FILE" 2>/dev/null | cut -d= -f2 | tr -d '[:space:]')"
PORT="${PORT:-9393}"
# ── helpers ──────────────────────────────────────────────────────────────────
green() { printf '\e[32m%s\e[0m\n' "$*"; }
yellow() { printf '\e[33m%s\e[0m\n' "$*"; }
red() { printf '\e[31m%s\e[0m\n' "$*"; }
is_screen_running() {
screen -list | grep -q "\.${SCREEN_NAME}[[:space:]]"
}
is_port_bound() {
ss -tlnp "sport = :${PORT}" 2>/dev/null | grep -q ":${PORT}"
}
is_healthy() {
curl -sf "http://${HOST}:${PORT}/api/health" -o /dev/null --max-time 3
}
# ── stop existing session (restart on re-run) ─────────────────────────────────
if is_screen_running; then
yellow "⚠ Restarting now-playing (stopping existing screen session)…"
screen -S "$SCREEN_NAME" -X quit 2>/dev/null || true
sleep 1
fi
if is_port_bound; then
red "✗ Port ${PORT} is in use by another process. Free it first:"
ss -tlnp "sport = :${PORT}"
exit 1
fi
# ── ensure log dir exists ─────────────────────────────────────────────────────
mkdir -p "$SCRIPT_DIR/log"
# ── build client (production bundle for server static hosting) ───────────────
echo "Building client…"
(cd "$SCRIPT_DIR" && npm run build:client)
# ── launch ────────────────────────────────────────────────────────────────────
echo "Starting now-playing on ${HOST}:${PORT}…"
screen -dmS "$SCREEN_NAME" bash -c \
"cd '$SCRIPT_DIR' && npm start 1>>'$LOG_FILE' 2>>'$ERROR_FILE'"
# ── wait for healthy ──────────────────────────────────────────────────────────
for i in $(seq 1 20); do
sleep 1
if is_healthy; then
green "✓ now-playing is up and healthy on ${HOST}:${PORT}"
echo " Screen session : $SCREEN_NAME (screen -r $SCREEN_NAME)"
echo " Log : $LOG_FILE"
echo " Errors : $ERROR_FILE"
exit 0
fi
printf ' Waiting… (%d/20)\r' "$i"
done
red "✗ App started but health check timed out after 20s."
echo " Check the logs : $LOG_FILE"
echo " : $ERROR_FILE"
echo " Or attach : screen -r $SCREEN_NAME"
exit 1