feat: hot reload for config/templates and GitPython-based hot update with pre-update notification
This commit is contained in:
1 parent
8775c022bd
commit
67776b0092
7 files changed
+164
-5
No files matched your search
@@ -2,11 +2,13 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import monitor
|
||||
import templates
|
||||
from alerter import Alerter
|
||||
from onebot import OneBotServer
|
||||
from updater import Updater
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
@@ -203,15 +205,40 @@ class Bot:
|
||||
await self.ob.start(self.on_event)
|
||||
alerter = Alerter(self.ob, self.cfg.get("alert", {}))
|
||||
alerter.start()
|
||||
updater = Updater(self.ob, self.cfg.get("update", {}))
|
||||
if self.cfg.get("update", {}).get("enabled", False):
|
||||
updater.start()
|
||||
watcher = asyncio.get_running_loop().create_task(self._watch_config(alerter))
|
||||
log.info("机器人就绪,等待 OneBot 实现端连接...")
|
||||
try:
|
||||
await asyncio.Event().wait()
|
||||
except (KeyboardInterrupt, asyncio.CancelledError):
|
||||
pass
|
||||
finally:
|
||||
watcher.cancel()
|
||||
await updater.stop()
|
||||
await alerter.stop()
|
||||
await self.ob.stop()
|
||||
|
||||
async def _watch_config(self, alerter: Alerter, interval: float = 5.0):
|
||||
"""监测 config.toml 修改并热重载(端口/token 等需重启的项不生效)。"""
|
||||
path = Path("config.toml")
|
||||
last = path.stat().st_mtime
|
||||
while True:
|
||||
await asyncio.sleep(interval)
|
||||
try:
|
||||
mtime = path.stat().st_mtime
|
||||
if mtime == last:
|
||||
continue
|
||||
last = mtime
|
||||
cfg = load_config()
|
||||
alerter.apply_config(cfg.get("alert", {}))
|
||||
log.info("配置已热重载 (端口/access_token 变更需重启生效)")
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
except Exception:
|
||||
log.exception("配置热重载失败")
|
||||
|
||||
|
||||
def main():
|
||||
if sys.platform == "win32":
|
||||
|
||||
Reference in new issue
Block a user