mirror of
https://github.com/trycua/lume.git
synced 2026-01-07 21:10:06 -06:00
implemented watchdog on server
This commit is contained in:
@@ -3,11 +3,15 @@ Command-line interface for the Computer API server.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import threading
|
||||
from typing import List, Optional
|
||||
|
||||
from .server import Server
|
||||
from .watchdog import Watchdog
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -37,6 +41,22 @@ def parse_args(args: Optional[List[str]] = None) -> argparse.Namespace:
|
||||
type=str,
|
||||
help="Path to SSL certificate file (enables HTTPS)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--watchdog",
|
||||
action="store_true",
|
||||
help="Enable watchdog monitoring (automatically enabled if CONTAINER_NAME env var is set)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--watchdog-interval",
|
||||
type=int,
|
||||
default=30,
|
||||
help="Watchdog ping interval in seconds (default: 30)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-restart",
|
||||
action="store_true",
|
||||
help="Disable automatic server restart in watchdog",
|
||||
)
|
||||
|
||||
return parser.parse_args(args)
|
||||
|
||||
@@ -51,6 +71,54 @@ def main() -> None:
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
)
|
||||
|
||||
# Check if watchdog should be enabled
|
||||
container_name = os.environ.get("CONTAINER_NAME")
|
||||
enable_watchdog = args.watchdog or bool(container_name)
|
||||
|
||||
if container_name:
|
||||
logger.info(f"Container environment detected (CONTAINER_NAME={container_name}), enabling watchdog")
|
||||
elif args.watchdog:
|
||||
logger.info("Watchdog explicitly enabled via --watchdog flag")
|
||||
|
||||
# Start watchdog if enabled
|
||||
if enable_watchdog:
|
||||
logger.info(f"Starting watchdog monitoring with {args.watchdog_interval}s interval")
|
||||
|
||||
def run_watchdog_thread():
|
||||
"""Run watchdog in a separate thread."""
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
try:
|
||||
# Create CLI args dict for watchdog
|
||||
cli_args = {
|
||||
'host': args.host,
|
||||
'port': args.port,
|
||||
'log_level': args.log_level,
|
||||
'ssl_keyfile': args.ssl_keyfile,
|
||||
'ssl_certfile': args.ssl_certfile
|
||||
}
|
||||
|
||||
# Create watchdog with restart settings
|
||||
watchdog = Watchdog(
|
||||
cli_args=cli_args,
|
||||
ping_interval=args.watchdog_interval
|
||||
)
|
||||
watchdog.restart_enabled = not args.no_restart
|
||||
|
||||
loop.run_until_complete(watchdog.start_monitoring())
|
||||
except Exception as e:
|
||||
logger.error(f"Watchdog error: {e}")
|
||||
finally:
|
||||
loop.close()
|
||||
|
||||
# Start watchdog in background thread
|
||||
watchdog_thread = threading.Thread(
|
||||
target=run_watchdog_thread,
|
||||
daemon=True,
|
||||
name="watchdog"
|
||||
)
|
||||
watchdog_thread.start()
|
||||
|
||||
# Create and start the server
|
||||
logger.info(f"Starting CUA Computer API server on {args.host}:{args.port}...")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user