mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-25 00:51:20 +00:00
fix: resolve systemd restart loop with --replace flag (#576)
When running under systemd, the gateway could enter restart loops in two scenarios: 1. The previous gateway process hasn't fully exited when systemd starts a new one, causing 'Gateway already running (PID ...)' → exit 1 → restart → same error → infinite loop. 2. The interactive CLI exits immediately in non-TTY mode, and systemd keeps restarting it. Changes: - Add --replace flag to 'hermes gateway run' that gracefully kills any existing gateway instance (SIGTERM → wait 10s → SIGKILL) before starting, preventing the PID-lock deadlock. - Update the generated systemd unit template to use --replace by default, add ExecStop for clean shutdown, set KillMode=mixed and TimeoutStopSec=15 for proper process management. - Existing behavior (without --replace) is unchanged: still prints the error message and exits, now also mentioning the --replace option. Fixes #576
This commit is contained in:
parent
23e84de830
commit
ee5daba061
3 changed files with 78 additions and 19 deletions
|
|
@ -154,19 +154,25 @@ def get_hermes_cli_path() -> str:
|
|||
# =============================================================================
|
||||
|
||||
def generate_systemd_unit() -> str:
|
||||
import shutil
|
||||
python_path = get_python_path()
|
||||
working_dir = str(PROJECT_ROOT)
|
||||
|
||||
hermes_cli = shutil.which("hermes") or f"{python_path} -m hermes_cli.main"
|
||||
return f"""[Unit]
|
||||
Description={SERVICE_DESCRIPTION}
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart={python_path} -m hermes_cli.main gateway run
|
||||
ExecStart={python_path} -m hermes_cli.main gateway run --replace
|
||||
ExecStop={hermes_cli} gateway stop
|
||||
WorkingDirectory={working_dir}
|
||||
Restart=on-failure
|
||||
RestartSec=10
|
||||
KillMode=mixed
|
||||
KillSignal=SIGTERM
|
||||
TimeoutStopSec=15
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
|
||||
|
|
@ -377,8 +383,15 @@ def launchd_status(deep: bool = False):
|
|||
# Gateway Runner
|
||||
# =============================================================================
|
||||
|
||||
def run_gateway(verbose: bool = False):
|
||||
"""Run the gateway in foreground."""
|
||||
def run_gateway(verbose: bool = False, replace: bool = False):
|
||||
"""Run the gateway in foreground.
|
||||
|
||||
Args:
|
||||
verbose: Enable verbose logging output.
|
||||
replace: If True, kill any existing gateway instance before starting.
|
||||
This prevents systemd restart loops when the old process
|
||||
hasn't fully exited yet.
|
||||
"""
|
||||
sys.path.insert(0, str(PROJECT_ROOT))
|
||||
|
||||
from gateway.run import start_gateway
|
||||
|
|
@ -393,7 +406,7 @@ def run_gateway(verbose: bool = False):
|
|||
|
||||
# Exit with code 1 if gateway fails to connect any platform,
|
||||
# so systemd Restart=on-failure will retry on transient errors
|
||||
success = asyncio.run(start_gateway())
|
||||
success = asyncio.run(start_gateway(replace=replace))
|
||||
if not success:
|
||||
sys.exit(1)
|
||||
|
||||
|
|
@ -765,7 +778,8 @@ def gateway_command(args):
|
|||
# Default to run if no subcommand
|
||||
if subcmd is None or subcmd == "run":
|
||||
verbose = getattr(args, 'verbose', False)
|
||||
run_gateway(verbose)
|
||||
replace = getattr(args, 'replace', False)
|
||||
run_gateway(verbose, replace=replace)
|
||||
return
|
||||
|
||||
if subcmd == "setup":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue