run.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Package local_manager handles the local plugin runtime management
  2. package local_manager
  3. import (
  4. "errors"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "sync"
  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. // gc performs garbage collection for the LocalPluginRuntime
  15. func (r *LocalPluginRuntime) gc() {
  16. if r.ioIdentity != "" {
  17. removeStdioHandler(r.ioIdentity)
  18. }
  19. if r.waitChan != nil {
  20. close(r.waitChan)
  21. r.waitChan = nil
  22. }
  23. }
  24. // Type returns the runtime type of the plugin
  25. func (r *LocalPluginRuntime) Type() plugin_entities.PluginRuntimeType {
  26. return plugin_entities.PLUGIN_RUNTIME_TYPE_LOCAL
  27. }
  28. // getCmd prepares the exec.Cmd for the plugin based on its language
  29. func (r *LocalPluginRuntime) getCmd() (*exec.Cmd, error) {
  30. if r.Config.Meta.Runner.Language == constants.Python {
  31. cmd := exec.Command(r.pythonInterpreterPath, "-m", r.Config.Meta.Runner.Entrypoint)
  32. cmd.Dir = r.State.WorkingPath
  33. return cmd, nil
  34. }
  35. return nil, fmt.Errorf("unsupported language: %s", r.Config.Meta.Runner.Language)
  36. }
  37. // StartPlugin starts the plugin and manages its lifecycle
  38. func (r *LocalPluginRuntime) StartPlugin() error {
  39. defer log.Info("plugin %s stopped", r.Config.Identity())
  40. defer func() {
  41. r.waitChanLock.Lock()
  42. for _, c := range r.waitStoppedChan {
  43. select {
  44. case c <- true:
  45. default:
  46. }
  47. }
  48. r.waitChanLock.Unlock()
  49. }()
  50. if r.isNotFirstStart {
  51. r.SetRestarting()
  52. } else {
  53. r.SetLaunching()
  54. r.isNotFirstStart = true
  55. }
  56. // reset wait chan
  57. r.waitChan = make(chan bool)
  58. // reset wait launched chan
  59. // start plugin
  60. e, err := r.getCmd()
  61. if err != nil {
  62. return err
  63. }
  64. e.Dir = r.State.WorkingPath
  65. // add env INSTALL_METHOD=local
  66. e.Env = append(e.Env, "INSTALL_METHOD=local", "PATH="+os.Getenv("PATH"))
  67. // get writer
  68. stdin, err := e.StdinPipe()
  69. if err != nil {
  70. return fmt.Errorf("get stdin pipe failed: %s", err.Error())
  71. }
  72. defer stdin.Close()
  73. // get stdout
  74. stdout, err := e.StdoutPipe()
  75. if err != nil {
  76. return fmt.Errorf("get stdout pipe failed: %s", err.Error())
  77. }
  78. defer stdout.Close()
  79. // get stderr
  80. stderr, err := e.StderrPipe()
  81. if err != nil {
  82. return fmt.Errorf("get stderr pipe failed: %s", err.Error())
  83. }
  84. defer stderr.Close()
  85. if err := e.Start(); err != nil {
  86. return fmt.Errorf("start plugin failed: %s", err.Error())
  87. }
  88. defer func() {
  89. // wait for plugin to exit
  90. err = e.Wait()
  91. if err != nil {
  92. log.Error("plugin %s exited with error: %s", r.Config.Identity(), err.Error())
  93. }
  94. r.gc()
  95. }()
  96. // ensure the plugin process is killed after the plugin exits
  97. defer e.Process.Kill()
  98. log.Info("plugin %s started", r.Config.Identity())
  99. // setup stdio
  100. stdio := registerStdioHandler(r.Config.Identity(), stdin, stdout, stderr)
  101. r.ioIdentity = stdio.GetID()
  102. defer stdio.Stop()
  103. wg := sync.WaitGroup{}
  104. wg.Add(2)
  105. // listen to plugin stdout
  106. routine.Submit(map[string]string{
  107. "module": "plugin_manager",
  108. "type": "local",
  109. "function": "StartStdout",
  110. }, func() {
  111. defer wg.Done()
  112. stdio.StartStdout(func() {})
  113. })
  114. // listen to plugin stderr
  115. routine.Submit(map[string]string{
  116. "module": "plugin_manager",
  117. "type": "local",
  118. "function": "StartStderr",
  119. }, func() {
  120. defer wg.Done()
  121. stdio.StartStderr()
  122. })
  123. // send started event
  124. r.waitChanLock.Lock()
  125. for _, c := range r.waitStartedChan {
  126. select {
  127. case c <- true:
  128. default:
  129. }
  130. }
  131. r.waitChanLock.Unlock()
  132. // wait for plugin to exit
  133. err = stdio.Wait()
  134. if err != nil {
  135. return err
  136. }
  137. wg.Wait()
  138. // plugin has exited
  139. return nil
  140. }
  141. // Wait returns a channel that will be closed when the plugin stops
  142. func (r *LocalPluginRuntime) Wait() (<-chan bool, error) {
  143. if r.waitChan == nil {
  144. return nil, errors.New("plugin not started")
  145. }
  146. return r.waitChan, nil
  147. }
  148. // WaitStarted returns a channel that will receive true when the plugin starts
  149. func (r *LocalPluginRuntime) WaitStarted() <-chan bool {
  150. c := make(chan bool)
  151. r.waitChanLock.Lock()
  152. r.waitStartedChan = append(r.waitStartedChan, c)
  153. r.waitChanLock.Unlock()
  154. return c
  155. }
  156. // WaitStopped returns a channel that will receive true when the plugin stops
  157. func (r *LocalPluginRuntime) WaitStopped() <-chan bool {
  158. c := make(chan bool)
  159. r.waitChanLock.Lock()
  160. r.waitStoppedChan = append(r.waitStoppedChan, c)
  161. r.waitChanLock.Unlock()
  162. return c
  163. }
  164. // Stop stops the plugin
  165. func (r *LocalPluginRuntime) Stop() {
  166. // inherit from PluginRuntime
  167. r.PluginRuntime.Stop()
  168. // get stdio
  169. stdio := getStdioHandler(r.ioIdentity)
  170. if stdio != nil {
  171. stdio.Stop()
  172. }
  173. }