lifecycle.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package plugins
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "os/exec"
  6. "path/filepath"
  7. )
  8. func (m *Manager) StartPlugin(pluginID string) error {
  9. plugin, ok := m.LoadedPlugins.Load(pluginID)
  10. if !ok {
  11. return errors.New("plugin not found")
  12. }
  13. //Get the plugin Entry point
  14. pluginEntryPoint, err := m.GetPluginEntryPoint(pluginID)
  15. if err != nil {
  16. //Plugin removed after introspect?
  17. return err
  18. }
  19. //Get the absolute path of the plugin entry point to prevent messing up with the cwd
  20. absolutePath, err := filepath.Abs(pluginEntryPoint)
  21. if err != nil {
  22. return err
  23. }
  24. //Prepare plugin start configuration
  25. pluginConfiguration := ConfigureSpec{
  26. Port: getRandomPortNumber(),
  27. RuntimeConst: *m.Options.SystemConst,
  28. }
  29. js, _ := json.Marshal(pluginConfiguration)
  30. cmd := exec.Command(absolutePath, "-configure="+string(js))
  31. cmd.Dir = filepath.Dir(absolutePath)
  32. if err := cmd.Start(); err != nil {
  33. return err
  34. }
  35. // Store the cmd object so it can be accessed later for stopping the plugin
  36. plugin.(*Plugin).Process = cmd
  37. plugin.(*Plugin).Enabled = true
  38. return nil
  39. }
  40. // Check if the plugin is still running
  41. func (m *Manager) PluginStillRunning(pluginID string) bool {
  42. plugin, ok := m.LoadedPlugins.Load(pluginID)
  43. if !ok {
  44. return false
  45. }
  46. return plugin.(*Plugin).Process.ProcessState == nil
  47. }
  48. // BlockUntilAllProcessExited blocks until all the plugins processes have exited
  49. func (m *Manager) BlockUntilAllProcessExited() {
  50. m.LoadedPlugins.Range(func(key, value interface{}) bool {
  51. plugin := value.(*Plugin)
  52. if m.PluginStillRunning(value.(*Plugin).Spec.ID) {
  53. //Wait for the plugin to exit
  54. plugin.Process.Wait()
  55. }
  56. return true
  57. })
  58. }