run.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package local_manager
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "sync"
  8. "github.com/langgenius/dify-plugin-daemon/internal/process"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/constants"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  13. )
  14. func (r *LocalPluginRuntime) gc() {
  15. if r.io_identity != "" {
  16. RemoveStdio(r.io_identity)
  17. }
  18. if r.wait_chan != nil {
  19. close(r.wait_chan)
  20. r.wait_chan = nil
  21. }
  22. }
  23. func (r *LocalPluginRuntime) init() {
  24. r.wait_chan = make(chan bool)
  25. r.SetLaunching()
  26. }
  27. func (r *LocalPluginRuntime) Type() plugin_entities.PluginRuntimeType {
  28. return plugin_entities.PLUGIN_RUNTIME_TYPE_LOCAL
  29. }
  30. func (r *LocalPluginRuntime) getCmd() (*exec.Cmd, error) {
  31. if r.Config.Meta.Runner.Language == constants.Python {
  32. cmd := exec.Command(r.python_interpreter_path, "-m", r.Config.Meta.Runner.Entrypoint)
  33. cmd.Dir = r.State.WorkingPath
  34. return cmd, nil
  35. }
  36. return nil, fmt.Errorf("unsupported language: %s", r.Config.Meta.Runner.Language)
  37. }
  38. func (r *LocalPluginRuntime) StartPlugin() error {
  39. defer log.Info("plugin %s stopped", r.Config.Identity())
  40. r.init()
  41. // start plugin
  42. e, err := r.getCmd()
  43. if err != nil {
  44. return err
  45. }
  46. e.Dir = r.State.WorkingPath
  47. // add env INSTALL_METHOD=local
  48. e.Env = append(e.Env, "INSTALL_METHOD=local", "PATH="+os.Getenv("PATH"))
  49. // NOTE: subprocess will be taken care of by subprocess manager
  50. // ensure all subprocess are killed when parent process exits, especially on Golang debugger
  51. process.WrapProcess(e)
  52. // get writer
  53. stdin, err := e.StdinPipe()
  54. if err != nil {
  55. r.SetRestarting()
  56. return fmt.Errorf("get stdin pipe failed: %s", err.Error())
  57. }
  58. defer stdin.Close()
  59. stdout, err := e.StdoutPipe()
  60. if err != nil {
  61. r.SetRestarting()
  62. return fmt.Errorf("get stdout pipe failed: %s", err.Error())
  63. }
  64. defer stdout.Close()
  65. stderr, err := e.StderrPipe()
  66. if err != nil {
  67. r.SetRestarting()
  68. return fmt.Errorf("get stderr pipe failed: %s", err.Error())
  69. }
  70. defer stderr.Close()
  71. if err := e.Start(); err != nil {
  72. r.SetRestarting()
  73. return err
  74. }
  75. // add to subprocess manager
  76. process.NewProcess(e)
  77. defer process.RemoveProcess(e)
  78. defer func() {
  79. // wait for plugin to exit
  80. err = e.Wait()
  81. if err != nil {
  82. r.SetRestarting()
  83. log.Error("plugin %s exited with error: %s", r.Config.Identity(), err.Error())
  84. }
  85. r.gc()
  86. }()
  87. defer e.Process.Kill()
  88. log.Info("plugin %s started", r.Config.Identity())
  89. // setup stdio
  90. stdio := PutStdioIo(r.Config.Identity(), stdin, stdout, stderr)
  91. r.io_identity = stdio.GetID()
  92. defer stdio.Stop()
  93. wg := sync.WaitGroup{}
  94. wg.Add(2)
  95. // listen to plugin stdout
  96. routine.Submit(func() {
  97. defer wg.Done()
  98. stdio.StartStdout()
  99. })
  100. // listen to plugin stderr
  101. routine.Submit(func() {
  102. defer wg.Done()
  103. stdio.StartStderr()
  104. })
  105. // wait for plugin to exit
  106. err = stdio.Wait()
  107. if err != nil {
  108. return err
  109. }
  110. wg.Wait()
  111. // plugin has exited
  112. r.SetPending()
  113. return nil
  114. }
  115. func (r *LocalPluginRuntime) Wait() (<-chan bool, error) {
  116. if r.wait_chan == nil {
  117. return nil, errors.New("plugin not started")
  118. }
  119. return r.wait_chan, nil
  120. }