diff --git a/core/grpc_plugins.go b/core/grpc_plugins.go index be559e1..782daf2 100644 --- a/core/grpc_plugins.go +++ b/core/grpc_plugins.go @@ -157,15 +157,14 @@ func initNodePlugins() { watcher.Remove(event.Name) // fmt.Println("移除插件目录", event.Name) // fmt.Println("移除插件", plugin_name) - RemNodePlugin(plugin_name) + AddNodePlugin(event.Name, plugin_name) } else if plugin_index { // fmt.Println("移除插件", plugin_name) - RemNodePlugin(plugin_name) + AddNodePlugin(event.Name, plugin_name) } case "WRITE": //, "CHMOD" if plugin_index { - RemNodePlugin(plugin_name) AddNodePlugin(event.Name, plugin_name) // fmt.Println("变更插件", event.Name, plugin_name) } @@ -179,37 +178,36 @@ func initNodePlugins() { } } -func RemNodePlugin(name string) bool { - if name == "" { - return false - } - pluginLock.Lock() - defer pluginLock.Unlock() - key := nameUuid(name) - for i := range Functions { - if Functions[i].UUID == key { - f := Functions[i] - // fmt.Println("pl", key) - DestroyAdapterByUUID(key) - Functions[i].Running = false - if len(Functions[i].CronIds) != 0 { - for _, id := range Functions[i].CronIds { - CRON.Remove(cron.EntryID(id)) - } - } - Functions = append(Functions[:i], Functions[i+1:]...) - CancelPluginCrons(key) - CancelPluginWebs(key) - CancelPluginlistening(key) - CancelHttpListen(key) - remStatic(key) - storage.DisableHandle(key) - console.Log("已移除 %s%s", f.Title, f.Suffix) - return true - } - } - return false -} +// func RemNodePlugin2(name string) bool { +// if name == "" { +// return false +// } +// pluginLock.Lock() +// defer pluginLock.Unlock() +// key := nameUuid(name) +// for i := range Functions { +// if Functions[i].UUID == key { +// f := Functions[i] +// DestroyAdapterByUUID(key) +// Functions[i].Running = false +// if len(Functions[i].CronIds) != 0 { +// for _, id := range Functions[i].CronIds { +// CRON.Remove(cron.EntryID(id)) +// } +// } +// Functions = append(Functions[:i], Functions[i+1:]...) +// CancelPluginCrons(key) +// CancelPluginWebs(key) +// CancelPluginlistening(key) +// CancelHttpListen(key) +// remStatic(key) +// storage.DisableHandle(key) +// console.Log("已移除 %s%s", f.Title, f.Suffix) +// return true +// } +// } +// return false +// } func nameUuid(name string) string { hash := sha1.Sum([]byte(name)) @@ -227,11 +225,35 @@ func AddNodePlugin(path, name string) error { return nil } uuid := nameUuid(name) - plugins.Set(uuid, "") pluginLock.Lock() defer pluginLock.Unlock() + //移除 + var rf *common.Function + for i := range Functions { + if Functions[i].UUID == uuid { + rf = Functions[i] + DestroyAdapterByUUID(uuid) + Functions[i].Running = false + if len(Functions[i].CronIds) != 0 { + for _, id := range Functions[i].CronIds { + CRON.Remove(cron.EntryID(id)) + } + } + Functions = append(Functions[:i], Functions[i+1:]...) + CancelPluginCrons(uuid) + CancelPluginWebs(uuid) + CancelPluginlistening(uuid) + CancelHttpListen(uuid) + remStatic(uuid) + storage.DisableHandle(uuid) + break + } + } file, err := os.Open(path) if err != nil { + if rf != nil { + console.Log("已卸载 %s%s", rf.Title, rf.Suffix) + } return err } defer file.Close() @@ -247,7 +269,6 @@ func AddNodePlugin(path, name string) error { // fmt.Println("add,", uuid, name) f, cbs := pluginParse(script, uuid) f.Reload = func() { //重载 - RemNodePlugin(path) AddNodePlugin(path, name) } f.Suffix = ".js" @@ -355,7 +376,11 @@ func AddNodePlugin(path, name string) error { cb() } if !f.OnStart { - console.Log("已加载 %s%s", f.Title, f.Suffix) + if rf == nil { + console.Log("已加载 %s%s", f.Title, f.Suffix) + } else { + console.Log("已重载 %s%s", f.Title, f.Suffix) + } } AddCommand([]*common.Function{f}) return nil diff --git a/core/plugin_core.go b/core/plugin_core.go index f603ad0..1a4b793 100644 --- a/core/plugin_core.go +++ b/core/plugin_core.go @@ -116,7 +116,6 @@ func initPlugins() { storage.Watch(plugins, nil, func(old, new, key string) (fin *storage.Final) { pluginLock.Lock() defer pluginLock.Unlock() - // fmt.Println(old, new, key, "===") if new == "uninstall" { new = "" fin = &storage.Final{ @@ -179,6 +178,7 @@ func initPlugins() { if new == "reload" { //重载 go f.Reload() } else if new == "" { + // fmt.Println("new", new) ss := strings.Split(filename, "/") processes.Range(func(key, value any) bool { //先停止脚本,避免windows锁定文件 p := key.(*exec.Cmd) @@ -198,8 +198,9 @@ func initPlugins() { }) os.RemoveAll(filepath.Dir(filename)) // fmt.Println(strings.Join(ss, " ")) - if len(ss) > 2 { - go RemNodePlugin(ss[len(ss)-2]) + l := len(ss) + if l > 2 { + go AddNodePlugin(filename, ss[l-1]) } } else { err := os.WriteFile(filename, []byte(new), 0755)