模块定义¶
基本模块¶
定义一个模块需要继承 BaseModule 并实现必要的方法:
from symphra_modules.abc import BaseModule, ModuleMetadata
class MyModule(BaseModule):
@property
def metadata(self) -> ModuleMetadata:
return ModuleMetadata(
name="my_module",
version="1.0.0",
description="我的模块"
)
def start(self) -> None:
print("模块启动")
def stop(self) -> None:
print("模块停止")
元数据配置¶
ModuleMetadata 支持以下字段:
name: 模块名称(必需)version: 版本号description: 描述dependencies: 依赖模块列表optional_dependencies: 可选依赖config_schema: 配置schema
生命周期钩子¶
bootstrap(): 模块注册时调用install(config): 安装时调用start(): 启动时调用stop(): 停止时调用uninstall(): 卸载时调用reload(): 重载时调用
配置管理¶
def install(self, config: dict | None = None) -> None:
super().install(config)
# 使用配置
db_host = self.get_config().get("db_host", "localhost")