watcher.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package plugin_manager
  2. import (
  3. "os"
  4. "path"
  5. "time"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/aws_manager"
  7. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/local_manager"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/positive_manager"
  9. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/remote_manager"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  11. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  12. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  13. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  14. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  15. )
  16. func (p *PluginManager) startLocalWatcher(config *app.Config) {
  17. go func() {
  18. log.Info("start to handle new plugins in path: %s", config.PluginStoragePath)
  19. p.handleNewPlugins(config)
  20. for range time.NewTicker(time.Second * 30).C {
  21. p.handleNewPlugins(config)
  22. }
  23. }()
  24. }
  25. func (p *PluginManager) startRemoteWatcher(config *app.Config) {
  26. // launch TCP debugging server if enabled
  27. if config.PluginRemoteInstallingEnabled {
  28. server := remote_manager.NewRemotePluginServer(config)
  29. go func() {
  30. err := server.Launch()
  31. if err != nil {
  32. log.Error("start remote plugin server failed: %s", err.Error())
  33. }
  34. }()
  35. go func() {
  36. server.Wrap(func(rpr *remote_manager.RemotePluginRuntime) {
  37. p.lifetime(config, rpr)
  38. })
  39. }()
  40. }
  41. }
  42. func (p *PluginManager) handleNewPlugins(config *app.Config) {
  43. // load local plugins firstly
  44. for plugin := range p.loadNewPlugins(config.PluginStoragePath) {
  45. var plugin_interface entities.PluginRuntimeInterface
  46. if config.Platform == app.PLATFORM_AWS_LAMBDA {
  47. plugin_interface = &aws_manager.AWSPluginRuntime{
  48. PluginRuntime: plugin,
  49. PositivePluginRuntime: positive_manager.PositivePluginRuntime{
  50. LocalPath: plugin.State.AbsolutePath,
  51. },
  52. }
  53. } else if config.Platform == app.PLATFORM_LOCAL {
  54. plugin_interface = &local_manager.LocalPluginRuntime{
  55. PluginRuntime: plugin,
  56. PositivePluginRuntime: positive_manager.PositivePluginRuntime{
  57. LocalPath: plugin.State.AbsolutePath,
  58. },
  59. }
  60. } else {
  61. log.Error("unsupported platform: %s for plugin: %s", config.Platform, plugin.Config.Name)
  62. continue
  63. }
  64. routine.Submit(func() {
  65. p.lifetime(config, plugin_interface)
  66. })
  67. }
  68. }
  69. // chan should be closed after using that
  70. func (p *PluginManager) loadNewPlugins(root_path string) <-chan entities.PluginRuntime {
  71. ch := make(chan entities.PluginRuntime)
  72. plugins, err := os.ReadDir(root_path)
  73. if err != nil {
  74. log.Error("no plugin found in path: %s", root_path)
  75. close(ch)
  76. return ch
  77. }
  78. routine.Submit(func() {
  79. for _, plugin := range plugins {
  80. if plugin.IsDir() {
  81. configuration_path := path.Join(root_path, plugin.Name(), "manifest.yaml")
  82. configuration, err := parsePluginConfig(configuration_path)
  83. if err != nil {
  84. log.Error("parse plugin config error: %v", err)
  85. continue
  86. }
  87. status := p.verifyPluginStatus(configuration)
  88. if status.exist {
  89. continue
  90. }
  91. // check if .verified file exists
  92. verified_path := path.Join(root_path, plugin.Name(), ".verified")
  93. _, err = os.Stat(verified_path)
  94. ch <- entities.PluginRuntime{
  95. Config: *configuration,
  96. State: entities.PluginRuntimeState{
  97. Status: entities.PLUGIN_RUNTIME_STATUS_PENDING,
  98. Restarts: 0,
  99. AbsolutePath: path.Join(root_path, plugin.Name()),
  100. ActiveAt: nil,
  101. Verified: err == nil,
  102. },
  103. }
  104. }
  105. }
  106. close(ch)
  107. })
  108. return ch
  109. }
  110. func parsePluginConfig(configuration_path string) (*plugin_entities.PluginDeclaration, error) {
  111. text, err := os.ReadFile(configuration_path)
  112. if err != nil {
  113. return nil, err
  114. }
  115. result, err := plugin_entities.UnmarshalPluginDeclarationFromYaml(text)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return result, nil
  120. }
  121. type pluginStatusResult struct {
  122. exist bool
  123. }
  124. func (p *PluginManager) verifyPluginStatus(config *plugin_entities.PluginDeclaration) pluginStatusResult {
  125. _, exist := p.checkPluginExist(config.Identity())
  126. if exist {
  127. return pluginStatusResult{
  128. exist: true,
  129. }
  130. }
  131. return pluginStatusResult{
  132. exist: false,
  133. }
  134. }