|
|
@@ -6,6 +6,7 @@ import logging
|
|
|
import time
|
|
|
import tempfile
|
|
|
import threading
|
|
|
+import subprocess
|
|
|
from flask import Flask, jsonify, request
|
|
|
from werkzeug.security import check_password_hash
|
|
|
from werkzeug.serving import WSGIRequestHandler
|
|
|
@@ -65,29 +66,33 @@ config = None
|
|
|
# Systemd watchdog support
|
|
|
def setup_systemd_watchdog():
|
|
|
"""Setup systemd watchdog notifications"""
|
|
|
+ import os
|
|
|
+
|
|
|
+ # Check if we have systemd watchdog enabled
|
|
|
+ watchdog_usec = os.environ.get('WATCHDOG_USEC')
|
|
|
+ if not watchdog_usec:
|
|
|
+ logger.info("Systemd watchdog not enabled")
|
|
|
+ return False
|
|
|
+
|
|
|
try:
|
|
|
- import systemd.daemon
|
|
|
- watchdog_usec = systemd.daemon.watchdog_enabled()
|
|
|
- if watchdog_usec:
|
|
|
- # Convert microseconds to seconds and send notifications at half the interval
|
|
|
- interval = (watchdog_usec / 1000000) / 2
|
|
|
- logger.info(f"Systemd watchdog enabled, sending notifications every {interval:.1f}s")
|
|
|
-
|
|
|
- def watchdog_ping():
|
|
|
- while True:
|
|
|
- try:
|
|
|
- systemd.daemon.notify("WATCHDOG=1")
|
|
|
- time.sleep(interval)
|
|
|
- except Exception as e:
|
|
|
- logger.warning(f"Failed to send watchdog notification: {e}")
|
|
|
- time.sleep(interval)
|
|
|
-
|
|
|
- # Start watchdog thread
|
|
|
- watchdog_thread = threading.Thread(target=watchdog_ping, daemon=True)
|
|
|
- watchdog_thread.start()
|
|
|
- return True
|
|
|
- except ImportError:
|
|
|
- logger.warning("systemd python package not available, watchdog disabled")
|
|
|
+ # Convert microseconds to seconds and send notifications at half the interval
|
|
|
+ interval = int(watchdog_usec) / 2000000 # Half interval in seconds
|
|
|
+ logger.info(f"Systemd watchdog enabled, sending notifications every {interval:.1f}s")
|
|
|
+
|
|
|
+ def watchdog_ping():
|
|
|
+ while True:
|
|
|
+ try:
|
|
|
+ subprocess.run(['systemd-notify', 'WATCHDOG=1'],
|
|
|
+ check=False, capture_output=True, timeout=5)
|
|
|
+ time.sleep(interval)
|
|
|
+ except Exception as e:
|
|
|
+ logger.warning(f"Failed to send watchdog notification: {e}")
|
|
|
+ time.sleep(interval)
|
|
|
+
|
|
|
+ # Start watchdog thread
|
|
|
+ watchdog_thread = threading.Thread(target=watchdog_ping, daemon=True)
|
|
|
+ watchdog_thread.start()
|
|
|
+ return True
|
|
|
except Exception as e:
|
|
|
logger.warning(f"Failed to setup systemd watchdog: {e}")
|
|
|
return False
|
|
|
@@ -548,11 +553,9 @@ def main():
|
|
|
|
|
|
# Notify systemd that we're ready
|
|
|
try:
|
|
|
- import systemd.daemon
|
|
|
- systemd.daemon.notify("READY=1")
|
|
|
+ subprocess.run(['systemd-notify', 'READY=1'],
|
|
|
+ check=False, capture_output=True, timeout=5)
|
|
|
logger.info("Sent READY=1 to systemd")
|
|
|
- except ImportError:
|
|
|
- pass
|
|
|
except Exception as e:
|
|
|
logger.warning(f"Failed to notify systemd ready: {e}")
|
|
|
|