diff --git a/AppImage/scripts/health_persistence.py b/AppImage/scripts/health_persistence.py index 70a81170..7758f171 100644 --- a/AppImage/scripts/health_persistence.py +++ b/AppImage/scripts/health_persistence.py @@ -1325,7 +1325,7 @@ class HealthPersistence: print(f"[HealthPersistence] Error recording UNKNOWN persistent: {e}") - # ──────────────────────────────────────────────────────────────── + # ───────────────────────────────────────────────────────────────�� # Disk Observations API # ──────────────────────────────────────────────────────────────── diff --git a/AppImage/scripts/notification_manager.py b/AppImage/scripts/notification_manager.py index 4b2bb46d..1261bec3 100644 --- a/AppImage/scripts/notification_manager.py +++ b/AppImage/scripts/notification_manager.py @@ -1043,8 +1043,16 @@ class NotificationManager: entity_id=entity_id, ) - # Process the event through the normal pipeline - return self._process_single_event(event) + # For urgent events (shutdown/reboot), dispatch directly to ensure + # immediate delivery before the system goes down. + # For other events, use the normal pipeline with aggregation. + _URGENT_EVENTS = {'system_shutdown', 'system_reboot'} + if event_type in _URGENT_EVENTS: + self._dispatch_event(event) + return {'success': True, 'event_type': event_type, 'dispatched': 'direct'} + else: + self._process_event(event) + return {'success': True, 'event_type': event_type, 'dispatched': 'queued'} def send_notification(self, event_type: str, severity: str, title: str, message: str, diff --git a/AppImage/scripts/shutdown-notify.sh b/AppImage/scripts/shutdown-notify.sh index 7fd9674f..34f46a8d 100644 --- a/AppImage/scripts/shutdown-notify.sh +++ b/AppImage/scripts/shutdown-notify.sh @@ -6,16 +6,26 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" CONFIG_DIR="/var/lib/proxmenux" CONFIG_FILE="$CONFIG_DIR/config.json" +LOG_FILE="/var/log/proxmenux-shutdown.log" PORT="${PORT:-5000}" +# Logging function +log() { + echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" 2>/dev/null || true +} + +log "=== Shutdown notification script started ===" + # Determine if this is a reboot or shutdown # Check for systemd targets or runlevel is_reboot=false if systemctl is-active --quiet reboot.target 2>/dev/null; then is_reboot=true + log "Detected: reboot.target is active" elif [ -f /run/systemd/shutdown/scheduled ]; then if grep -q "reboot" /run/systemd/shutdown/scheduled 2>/dev/null; then is_reboot=true + log "Detected: /run/systemd/shutdown/scheduled contains 'reboot'" fi fi @@ -29,19 +39,37 @@ else fi hostname=$(hostname) +log "Event type: $event_type, Hostname: $hostname" # Try to send notification via internal API endpoint # The Flask server may still be running at this point -curl -s -X POST "http://127.0.0.1:$PORT/api/internal/shutdown-event" \ +log "Sending notification to http://127.0.0.1:$PORT/api/internal/shutdown-event" + +response=$(curl -s -w "\n%{http_code}" -X POST "http://127.0.0.1:$PORT/api/internal/shutdown-event" \ -H "Content-Type: application/json" \ -d "{\"event_type\": \"$event_type\", \"hostname\": \"$hostname\", \"reason\": \"$reason\"}" \ - --max-time 5 2>/dev/null || true + --max-time 10 2>&1) + +http_code=$(echo "$response" | tail -n1) +body=$(echo "$response" | head -n -1) + +log "Response HTTP code: $http_code" +log "Response body: $body" + +if [ "$http_code" = "200" ]; then + log "Notification sent successfully" +else + log "WARNING: Notification may have failed (HTTP $http_code)" +fi # Give the notification a moment to be sent -sleep 2 +log "Waiting 3 seconds for notification delivery..." +sleep 3 # Now terminate the Flask process # Find the main process and send SIGTERM -pkill -TERM -f "proxmenux-monitor" 2>/dev/null || true +log "Terminating Flask process..." +pkill -TERM -f "flask_server" 2>/dev/null || true +log "=== Shutdown notification script completed ===" exit 0 diff --git a/AppImage/scripts/test_real_events.sh b/AppImage/scripts/test_real_events.sh index 8e377f76..16599fee 100644 --- a/AppImage/scripts/test_real_events.sh +++ b/AppImage/scripts/test_real_events.sh @@ -30,7 +30,7 @@ NC='\033[0m' API="http://127.0.0.1:8008" LOG_FILE="/tmp/proxmenux_real_test_$(date +%Y%m%d_%H%M%S).log" -# ── Helpers ───────────────────────────────────────────────────── +# ── Helpers ─────────────────────���─────────────────────────────── log() { echo -e "$1" | tee -a "$LOG_FILE"; } header() { echo "" | tee -a "$LOG_FILE"