watcher.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package plugin_manager
  2. import (
  3. "time"
  4. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/local_manager"
  5. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/remote_manager"
  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 = remote_manager.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(func() {
  46. defer func() {
  47. if err := recover(); err != nil {
  48. log.Error("plugin runtime error: %v", err)
  49. }
  50. p.m.Delete(identity.String())
  51. }()
  52. p.fullDuplexLifecycle(rpr, nil, nil)
  53. })
  54. })
  55. }()
  56. }
  57. }
  58. func (p *PluginManager) handleNewLocalPlugins() {
  59. // walk through all plugins
  60. plugins, err := p.installedBucket.List()
  61. if err != nil {
  62. log.Error("list installed plugins failed: %s", err.Error())
  63. return
  64. }
  65. for _, plugin := range plugins {
  66. _, launchedChan, errChan, err := p.launchLocal(plugin)
  67. if err != nil {
  68. log.Error("launch local plugin failed: %s", err.Error())
  69. }
  70. // consume error, avoid deadlock
  71. for err := range errChan {
  72. log.Error("plugin launch error: %s", err.Error())
  73. }
  74. // wait for plugin launched
  75. <-launchedChan
  76. }
  77. }
  78. // an async function to remove uninstalled local plugins
  79. func (p *PluginManager) removeUninstalledLocalPlugins() {
  80. // read all local plugin runtimes
  81. p.m.Range(func(key string, value plugin_entities.PluginLifetime) bool {
  82. // try to convert to local runtime
  83. runtime, ok := value.(*local_manager.LocalPluginRuntime)
  84. if !ok {
  85. return true
  86. }
  87. pluginUniqueIdentifier, err := runtime.Identity()
  88. if err != nil {
  89. log.Error("get plugin identity failed: %s", err.Error())
  90. return true
  91. }
  92. // check if plugin is deleted, stop it if so
  93. exists, err := p.installedBucket.Exists(pluginUniqueIdentifier)
  94. if err != nil {
  95. log.Error("check if plugin is deleted failed: %s", err.Error())
  96. return true
  97. }
  98. if !exists {
  99. runtime.Stop()
  100. }
  101. return true
  102. })
  103. }