feat: load and run plugins concurrently

This commit is contained in:
Aliberk Sandıkçı 2023-08-26 13:19:06 +03:00
parent f8bafbbff0
commit 2b8de67405
Signed by: asandikci
GPG key ID: 25C67A03B5666BC1
2 changed files with 48 additions and 8 deletions

View file

@ -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() {

View file

@ -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{} {
// }