modify: 插件初始化
This commit is contained in:
+29
-48
@@ -1,13 +1,14 @@
|
||||
"""
|
||||
Example Worker 插件 — 演示 @rpc_method + 显式模型绑定用法
|
||||
Example Worker 插件 — 演示 @rpc_method + Plugin 基类统一生命周期
|
||||
|
||||
本插件展示如何使用 chongming_worker 框架定义 RPC 端点。
|
||||
本插件展示如何使用 chongming_worker 框架定义 RPC 端点,以及如何
|
||||
基于 Plugin 基类编写风格统一的插件初始化 / 清理。
|
||||
|
||||
核心工作流:
|
||||
1. 编写 config/workers/example/*.toml 配置文件
|
||||
2. 执行 uv run gen_models 生成 Pydantic 模型
|
||||
3. 在 __init__.py 中用 @rpc_method 定义业务函数
|
||||
4. 显式导入模型并绑定到对应端点(替代 auto_bind_models)
|
||||
3. 在 router/*.py 中用 @rpc_method 定义业务函数
|
||||
4. 在 __init__.py 中用 Plugin 基类统一管理生命周期
|
||||
|
||||
关键约定:
|
||||
- 模型命名: <Operation><Method>{Request|Response}
|
||||
@@ -15,54 +16,34 @@ Example Worker 插件 — 演示 @rpc_method + 显式模型绑定用法
|
||||
- 装饰器 subject 中的操作名 → 模型前缀(驼峰)
|
||||
example.add → Add, example.subtract → Subtract
|
||||
"""
|
||||
from pathlib import Path
|
||||
import logging
|
||||
|
||||
from chongming_worker import auto_bind_models
|
||||
from chongming_worker.runtime import create_workers as _create_workers
|
||||
from chongming_worker.plugin import Plugin
|
||||
|
||||
from .router import *
|
||||
from .router import * # noqa: F401,F403 触发 @rpc_method 注册
|
||||
|
||||
auto_bind_models("example")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 工厂函数 — 供 app lifespan 动态加载
|
||||
# ============================================================
|
||||
# 框架通过 __import__ + getattr 获取此函数,
|
||||
# 然后调用 create_workers(session, config, count, worker_manager=worker_manager)。
|
||||
# 详见 src/chongming/app/__init__.py 的 lifespan。
|
||||
class ExamplePlugin(Plugin):
|
||||
"""Example 插件 — 统一生命周期。"""
|
||||
|
||||
type = "example"
|
||||
db_name = "example.db" # 自动在 data_dir 下建 SQLite 库
|
||||
auto_bind = True # 首次初始化时自动 auto_bind_models("example")
|
||||
|
||||
async def on_init(self):
|
||||
"""首次 create_workers 时调用一次:初始化资源。"""
|
||||
# 数据库已由基类自动初始化(self.db),写入全局变量供 router 使用
|
||||
self.set_var("db", self.db)
|
||||
logger.info("[example] 初始化完成")
|
||||
|
||||
async def on_cleanup(self):
|
||||
"""cleanup 时调用:关闭自定义客户端等(数据库由基类处理)。"""
|
||||
logger.info("[example] 清理完成")
|
||||
|
||||
|
||||
# 幂等标记:防止 create_workers 被重复调用时重复初始化数据库等资源
|
||||
_initialized = False
|
||||
|
||||
|
||||
async def create_workers(session, config, count=1, worker_manager=None):
|
||||
"""插件入口 — 委托给 chongming_worker 运行时创建 Worker 协程"""
|
||||
global _initialized
|
||||
if _initialized:
|
||||
print("[example] create_workers 重复调用,跳过重复初始化")
|
||||
return _create_workers(session, config, count, worker_manager)
|
||||
|
||||
from .global_var import set_global_var
|
||||
from chongming_database import Database
|
||||
|
||||
db_path = Path(config.get("data_dir", "./data")) / "example.db"
|
||||
|
||||
db = Database(db_path)
|
||||
await db.initialize()
|
||||
set_global_var("db", db)
|
||||
|
||||
_initialized = True
|
||||
return _create_workers(session, config, count, worker_manager)
|
||||
|
||||
|
||||
async def cleanup(worker_item: str):
|
||||
global _initialized
|
||||
from .global_var import db, set_global_var
|
||||
|
||||
if db is not None:
|
||||
await db.dispose()
|
||||
set_global_var("db", None)
|
||||
_initialized = False
|
||||
print(f"{worker_item} 清理完毕")
|
||||
# 暴露给 app lifespan 的模块级入口(bound method,签名与旧写法完全一致)
|
||||
plugin = ExamplePlugin()
|
||||
create_workers = plugin.create_workers
|
||||
cleanup = plugin.cleanup
|
||||
|
||||
Reference in New Issue
Block a user