Files
chongming-plugins/ip_sound_column/__init__.py
T
2026-09-04 14:51:12 +08:00

53 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Ip_sound_column Worker 插件
统一风格模板 — 基于 chongming_worker.plugin.Plugin 基类。
工作流:
1. 编辑 config/workers/ip_sound_column/*.toml 定义 RPC 端点
2. uv run gen_models config/workers/ip_sound_column src/plugins/ip_sound_column
3. 在 router/<操作>.py 中实现业务逻辑
4. 在 on_init / on_cleanup 中初始化 / 清理资源
"""
import logging
from chongming_worker.plugin import Plugin
from . import router # noqa: F401 触发 @rpc_method 注册
# from .routes import router # 可选: 自定义 FastAPI 路由(网关自动发现)
logger = logging.getLogger(__name__)
class IpSoundColumnPlugin(Plugin):
"""ip_sound_column 插件 — 统一生命周期。"""
type = "ip_sound_column"
db_name = "ip_sound_column.db" # 自动在 data_dir 下建 SQLite 库(不需要则置 None)
auto_bind = True # 首次初始化时自动 auto_bind_models(self.type)
async def on_init(self):
"""首次 create_workers 时调用一次:初始化资源、启动后台任务。
数据库已由基类 _bootstrap 自动初始化(self.db)。
"""
config = self.config
logger.info(
"[ip_sound_column] 初始化开始 (count=%s, ip=%s)",
config.get("count", 1),
config.get("ip", "unknown")
)
ip = config.get("ip", "unknown")
self.set_var("ip", ip)
logger.info("[ip_sound_column] 初始化完成")
async def on_cleanup(self):
"""cleanup 时调用:关闭自定义客户端等(后台任务与数据库由基类处理)。"""
logger.info("[ip_sound_column] 清理完成")
# 暴露给 app lifespan 的模块级入口(bound method,签名与旧写法完全一致)
_plugin = IpSoundColumnPlugin()
create_workers = _plugin.create_workers
cleanup = _plugin.cleanup