diff --git a/diskwatcher.py b/diskwatcher.py index 1a9e31b..d309718 100755 --- a/diskwatcher.py +++ b/diskwatcher.py @@ -1297,6 +1297,9 @@ def main(): ) args = parser.parse_args() + # Control room launcher env override (between YAML and CLI in priority) + if os.environ.get("CRS_PORT_HTTP"): WEB_PORT = int(os.environ["CRS_PORT_HTTP"]) + # CLI flags override YAML (only when actually supplied) if args.host is not None: WEB_HOST = args.host if args.port is not None: WEB_PORT = args.port @@ -1334,6 +1337,14 @@ def main(): t = threading.Thread(target=poll_loop, daemon=True) t.start() + # Announce via mu2edaq-discovery (optional dependency). + try: + from mu2edaq_discovery import Responder + Responder(name="Disk Watcher", app="diskwatcher", + port=WEB_PORT, scheme="http").start() + except ImportError: + print("[Discovery] mu2edaq-discovery not installed; discovery disabled") + # Start Flask (werkzeug development server) app.run(host=WEB_HOST, port=WEB_PORT, use_reloader=False, threaded=True) diff --git a/requirements.txt b/requirements.txt index f3ee296..28332b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ flask>=3.0 pyyaml>=6.0 +mu2edaq-discovery @ git+https://github.com/Mu2e/mu2edaq-discovery.git diff --git a/start-mu2edaq-diskwatcher.sh b/start-mu2edaq-diskwatcher.sh new file mode 100755 index 0000000..861cc5f --- /dev/null +++ b/start-mu2edaq-diskwatcher.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# Resolve paths from this script's location so it works from any cwd, and +# invoke the venv interpreter directly. Sourcing venv/bin/activate is +# fragile: if the venv has been moved, activate prepends a dead path and +# `python` silently falls back to the system interpreter (often Python 2). +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PYTHON="$SCRIPT_DIR/venv/bin/python" +if [[ ! -x "$PYTHON" ]]; then + echo "ERROR: venv interpreter not found at $PYTHON" >&2 + echo " Run ./bootstrap_diskwatcher.sh first to create the venv." >&2 + exit 1 +fi +export PYTHONPATH="$SCRIPT_DIR/src:${PYTHONPATH:-}" + +# Configuration file: first argument, else the default. +CONFIG_FILE="${1:-$SCRIPT_DIR/config/mu2edaq-diskwatcher.yaml}" +echo "Starting DAQ Diskwatcher with configuration: $CONFIG_FILE" + +#"$PYTHON" "$SCRIPT_DIR/diskwatcher.py" --config "$CONFIG_FILE" --daemon +"$PYTHON" "$SCRIPT_DIR/diskwatcher.py" --config "$CONFIG_FILE" +if [ $? -eq 0 ]; then + echo "Diskwatcher started successfully" +else + echo "Failed to start Diskwatcher" +fi diff --git a/start_diskwatcher.sh b/start_diskwatcher.sh deleted file mode 100755 index 08a0b44..0000000 --- a/start_diskwatcher.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -source ./venv/bin/activate -export PYTHONPATH=./src:$PYTHONPATH - -# Use a configuration file for the Diskwatcher, defaulting to diskwatcher.yaml if not provided -CONFIG_FILE="${1:-./config/mu2edaq-diskwatcher.yaml}" -echo "Starting DAQ Diskwatcher with configuration: $CONFIG_FILE" - -# Start the Diskwatcher -#python diskwatcher.py --config $CONFIG_FILE --daemon -python diskwatcher.py --config $CONFIG_FILE -if [ $? -eq 0 ]; then - echo "Diskwatcher started successfully" - echo "Diskwatcher PID: $(cat ${CONFIG_FILE%.yaml}.pid)" -else - echo "Failed to start Diskwatcher" -fi - - diff --git a/start_diskwatcher.sh b/start_diskwatcher.sh new file mode 120000 index 0000000..c190e1a --- /dev/null +++ b/start_diskwatcher.sh @@ -0,0 +1 @@ +start-mu2edaq-diskwatcher.sh \ No newline at end of file diff --git a/stop-mu2edaq-diskwatcher.sh b/stop-mu2edaq-diskwatcher.sh new file mode 100755 index 0000000..5899f1d --- /dev/null +++ b/stop-mu2edaq-diskwatcher.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# +# This script is used to stop the Diskwatcher. It sends a shutdown signal to the server, +# which will then cleanly shut down. +# +# We start with a simple curl command to send a shutdown request to the server's API endpoint. +# If this fails then we resort to killing the process directly. +# +# We start with a kill and then do a kill -9 if the process does not terminate within a reasonable time frame. + +# Take the name of the PID file as an argument, defaulting to the diskwatcher.pid if not provided +PID_FILE="${1:-./config/mu2edaq-diskwatcher.pid}" + +if [ -f "$PID_FILE" ]; then + PID=$(cat "$PID_FILE") + echo "Using Diskwatcher PID from file: $PID" + echo "Attempting to stop Diskwatcher with PID: $PID" + kill $PID 2>/dev/null || true + sleep 5 + if kill -0 $PID 2>/dev/null; then + echo "Diskwatcher did not shut down cleanly, killing forcefully." + kill -9 $PID 2>/dev/null || true + else + echo "Diskwatcher stopped successfully." + fi + rm -f "$PID_FILE" +else + echo "Diskwatcher PID file not found: $PID_FILE." + echo "Is the server running?" +fi diff --git a/stop_diskwatcher.sh b/stop_diskwatcher.sh deleted file mode 100755 index 5899f1d..0000000 --- a/stop_diskwatcher.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash -# -# This script is used to stop the Diskwatcher. It sends a shutdown signal to the server, -# which will then cleanly shut down. -# -# We start with a simple curl command to send a shutdown request to the server's API endpoint. -# If this fails then we resort to killing the process directly. -# -# We start with a kill and then do a kill -9 if the process does not terminate within a reasonable time frame. - -# Take the name of the PID file as an argument, defaulting to the diskwatcher.pid if not provided -PID_FILE="${1:-./config/mu2edaq-diskwatcher.pid}" - -if [ -f "$PID_FILE" ]; then - PID=$(cat "$PID_FILE") - echo "Using Diskwatcher PID from file: $PID" - echo "Attempting to stop Diskwatcher with PID: $PID" - kill $PID 2>/dev/null || true - sleep 5 - if kill -0 $PID 2>/dev/null; then - echo "Diskwatcher did not shut down cleanly, killing forcefully." - kill -9 $PID 2>/dev/null || true - else - echo "Diskwatcher stopped successfully." - fi - rm -f "$PID_FILE" -else - echo "Diskwatcher PID file not found: $PID_FILE." - echo "Is the server running?" -fi diff --git a/stop_diskwatcher.sh b/stop_diskwatcher.sh new file mode 120000 index 0000000..01204b5 --- /dev/null +++ b/stop_diskwatcher.sh @@ -0,0 +1 @@ +stop-mu2edaq-diskwatcher.sh \ No newline at end of file