watcher.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package plugin_manager
  2. import (
  3. "time"
  4. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/debugging_runtime"
  5. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/local_runtime"
  6. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  10. )
  11. func (p *PluginManager) startLocalWatcher() {
  12. go func() {
  13. log.Info("start to handle new plugins in path: %s", p.pluginStoragePath)
  14. p.handleNewLocalPlugins()
  15. for range time.NewTicker(time.Second * 30).C {
  16. p.handleNewLocalPlugins()
  17. p.removeUninstalledLocalPlugins()
  18. }
  19. }()
  20. }
  21. func (p *PluginManager) initRemotePluginServer(config *app.Config) {
  22. if p.remotePluginServer != nil {
  23. return
  24. }
  25. p.remotePluginServer = debugging_runtime.NewRemotePluginServer(config, p.mediaBucket)
  26. }
  27. func (p *PluginManager) startRemoteWatcher(config *app.Config) {
  28. // launch TCP debugging server if enabled
  29. if config.PluginRemoteInstallingEnabled {
  30. p.initRemotePluginServer(config)
  31. go func() {
  32. err := p.remotePluginServer.Launch()
  33. if err != nil {
  34. log.Error("start remote plugin server failed: %s", err.Error())
  35. }
  36. }()
  37. go func() {
  38. p.remotePluginServer.Wrap(func(rpr plugin_entities.PluginFullDuplexLifetime) {
  39. identity, err := rpr.Identity()
  40. if err != nil {
  41. log.Error("get remote plugin identity failed: %s", err.Error())
  42. return
  43. }
  44. p.m.Store(identity.String(), rpr)
  45. routine.Submit(map[string]string{
  46. "module": "plugin_manager",
  47. "function": "startRemoteWatcher",
  48. "plugin_id": identity.String(),
  49. "type": "remote",
  50. }, func() {
  51. defer func() {
  52. if err := recover(); err != nil {
  53. log.Error("plugin runtime error: %v", err)
  54. }
  55. p.m.Delete(identity.String())
  56. }()
  57. p.fullDuplexLifecycle(rpr, nil, nil)
  58. })
  59. })
  60. }()
  61. }
  62. }
  63. func (p *PluginManager) handleNewLocalPlugins() {
  64. // walk through all plugins
  65. plugins, err := p.installedBucket.List()
  66. if err != nil {
  67. log.Error("list installed plugins failed: %s", err.Error())
  68. return
  69. }
  70. for _, plugin := range plugins {
  71. _, launchedChan, errChan, err := p.launchLocal(plugin)
  72. if err != nil {
  73. log.Error("launch local plugin failed: %s", err.Error())
  74. }
  75. // consume error, avoid deadlock
  76. for err := range errChan {
  77. log.Error("plugin launch error: %s", err.Error())
  78. }
  79. // wait for plugin launched
  80. <-launchedChan
  81. }
  82. }
  83. // an async function to remove uninstalled local plugins
  84. func (p *PluginManager) removeUninstalledLocalPlugins() {
  85. // read all local plugin runtimes
  86. p.m.Range(func(key string, value plugin_entities.PluginLifetime) bool {
  87. // try to convert to local runtime
  88. runtime, ok := value.(*local_runtime.LocalPluginRuntime)
  89. if !ok {
  90. return true
  91. }
  92. pluginUniqueIdentifier, err := runtime.Identity()
  93. if err != nil {
  94. log.Error("get plugin identity failed: %s", err.Error())
  95. return true
  96. }
  97. // check if plugin is deleted, stop it if so
  98. exists, err := p.installedBucket.Exists(pluginUniqueIdentifier)
  99. if err != nil {
  100. log.Error("check if plugin is deleted failed: %s", err.Error())
  101. return true
  102. }
  103. if !exists {
  104. runtime.Stop()
  105. }
  106. return true
  107. })
  108. }