Skip to content

Commit

Permalink
feat: 插件文件更改热重载
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Nov 16, 2024
1 parent 658eb27 commit ca3999d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
21 changes: 18 additions & 3 deletions pkg/core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import threading
import traceback
import enum
import sys

from ..platform import manager as im_mgr
from ..provider.session import sessionmgr as llm_session_mgr
Expand Down Expand Up @@ -106,10 +107,8 @@ async def initialize(self):
pass

async def run(self):
await self.plugin_mgr.initialize_plugins()

try:

await self.plugin_mgr.initialize_plugins()
# 后续可能会允许动态重启其他任务
# 故为了防止程序在非 Ctrl-C 情况下退出,这里创建一个不会结束的协程
async def never_ending():
Expand Down Expand Up @@ -171,5 +170,21 @@ async def reload(
await self.platform_mgr.initialize()

self.task_mgr.create_task(self.platform_mgr.run(), name="platform-manager", scopes=[core_entities.LifecycleControlScope.APPLICATION, core_entities.LifecycleControlScope.PLATFORM])
case core_entities.LifecycleControlScope.PLUGIN.value:
self.logger.info("执行热重载 scope="+scope)
await self.plugin_mgr.destroy_plugins()

# 删除 sys.module 中所有的 plugins/* 下的模块
for mod in list(sys.modules.keys()):
if mod.startswith("plugins."):
del sys.modules[mod]

self.plugin_mgr = plugin_mgr.PluginManager(self)
await self.plugin_mgr.initialize()

await self.plugin_mgr.initialize_plugins()

await self.plugin_mgr.load_plugins()
await self.plugin_mgr.initialize_plugins()
case _:
pass
1 change: 1 addition & 0 deletions pkg/plugin/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ async def destroy(self):
pass

def __del__(self):
"""释放/禁用插件时被调用"""
pass


Expand Down
16 changes: 15 additions & 1 deletion pkg/plugin/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ async def destroy_plugin(self, plugin: context.RuntimeContainer):
return

self.ap.logger.debug(f'释放插件 {plugin.plugin_name}')
plugin.plugin_inst.__del__()
await plugin.plugin_inst.destroy()
plugin.plugin_inst = None
plugin.status = context.RuntimeContainerStatus.MOUNTED
Expand Down Expand Up @@ -124,17 +125,25 @@ async def install_plugin(
}
)

task_context.trace('重载插件..', 'reload-plugin')
await self.ap.reload(scope='plugin')

async def uninstall_plugin(
self,
plugin_name: str,
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
):
"""卸载插件
"""
await self.installer.uninstall_plugin(plugin_name, task_context)

plugin_container = self.get_plugin_by_name(plugin_name)

if plugin_container is None:
raise ValueError(f'插件 {plugin_name} 不存在')

await self.destroy_plugin(plugin_container)
await self.installer.uninstall_plugin(plugin_name, task_context)

await self.ap.ctr_mgr.plugin.post_remove_record(
{
"name": plugin_name,
Expand All @@ -144,6 +153,9 @@ async def uninstall_plugin(
}
)

task_context.trace('重载插件..', 'reload-plugin')
await self.ap.reload(scope='plugin')

async def update_plugin(
self,
plugin_name: str,
Expand All @@ -167,6 +179,8 @@ async def update_plugin(
new_version="HEAD"
)

task_context.trace('重载插件..', 'reload-plugin')
await self.ap.reload(scope='plugin')

def get_plugin_by_name(self, plugin_name: str) -> context.RuntimeContainer:
"""通过插件名获取插件
Expand Down
22 changes: 16 additions & 6 deletions web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
</v-list-item-title>
</v-list-item>

<v-list-item @click="reload('plugin')">
<v-list-item-title>
重载插件
</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</v-list-item>
Expand Down Expand Up @@ -143,21 +148,26 @@ function openDocs() {
window.open('https://docs.langbot.app', '_blank')
}
const reloadScopeLabel = {
'platform': "消息平台",
'plugin': "插件"
}
function reload(scope) {
proxy.$axios.post('/system/reload',
let label = reloadScopeLabel[scope]
proxy.$axios.post('/system/reload',
{ scope: scope },
{ headers: { 'Content-Type': 'application/json' } }
).then(response => {
if (response.data.code === 0) {
success('消息平台已重载')
success(label+'已重载')
// 关闭菜单
} else {
error('消息平台重载失败' + response.data.message)
error(label+'重载失败' + response.data.message)
}
}).catch(error => {
console.error(error)
error('消息平台重载失败:' + error)
}).catch(err => {
error(label+'重载失败:' + err)
})
}
Expand Down

0 comments on commit ca3999d

Please sign in to comment.