#!/bin/bash # Restart the LDR dev server. # # Usage: # scripts/dev/restart_server.sh [PORT] [--debug] [--tmp] # # PORT Optional port (default: 5000). Passed via LDR_WEB_PORT. # --debug DEBUG logging (LDR_APP_DEBUG=true, LDR_LOG_SETTINGS=summary). # WARNING: debug logs may contain sensitive data (queries, # answers, API responses) — local dev / feature testing only, # never production or real user data. # --tmp Disposable instance: point LDR_DATA_DIR at a throwaway dir # (default /tmp/ldr-test) so encrypted user DBs, the auth DB, # library, logs, research_outputs etc. land OUTSIDE your real # data dir (~/.local/share/local-deep-research). Override the # location by exporting LDR_DATA_DIR before running. Reuses one # dir (doesn't accumulate throwaway databases). To start from a # clean slate, wipe LDR_DATA_DIR yourself first (e.g. # `rm -rf "$LDR_DATA_DIR"`) — this script never deletes data. # # Only the instance listening on the target PORT is stopped, so multiple # instances on different ports (e.g. several agents each on their own port) # coexist — restarting one does not kill the others. # # Referenced in: # - tests/api_tests_with_login/README.md # - examples/api_usage/http/README.md # - examples/api_usage/http/advanced/simple_http_example.py # Do not delete without updating those references. set -u usage() { cat <<'EOF' Restart the LDR dev server. Usage: scripts/dev/restart_server.sh [PORT] [--debug] [--tmp] PORT Optional port (default: 5000). --debug DEBUG logging (may log sensitive data; local dev only). --tmp Disposable instance: LDR_DATA_DIR -> /tmp/ldr-test (override by exporting LDR_DATA_DIR). Keeps test data out of your real dir. This script never deletes data; wipe LDR_DATA_DIR yourself to start from a clean slate. --test Alias for --tmp. Only the instance on the target PORT is stopped; instances on other ports keep running. EOF } PORT=5000 debug=0 use_tmp=0 for arg in "$@"; do case "$arg" in --debug) debug=1 ;; --tmp|--test) use_tmp=1 ;; -h|--help) usage; exit 0 ;; ''|*[!0-9]*) echo "Unknown argument: $arg" >&2; usage >&2; exit 2 ;; *) PORT="$arg" ;; esac done echo "Stopping existing LDR server on port ${PORT} (if any)..." # Port-scoped stop: kill only the process LISTENing on PORT, leaving # instances on other ports running. The port isn't in the process argv # (it comes from LDR_WEB_PORT), so a broad pkill can't distinguish them. if command -v fuser >/dev/null 2>&1; then fuser -k "${PORT}/tcp" 2>/dev/null || echo "No existing server on port ${PORT}" elif command -v lsof >/dev/null 2>&1; then pids=$(lsof -ti "tcp:${PORT}" -sTCP:LISTEN 2>/dev/null) if [ -n "${pids}" ]; then # $pids may be several PIDs; word-splitting is intended here. # shellcheck disable=SC2086 kill ${pids} 2>/dev/null else echo "No existing server on port ${PORT}" fi else echo "Warning: neither fuser nor lsof found; cannot port-scope the stop." >&2 echo "Skipping stop to avoid killing instances on other ports." >&2 fi sleep 1 # Change to the project root. cd "$(dirname "$0")/../.." || exit 1 # --- environment for the launched server ------------------------------------- env_args=(LDR_WEB_PORT="${PORT}") if [ "$use_tmp" -eq 1 ]; then : "${LDR_DATA_DIR:=/tmp/ldr-test}" mkdir -p "$LDR_DATA_DIR" echo "Using disposable LDR_DATA_DIR=$LDR_DATA_DIR" env_args+=(LDR_DATA_DIR="${LDR_DATA_DIR}") fi if [ "$debug" -eq 1 ]; then env_args+=(LDR_APP_DEBUG=true LDR_LOG_SETTINGS=summary) fi LOG_FILE="/tmp/ldr_server_${PORT}.log" if [ "$debug" -eq 1 ]; then echo "Starting LDR server on port ${PORT} (DEBUG)..." else echo "Starting LDR server on port ${PORT}..." fi # Start server in background and detach from terminal. Backgrounding nohup # directly (rather than wrapping in an extra `( ... & ) &` subshell) means # $! captures the real server PID, not a transient subshell that has already # exited. nohup ignores SIGHUP, so the server survives the launcher exiting # and is reparented to init. nohup env "${env_args[@]}" pdm run python -m local_deep_research.web.app > "${LOG_FILE}" 2>&1 & SERVER_PID=$! sleep 2 echo "Server started. PID: ${SERVER_PID}" echo "Logs: ${LOG_FILE}" echo "URL: http://127.0.0.1:${PORT}" if [ "$debug" -eq 1 ]; then echo "" echo "WARNING: DEBUG mode — logs may contain sensitive data; local dev only." fi echo "" echo "To view logs: tail -f ${LOG_FILE}" echo "To stop server: fuser -k ${PORT}/tcp" exit 0