mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-21 17:38:10 +00:00
插件删除回调:onRemove 生命周期(仅卸载触发,重载不触发)
- 公共 SDK(third_party/homeagent-sdk):RegisterOnRemoveHandler/RunOnRemoveHandlers (后注册先执行、幂等);内部 SDK PluginManager 接口新增 RemovePlugin - registry.RemovePlugin:runStopHandlers → Stop → 清理 plugins/sdkRefs/instances → runOnRemoveHandlers → toolCleaner.UnregisterPluginTools → cfgReg.RemoveDisabledPlugin - pluginmgr.removePlugin 先调 Registry.RemovePlugin 再删目录 - 配置清理:PluginSettings.Remove(key)(内部 SettingsAPI + settingsImpl) - 演示:timer 插件 onRemove 删 max_duration 键;plugindev 模板同步 onRemove 示例; SDK example/calendar cleanupData 删 events.json;manager plugins/uninstall 先停通道
This commit is contained in:
28
third_party/homeagent-sdk/sdk/plugin.go
vendored
28
third_party/homeagent-sdk/sdk/plugin.go
vendored
@ -205,6 +205,9 @@ type PluginSDK struct {
|
||||
|
||||
stopMu sync.Mutex
|
||||
stopHandlers []func()
|
||||
|
||||
removeMu sync.Mutex
|
||||
removeHandlers []func()
|
||||
}
|
||||
|
||||
// New creates a PluginSDK with the given dependencies.
|
||||
@ -408,3 +411,28 @@ func (s *PluginSDK) RunStopHandlers() {
|
||||
handlers[i]()
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterOnRemoveHandler 注册插件被删除(卸载)时的清理回调。
|
||||
// 注册的 handler 会在插件目录被移除前按"后注册先执行"的顺序调用,
|
||||
// 适用于清理外部资源、删除配置表、下线状态等删除后处理。
|
||||
// 可注册多个;执行后清空(一次删除只执行一次)。
|
||||
func (s *PluginSDK) RegisterOnRemoveHandler(fn func()) {
|
||||
if fn == nil {
|
||||
return
|
||||
}
|
||||
s.removeMu.Lock()
|
||||
s.removeHandlers = append(s.removeHandlers, fn)
|
||||
s.removeMu.Unlock()
|
||||
}
|
||||
|
||||
// RunOnRemoveHandlers 执行全部已注册的 onRemove handler(后注册先执行,执行后清空,幂等)。
|
||||
// 由内核在卸载插件(registry.RemovePlugin)时、插件 Stop() 之后执行。
|
||||
func (s *PluginSDK) RunOnRemoveHandlers() {
|
||||
s.removeMu.Lock()
|
||||
handlers := append([]func(){}, s.removeHandlers...)
|
||||
s.removeHandlers = nil
|
||||
s.removeMu.Unlock()
|
||||
for i := len(handlers) - 1; i >= 0; i-- {
|
||||
handlers[i]()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user