新增定时调度任务
This commit is contained in:
@@ -5,6 +5,7 @@ from ..Convention.Runtime.GlobalConfig import ProjectConfig
|
||||
from ..Convention.Runtime.Architecture import Architecture
|
||||
from ..Convention.Runtime.File import ToolFile
|
||||
from ..CoreModules.database import get_db
|
||||
from ..CoreModules.clock_scheduler import get_clock_scheduler
|
||||
from fastapi import APIRouter, FastAPI
|
||||
from typing import *
|
||||
from pydantic import *
|
||||
@@ -33,6 +34,7 @@ class PluginInterface(ABC):
|
||||
config.Log("Warning", f"{ConsoleFrontColor.YELLOW}插件{self.__class__.__name__}未实现callback方法{ConsoleFrontColor.RESET}")
|
||||
return ""
|
||||
|
||||
@final
|
||||
def execute(self, path:str) -> Optional[APIRouter]:
|
||||
'''
|
||||
继承后是否返回路由决定是否启动该插件
|
||||
@@ -76,6 +78,7 @@ class PluginInterface(ABC):
|
||||
'''
|
||||
pass
|
||||
|
||||
@final
|
||||
def register_plugin(self, command: str) -> None:
|
||||
'''
|
||||
将插件注册, 使其可以被命令匹配
|
||||
@@ -92,6 +95,43 @@ class PluginInterface(ABC):
|
||||
'''
|
||||
return None
|
||||
|
||||
def register_clock(
|
||||
self,
|
||||
callback: Callable[..., Any],
|
||||
delay_ms: int,
|
||||
*,
|
||||
args: Optional[Sequence[Any]] = None,
|
||||
kwargs: Optional[Dict[str, Any]] = None,
|
||||
) -> int:
|
||||
'''
|
||||
注册一次性延时任务
|
||||
Args:
|
||||
callback: 时间到期后调用的函数/方法
|
||||
delay_ms: 延迟毫秒数
|
||||
args: 传入回调的位置参数
|
||||
kwargs: 传入回调的关键字参数
|
||||
'''
|
||||
if not callable(callback):
|
||||
raise ValueError("callback must be callable")
|
||||
scheduler = get_clock_scheduler()
|
||||
plugin_module = callback.__module__
|
||||
plugin_class: Optional[str] = None
|
||||
if hasattr(callback, "__self__") and callback.__self__ is self:
|
||||
plugin_module = self.__class__.__module__
|
||||
plugin_class = self.__class__.__name__
|
||||
callback_name = getattr(callback, "__name__", None)
|
||||
if not callback_name:
|
||||
raise ValueError("callback must have a __name__ attribute")
|
||||
task_id = scheduler.register_task(
|
||||
plugin_module,
|
||||
plugin_class,
|
||||
callback_name,
|
||||
delay_ms,
|
||||
args=args,
|
||||
kwargs=kwargs,
|
||||
)
|
||||
return task_id
|
||||
|
||||
def is_enable_plugin(self) -> bool:
|
||||
'''
|
||||
继承后重写该方法判断是否启用该插件
|
||||
|
||||
Reference in New Issue
Block a user