From 2b8de674057cdfdea8b22e16610db4d7e6d32f5d Mon Sep 17 00:00:00 2001 From: asandikci Date: Sat, 26 Aug 2023 13:19:06 +0300 Subject: [PATCH] feat: load and run plugins concurrently --- cmd/ahenk-go/plugin-manager.go | 45 ++++++++++++++++++++++++++++++---- cmd/ahenk-go/plugin-opener.go | 11 ++++++--- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/cmd/ahenk-go/plugin-manager.go b/cmd/ahenk-go/plugin-manager.go index 26e85c3..e6ac0d2 100644 --- a/cmd/ahenk-go/plugin-manager.go +++ b/cmd/ahenk-go/plugin-manager.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log" "time" ) @@ -10,18 +11,35 @@ type Resources interface { AgentInfo() map[string]interface{} } +// FILLME creating new plugin interface template // type NewPluginInterface interface { // PluginMethod() returnType // } -// Loads Plugins and runs them. +// Loads Plugins and runs them concurrently. // When you create a new plugin create a new interface and call this plugin in this function func PluginManager() { - var resources Resources = LoadPlugin("resources").(Resources) - // var pluginName NewPluginInterface = LoadPlugin("pluginName").(NewPluginInterface) + chanPlug := make(chan interface{}) + + go LoadPlugin("resources", chanPlug) + res, ok := <-chanPlug + var resources Resources = res.(Resources) + checkPlugin(res, ok) + + // FILLME Loading new plugin template + // go LoadPlugin("pluginName", chanPlug) + // res, ok = <-chanPlug + // var pluginName NewPluginInterface = res.(NewPluginInterface) + // checkPlugin(res, ok) + + // Run plugins concurrently and log out for { - logPlugin("AgentInfo", resources.AgentInfo()) - time.Sleep(30 * time.Second) + go logPlugin("AgentInfo", resources.AgentInfo()) + + // FILLME Running/Logout a plugin template + // go logPlugin("InfoAboutFunction", pluginName.Function() ) + + time.Sleep(31 * time.Second) } } @@ -33,6 +51,23 @@ func logPlugin(title string, mp map[string]interface{}) { } } +// Checks plugin status +func checkPlugin(plugVal interface{}, status bool) { + if status { + if plugVal == nil { + log.Fatal("Plugin loaded but there is no value!") + } else { + log.Println("Plugin loaded and ready to use") + } + } else { + if plugVal == nil { + log.Fatal("Plugin closed and there is no value! ") + } else { + log.Fatal("Plugin closed or there is an error") + } + } +} + // // TODO response to Lider // // func createResponse() { diff --git a/cmd/ahenk-go/plugin-opener.go b/cmd/ahenk-go/plugin-opener.go index 123422f..1a9604e 100644 --- a/cmd/ahenk-go/plugin-opener.go +++ b/cmd/ahenk-go/plugin-opener.go @@ -8,11 +8,11 @@ import ( "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" ) -// Load Plugin placed in PluginDir, returns empty interface. +// Load Plugin placed in PluginDir, returns empty interface with channel // Do not forget to cast in plugin manager // // Give Plugin Name as argument and be sure you compiled plugins with `-buildmode=plugin` to PluginDir as `pluginname.so` -func LoadPlugin(plugName string) interface{} { +func LoadPlugin(plugName string, chn chan interface{}) { // TODO if error caugth try without relative path, this will be good for local testing plug, err := plugin.Open(PluginDir + plugName + ".so") @@ -28,5 +28,10 @@ func LoadPlugin(plugName string) interface{} { fmt.Println("unexpected type from module symbol") os.Exit(1) } - return plugOut + chn <- plugOut } + +// NEXT implement unload function +// func UnloadPlugin(plugName string) interface{} { + +// }