Add process controls and restart command

This commit is contained in:
Azimkin
2026-08-28 23:25:20 +02:00
parent 12434ec557
commit d3c02ca203
14 changed files with 358 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ from app.commands.core import (
shared_access_check,
)
from app.commands.dispatcher import CommandDispatcher
from app.commands.persistent import load_persistent_commands
__all__ = [
"CheckResult",
@@ -31,4 +32,5 @@ __all__ = [
"require_args_check",
"require_reply_check",
"shared_access_check",
"load_persistent_commands",
]

View File

@@ -1,8 +1,10 @@
from __future__ import annotations
import html
import os
import re
import shlex
import signal
from textwrap import wrap
from typing import TYPE_CHECKING, Any
@@ -376,6 +378,17 @@ async def status(ctx: "CommandContext") -> None:
await ctx.reply("\n".join(lines))
@command_registry.command(
"restart",
"Stop the bot process so its supervisor can restart it",
usage=".restart",
category="system",
)
async def restart(ctx: "CommandContext") -> None:
await ctx.reply("Stopping bot process for restart")
os.kill(os.getpid(), signal.SIGTERM)
@command_registry.command(
"id",
"Show chat, thread, user, and replied message IDs",

View File

@@ -0,0 +1,32 @@
from __future__ import annotations
import importlib.util
import logging
import sys
from pathlib import Path
from app.commands.core import CommandRegistry
log = logging.getLogger(__name__)
def load_persistent_commands(commands_dir: Path, registry: CommandRegistry) -> list[str]:
"""Load command modules from workspace/commands on every bot startup."""
commands_dir.mkdir(parents=True, exist_ok=True)
loaded: list[str] = []
for path in sorted(commands_dir.glob("*.py")):
if path.name.startswith("_"):
continue
module_name = f"bot_persistent_command_{path.stem}"
try:
spec = importlib.util.spec_from_file_location(module_name, path)
if spec is None or spec.loader is None:
raise ImportError("cannot create import spec")
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
loaded.append(path.name)
except Exception:
sys.modules.pop(module_name, None)
log.exception("Failed to load persistent command module %s", path)
return loaded