mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-08-28 19:47:41 +08:00
feat(plugin): add state check for commands, APIs, and services
This commit is contained in:
+40
-15
@@ -31,7 +31,7 @@ def register_plugin_api(plugin_id: Optional[str] = None):
|
|||||||
|
|
||||||
def remove_plugin_api(plugin_id: str):
|
def remove_plugin_api(plugin_id: str):
|
||||||
"""
|
"""
|
||||||
动态移除插件 API
|
动态移除单个插件的 API
|
||||||
:param plugin_id: 插件 ID
|
:param plugin_id: 插件 ID
|
||||||
"""
|
"""
|
||||||
_update_plugin_api_routes(plugin_id, action="remove")
|
_update_plugin_api_routes(plugin_id, action="remove")
|
||||||
@@ -40,25 +40,29 @@ def remove_plugin_api(plugin_id: str):
|
|||||||
def _update_plugin_api_routes(plugin_id: Optional[str], action: str):
|
def _update_plugin_api_routes(plugin_id: Optional[str], action: str):
|
||||||
"""
|
"""
|
||||||
插件 API 路由注册和移除
|
插件 API 路由注册和移除
|
||||||
:param plugin_id: 插件 ID,如果为 None,则处理所有插件
|
:param plugin_id: 插件 ID,如果 action 为 "add" 且 plugin_id 为 None,则处理所有插件
|
||||||
:param action: 'add' 或 'remove',决定是添加还是移除路由
|
如果 action 为 "remove",plugin_id 必须是有效的插件 ID
|
||||||
|
:param action: "add" 或 "remove",决定是添加还是移除路由
|
||||||
"""
|
"""
|
||||||
if action not in {"add", "remove"}:
|
if action not in {"add", "remove"}:
|
||||||
raise ValueError("Action must be 'add' or 'remove'")
|
raise ValueError("Action must be 'add' or 'remove'")
|
||||||
|
|
||||||
is_modified = False
|
is_modified = False
|
||||||
existing_paths = {route.path: route for route in app.routes}
|
existing_paths = {route.path: route for route in app.routes}
|
||||||
plugin_apis = PluginManager().get_plugin_apis(plugin_id)
|
|
||||||
|
|
||||||
for api in plugin_apis:
|
plugin_ids = [plugin_id] if plugin_id else PluginManager().get_running_plugin_ids()
|
||||||
api_path = f"{PLUGIN_PREFIX}{api.get('path', '')}"
|
for plugin_id in plugin_ids:
|
||||||
try:
|
routes_removed = _remove_routes(plugin_id)
|
||||||
existing_route = existing_paths.get(api_path)
|
if routes_removed:
|
||||||
if existing_route:
|
is_modified = True
|
||||||
app.routes.remove(existing_route)
|
|
||||||
is_modified = True
|
|
||||||
|
|
||||||
if action == "add":
|
if action != "add":
|
||||||
|
continue
|
||||||
|
# 获取插件的 API 路由信息
|
||||||
|
plugin_apis = PluginManager().get_plugin_apis(plugin_id)
|
||||||
|
for api in plugin_apis:
|
||||||
|
api_path = f"{PLUGIN_PREFIX}{api.get('path', '')}"
|
||||||
|
try:
|
||||||
api["path"] = api_path
|
api["path"] = api_path
|
||||||
allow_anonymous = api.pop("allow_anonymous", False)
|
allow_anonymous = api.pop("allow_anonymous", False)
|
||||||
dependencies = api.setdefault("dependencies", [])
|
dependencies = api.setdefault("dependencies", [])
|
||||||
@@ -66,9 +70,9 @@ def _update_plugin_api_routes(plugin_id: Optional[str], action: str):
|
|||||||
dependencies.append(Depends(verify_apikey))
|
dependencies.append(Depends(verify_apikey))
|
||||||
app.add_api_route(**api, tags=["plugin"])
|
app.add_api_route(**api, tags=["plugin"])
|
||||||
is_modified = True
|
is_modified = True
|
||||||
|
logger.debug(f"Added plugin route: {api_path}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error {action}ing route {api_path}: {str(e)}")
|
logger.error(f"Error adding plugin route {api_path}: {str(e)}")
|
||||||
|
|
||||||
if is_modified:
|
if is_modified:
|
||||||
_clean_protected_routes(existing_paths)
|
_clean_protected_routes(existing_paths)
|
||||||
@@ -76,6 +80,27 @@ def _update_plugin_api_routes(plugin_id: Optional[str], action: str):
|
|||||||
app.setup()
|
app.setup()
|
||||||
|
|
||||||
|
|
||||||
|
def _remove_routes(plugin_id: str) -> bool:
|
||||||
|
"""
|
||||||
|
移除与单个插件相关的路由
|
||||||
|
:param plugin_id: 插件 ID
|
||||||
|
:return: 是否有路由被移除
|
||||||
|
"""
|
||||||
|
if not plugin_id:
|
||||||
|
return False
|
||||||
|
prefix = f"{PLUGIN_PREFIX}/{plugin_id}/"
|
||||||
|
routes_to_remove = [route for route in app.routes if route.path.startswith(prefix)]
|
||||||
|
removed = False
|
||||||
|
for route in routes_to_remove:
|
||||||
|
try:
|
||||||
|
app.routes.remove(route)
|
||||||
|
removed = True
|
||||||
|
logger.debug(f"Removed plugin route: {route.path}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error removing plugin route {route.path}: {str(e)}")
|
||||||
|
return removed
|
||||||
|
|
||||||
|
|
||||||
def _clean_protected_routes(existing_paths: dict):
|
def _clean_protected_routes(existing_paths: dict):
|
||||||
"""
|
"""
|
||||||
清理受保护的路由,防止在插件操作中被删除或重复添加
|
清理受保护的路由,防止在插件操作中被删除或重复添加
|
||||||
|
|||||||
+40
-22
@@ -435,27 +435,42 @@ class PluginManager(metaclass=Singleton):
|
|||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_plugin_commands(self) -> List[Dict[str, Any]]:
|
def get_plugin_state(self, pid: str) -> bool:
|
||||||
|
"""
|
||||||
|
获取插件状态
|
||||||
|
:param pid: 插件ID
|
||||||
|
"""
|
||||||
|
plugin = self._running_plugins.get(pid)
|
||||||
|
return plugin.get_state() if plugin else False
|
||||||
|
|
||||||
|
def get_plugin_commands(self, pid: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
获取插件命令
|
获取插件命令
|
||||||
[{
|
[{
|
||||||
"cmd": "/xx",
|
"cmd": "/xx",
|
||||||
"event": EventType.xx,
|
"event": EventType.xx,
|
||||||
"desc": "xxxx",
|
"desc": "xxxx",
|
||||||
"data": {}
|
"data": {},
|
||||||
|
"pid": "",
|
||||||
}]
|
}]
|
||||||
"""
|
"""
|
||||||
ret_commands = []
|
ret_commands = []
|
||||||
for _, plugin in self._running_plugins.items():
|
for plugin_id, plugin in self._running_plugins.items():
|
||||||
if hasattr(plugin, "get_command") \
|
if pid and pid != plugin_id:
|
||||||
and ObjectUtils.check_method(plugin.get_command):
|
continue
|
||||||
|
if hasattr(plugin, "get_command") and ObjectUtils.check_method(plugin.get_command):
|
||||||
try:
|
try:
|
||||||
ret_commands += plugin.get_command() or []
|
if not plugin.get_state():
|
||||||
|
continue
|
||||||
|
commands = plugin.get_command() or []
|
||||||
|
for command in commands:
|
||||||
|
command["pid"] = plugin_id
|
||||||
|
ret_commands.extend(commands)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"获取插件命令出错:{str(e)}")
|
logger.error(f"获取插件命令出错:{str(e)}")
|
||||||
return ret_commands
|
return ret_commands
|
||||||
|
|
||||||
def get_plugin_apis(self, plugin_id: str = None) -> List[Dict[str, Any]]:
|
def get_plugin_apis(self, pid: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
获取插件API
|
获取插件API
|
||||||
[{
|
[{
|
||||||
@@ -468,21 +483,22 @@ class PluginManager(metaclass=Singleton):
|
|||||||
}]
|
}]
|
||||||
"""
|
"""
|
||||||
ret_apis = []
|
ret_apis = []
|
||||||
for pid, plugin in self._running_plugins.items():
|
for plugin_id, plugin in self._running_plugins.items():
|
||||||
if plugin_id and pid != plugin_id:
|
if pid and pid != plugin_id:
|
||||||
continue
|
continue
|
||||||
if hasattr(plugin, "get_api") \
|
if hasattr(plugin, "get_api") and ObjectUtils.check_method(plugin.get_api):
|
||||||
and ObjectUtils.check_method(plugin.get_api):
|
|
||||||
try:
|
try:
|
||||||
|
if not plugin.get_state():
|
||||||
|
continue
|
||||||
apis = plugin.get_api() or []
|
apis = plugin.get_api() or []
|
||||||
for api in apis:
|
for api in apis:
|
||||||
api["path"] = f"/{pid}{api['path']}"
|
api["path"] = f"/{plugin_id}{api['path']}"
|
||||||
ret_apis.extend(apis)
|
ret_apis.extend(apis)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"获取插件 {pid} API出错:{str(e)}")
|
logger.error(f"获取插件 {plugin_id} API出错:{str(e)}")
|
||||||
return ret_apis
|
return ret_apis
|
||||||
|
|
||||||
def get_plugin_services(self) -> List[Dict[str, Any]]:
|
def get_plugin_services(self, pid: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
获取插件服务
|
获取插件服务
|
||||||
[{
|
[{
|
||||||
@@ -490,19 +506,21 @@ class PluginManager(metaclass=Singleton):
|
|||||||
"name": "服务名称",
|
"name": "服务名称",
|
||||||
"trigger": "触发器:cron、interval、date、CronTrigger.from_crontab()",
|
"trigger": "触发器:cron、interval、date、CronTrigger.from_crontab()",
|
||||||
"func": self.xxx,
|
"func": self.xxx,
|
||||||
"kwagrs": {} # 定时器参数
|
"kwargs": {} # 定时器参数
|
||||||
}]
|
}]
|
||||||
"""
|
"""
|
||||||
ret_services = []
|
ret_services = []
|
||||||
for pid, plugin in self._running_plugins.items():
|
for plugin_id, plugin in self._running_plugins.items():
|
||||||
if hasattr(plugin, "get_service") \
|
if pid and pid != plugin_id:
|
||||||
and ObjectUtils.check_method(plugin.get_service):
|
continue
|
||||||
|
if hasattr(plugin, "get_service") and ObjectUtils.check_method(plugin.get_service):
|
||||||
try:
|
try:
|
||||||
services = plugin.get_service()
|
if not plugin.get_state():
|
||||||
if services:
|
continue
|
||||||
ret_services.extend(services)
|
services = plugin.get_service() or []
|
||||||
|
ret_services.extend(services)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"获取插件 {pid} 服务出错:{str(e)}")
|
logger.error(f"获取插件 {plugin_id} 服务出错:{str(e)}")
|
||||||
return ret_services
|
return ret_services
|
||||||
|
|
||||||
def get_plugin_dashboard_meta(self):
|
def get_plugin_dashboard_meta(self):
|
||||||
|
|||||||
+5
-8
@@ -95,8 +95,7 @@ class Scheduler(metaclass=Singleton):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
PluginManager().init_config()
|
PluginManager().init_config()
|
||||||
for plugin_id in PluginManager().get_running_plugin_ids():
|
self.init_plugin_jobs()
|
||||||
self.update_plugin_job(plugin_id)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self._auth_count += 1
|
self._auth_count += 1
|
||||||
@@ -410,7 +409,7 @@ class Scheduler(metaclass=Singleton):
|
|||||||
|
|
||||||
def init_plugin_jobs(self):
|
def init_plugin_jobs(self):
|
||||||
"""
|
"""
|
||||||
注册插件公共服务
|
初始化插件定时服务
|
||||||
"""
|
"""
|
||||||
for pid in PluginManager().get_running_plugin_ids():
|
for pid in PluginManager().get_running_plugin_ids():
|
||||||
self.update_plugin_job(pid)
|
self.update_plugin_job(pid)
|
||||||
@@ -419,14 +418,14 @@ class Scheduler(metaclass=Singleton):
|
|||||||
"""
|
"""
|
||||||
更新插件定时服务
|
更新插件定时服务
|
||||||
"""
|
"""
|
||||||
if not self._scheduler:
|
if not self._scheduler or not pid:
|
||||||
return
|
return
|
||||||
# 移除该插件的全部服务
|
# 移除该插件的全部服务
|
||||||
self.remove_plugin_job(pid)
|
self.remove_plugin_job(pid)
|
||||||
# 获取插件服务列表
|
# 获取插件服务列表
|
||||||
with self._lock:
|
with self._lock:
|
||||||
try:
|
try:
|
||||||
plugin_services = PluginManager().run_plugin_method(pid, "get_service") or []
|
plugin_services = PluginManager().get_plugin_services(pid=pid)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"运行插件 {pid} 服务失败:{str(e)} - {traceback.format_exc()}")
|
logger.error(f"运行插件 {pid} 服务失败:{str(e)} - {traceback.format_exc()}")
|
||||||
return
|
return
|
||||||
@@ -451,9 +450,7 @@ class Scheduler(metaclass=Singleton):
|
|||||||
id=sid,
|
id=sid,
|
||||||
name=service["name"],
|
name=service["name"],
|
||||||
**service["kwargs"],
|
**service["kwargs"],
|
||||||
kwargs={
|
kwargs={"job_id": job_id}
|
||||||
'job_id': job_id
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
logger.info(f"注册插件{plugin_name}服务:{service['name']} - {service['trigger']}")
|
logger.info(f"注册插件{plugin_name}服务:{service['name']} - {service['trigger']}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user