Skip to content

Commit a8ecb4a

Browse files
authored
Add new plugin status check interface (#606)
1 parent a87ff74 commit a8ecb4a

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

backend/app/admin/api/v1/sys/plugin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ async def get_all_plugins() -> ResponseSchemaModel[list[dict[str, Any]]]:
2222
return response_base.success(data=plugins)
2323

2424

25+
@router.get('/new', summary='是否存在新插件', dependencies=[DependsJwtAuth])
26+
async def has_new_plugins() -> ResponseSchemaModel[bool]:
27+
plugins = await plugin_service.has_new()
28+
return response_base.success(data=bool(plugins))
29+
30+
2531
@router.post(
2632
'/install/zip',
2733
summary='安装 zip 插件',

backend/app/admin/service/plugin_service.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ async def get_all() -> list[dict[str, Any]]:
3939

4040
return result
4141

42+
@staticmethod
43+
async def has_new() -> str | None:
44+
"""是否存在新插件"""
45+
return await redis_client.get(f'{settings.PLUGIN_REDIS_PREFIX}:new')
46+
4247
@staticmethod
4348
async def install_zip(*, file: UploadFile) -> None:
4449
"""
@@ -89,6 +94,7 @@ async def install_zip(*, file: UploadFile) -> None:
8994
zf.extractall(os.path.join(PLUGIN_DIR, plugin_name), members)
9095

9196
await install_requirements_async(plugin_name)
97+
await redis_client.set(f'{settings.PLUGIN_REDIS_PREFIX}:new', 'ture')
9298

9399
@staticmethod
94100
async def install_git(*, repo_url: str):
@@ -112,6 +118,7 @@ async def install_git(*, repo_url: str):
112118
raise errors.ServerError(msg='插件安装失败,请稍后重试') from e
113119
else:
114120
await install_requirements_async(repo_name)
121+
await redis_client.set(f'{settings.PLUGIN_REDIS_PREFIX}:new', 'ture')
115122

116123
@staticmethod
117124
async def uninstall(*, plugin: str):
@@ -127,6 +134,7 @@ async def uninstall(*, plugin: str):
127134
await uninstall_requirements_async(plugin)
128135
bacup_dir = os.path.join(PLUGIN_DIR, f'{plugin}.{timezone.now().strftime("%Y%m%d%H%M%S")}.backup')
129136
shutil.move(plugin_dir, bacup_dir)
137+
await redis_client.delete(f'{settings.PLUGIN_REDIS_PREFIX}:info:{plugin}')
130138

131139
@staticmethod
132140
async def update_status(*, plugin: str):
@@ -140,12 +148,14 @@ async def update_status(*, plugin: str):
140148
if not plugin_info:
141149
raise errors.ForbiddenError(msg='插件不存在')
142150
plugin_info = json.loads(plugin_info)
151+
152+
# 更新持久缓存状态
143153
new_status = (
144154
StatusType.enable.value
145155
if plugin_info.get('plugin', {}).get('enable') == StatusType.disable.value
146156
else StatusType.disable.value
147157
)
148-
plugin_info['plugin']['enable'] = new_status
158+
plugin_info['plugin']['enable'] = str(new_status)
149159
await redis_client.set(
150160
f'{settings.PLUGIN_REDIS_PREFIX}:info:{plugin}', json.dumps(plugin_info, ensure_ascii=False)
151161
)

backend/plugin/tools.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
9292
else:
9393
nest_asyncio.apply()
9494

95+
loop.create_task(redis_client.delete_prefix(f'{settings.PLUGIN_REDIS_PREFIX}:info'))
9596
plugin_status = loop.run_until_complete(redis_client.hgetall(f'{settings.PLUGIN_REDIS_PREFIX}:status')) # type: ignore
9697
if not plugin_status:
9798
plugin_status = {}
@@ -118,7 +119,7 @@ def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
118119
app_plugins.append(data)
119120

120121
# 补充插件信息
121-
data['plugin']['enable'] = plugin_status.setdefault(plugin, StatusType.enable.value)
122+
data['plugin']['enable'] = plugin_status.setdefault(plugin, str(StatusType.enable.value))
122123
data['plugin']['name'] = plugin
123124

124125
# 缓存插件信息
@@ -127,7 +128,8 @@ def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
127128
)
128129

129130
# 缓存插件状态
130-
loop.create_task(redis_client.hset(f'{settings.PLUGIN_REDIS_PREFIX}:status', mapping=plugin_status))
131+
loop.create_task(redis_client.hset(f'{settings.PLUGIN_REDIS_PREFIX}:status', mapping=plugin_status)) # type: ignore
132+
loop.create_task(redis_client.delete(f'{settings.PLUGIN_REDIS_PREFIX}:new'))
131133

132134
return extra_plugins, app_plugins
133135

0 commit comments

Comments
 (0)