(path, venv string, logOn bool)
| 33 | ) |
| 34 | |
| 35 | func initPlugin(path, venv string, logOn bool) (plugin funplugin.IPlugin, err error) { |
| 36 | // plugin file not found |
| 37 | if path == "" { |
| 38 | return nil, nil |
| 39 | } |
| 40 | pluginPath, err := locatePlugin(path) |
| 41 | if err != nil { |
| 42 | log.Warn().Str("path", path).Msg("locate plugin failed") |
| 43 | return nil, nil |
| 44 | } |
| 45 | |
| 46 | pluginMutex.Lock() |
| 47 | defer pluginMutex.Unlock() |
| 48 | |
| 49 | // reuse plugin instance if it already initialized |
| 50 | if p, ok := pluginMap.Load(pluginPath); ok { |
| 51 | return p.(funplugin.IPlugin), nil |
| 52 | } |
| 53 | |
| 54 | pluginOptions := []funplugin.Option{funplugin.WithDebugLogger(logOn)} |
| 55 | |
| 56 | if strings.HasSuffix(pluginPath, ".py") { |
| 57 | // register funppy plugin |
| 58 | genPyPluginPath := filepath.Join(filepath.Dir(pluginPath), PluginPySourceGenFile) |
| 59 | err = BuildPlugin(pluginPath, genPyPluginPath) |
| 60 | if err != nil { |
| 61 | log.Error().Err(err).Str("path", pluginPath).Msg("build plugin failed") |
| 62 | return nil, err |
| 63 | } |
| 64 | pluginPath = genPyPluginPath |
| 65 | |
| 66 | packages := []string{"funppy"} |
| 67 | python3, err := myexec.EnsurePython3Venv(venv, packages...) |
| 68 | if err != nil { |
| 69 | log.Error().Err(err). |
| 70 | Interface("packages", packages). |
| 71 | Msg("python3 venv is not ready") |
| 72 | return nil, errors.Wrap(code.InvalidPython3Venv, err.Error()) |
| 73 | } |
| 74 | pluginOptions = append(pluginOptions, funplugin.WithPython3(python3)) |
| 75 | } |
| 76 | |
| 77 | // found plugin file |
| 78 | plugin, err = funplugin.Init(pluginPath, pluginOptions...) |
| 79 | if err != nil { |
| 80 | log.Error().Err(err).Msgf("init plugin failed: %s", pluginPath) |
| 81 | err = errors.Wrap(code.InitPluginFailed, err.Error()) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | // add plugin instance to plugin map |
| 86 | pluginMap.Store(pluginPath, plugin) |
| 87 | |
| 88 | // report event for initializing plugin |
| 89 | params := map[string]interface{}{ |
| 90 | "type": plugin.Type(), |
| 91 | "result": "success", |
| 92 | } |
no test coverage detected