Files
hevc-encoder/start_dashboard.sh
T
2026-06-16 22:25:33 +01:00

139 lines
3.3 KiB
Bash

#!/usr/bin/env bash
# Simple non-interactive hevc-dashboard launcher for Linux
set -euo pipefail
ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SERVER_DIR="$ROOT_DIR/server"
DASH_DIR="$ROOT_DIR/dashboard"
usage() {
cat <<EOF
Usage: $0 [--setup] [--worker] [--dashboard] [--cleanup] [--help]
No flags: start worker + dashboard (setup venvs if missing)
--setup only create virtual environments and install requirements
--worker start Worker API only (detached)
--dashboard start Dashboard only (foreground)
--cleanup remove server/dashboard venvs and __pycache__ folders
--help show this message
EOF
}
check_cmd() { command -v "$1" >/dev/null 2>&1; }
required=(python3 ffmpeg ffprobe)
missing=()
for cmd in "${required[@]}"; do
if ! check_cmd "$cmd"; then
missing+=("$cmd")
fi
done
if [ ${#missing[@]} -gt 0 ]; then
echo "Missing dependencies: ${missing[*]}"
echo "Please install them before proceeding."
fi
do_setup=false
do_worker=false
do_dashboard=false
do_cleanup=false
if [ "$#" -eq 0 ]; then
do_setup=true
do_worker=true
do_dashboard=true
else
while [ "$#" -gt 0 ]; do
case "$1" in
--setup) do_setup=true ;;
--worker) do_worker=true ;;
--dashboard) do_dashboard=true ;;
--cleanup) do_cleanup=true ;;
--help) usage; exit 0 ;;
*) echo "Unknown option: $1"; usage; exit 1 ;;
esac
shift
done
fi
if [ "$do_cleanup" = true ]; then
cleanup_workspace
exit 0
fi
setup_venvs() {
echo "Setting up server venv..."
mkdir -p "$SERVER_DIR"
pushd "$SERVER_DIR" >/dev/null
if [ ! -d venv ]; then
python3 -m venv venv
fi
source venv/bin/activate
pip install -q -r requirements.txt
deactivate
popd >/dev/null
echo "Setting up dashboard venv..."
pushd "$DASH_DIR" >/dev/null
if [ ! -d venv ]; then
python3 -m venv venv
fi
source venv/bin/activate
pip install -q -r requirements.txt
deactivate
popd >/dev/null
echo "Virtual environments ready."
}
cleanup_workspace() {
echo "Removing server and dashboard venvs..."
rm -rf "$SERVER_DIR/venv" "$DASH_DIR/venv"
echo "Removing __pycache__ folders..."
find "$SERVER_DIR" "$DASH_DIR" -type d -name '__pycache__' -prune -exec rm -rf {} +
echo "Cleanup complete."
}
start_worker() {
pushd "$SERVER_DIR" >/dev/null
source venv/bin/activate || true
echo "Starting Worker API..."
nohup python3 worker_api.py --port 5001 > worker.log 2>&1 &
WORKER_PID=$!
echo "Worker PID: $WORKER_PID"
popd >/dev/null
}
start_dashboard() {
pushd "$DASH_DIR" >/dev/null
source venv/bin/activate || true
export WORKER_URL="http://127.0.0.1:5001"
if command -v xdg-open >/dev/null 2>&1; then
(sleep 1 && xdg-open "http://localhost:5000" >/dev/null 2>&1 || true) &
fi
echo "Starting Dashboard (foreground)... Press Ctrl+C to stop."
python3 dashboard.py
popd >/dev/null
}
if [ "$do_setup" = true ]; then
setup_venvs
fi
if [ "$do_worker" = true ]; then
start_worker
fi
if [ "$do_dashboard" = true ]; then
start_dashboard
fi
if [ -n "${WORKER_PID-}" ]; then
trap 'echo "Stopping worker..."; kill "$WORKER_PID" 2>/dev/null || true' EXIT INT TERM
fi
exit 0