watcher.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/remote_manager"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  11. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  13. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  14. )
  15. func (p *PluginManager) startWatcher(config *app.Config) {
  16. go func() {
  17. log.Info("start to handle new plugins in path: %s", config.StoragePath)
  18. p.handleNewPlugins(config)
  19. for range time.NewTicker(time.Second * 30).C {
  20. p.handleNewPlugins(config)
  21. }
  22. }()
  23. p.startRemoteWatcher(config)
  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 loadNewPlugins(config.StoragePath) {
  45. var plugin_interface entities.PluginRuntimeInterface
  46. if config.Platform == app.PLATFORM_AWS_LAMBDA {
  47. plugin_interface = &aws_manager.AWSPluginRuntime{
  48. PluginRuntime: plugin,
  49. }
  50. } else if config.Platform == app.PLATFORM_LOCAL {
  51. plugin_interface = &local_manager.LocalPluginRuntime{
  52. PluginRuntime: plugin,
  53. }
  54. } else {
  55. log.Error("unsupported platform: %s for plugin: %s", config.Platform, plugin.Config.Name)
  56. continue
  57. }
  58. routine.Submit(func() {
  59. p.lifetime(config, plugin_interface)
  60. })
  61. }
  62. }
  63. // chan should be closed after using that
  64. func loadNewPlugins(root_path string) <-chan entities.PluginRuntime {
  65. ch := make(chan entities.PluginRuntime)
  66. plugins, err := os.ReadDir(root_path)
  67. if err != nil {
  68. log.Error("no plugin found in path: %s", root_path)
  69. close(ch)
  70. return ch
  71. }
  72. routine.Submit(func() {
  73. for _, plugin := range plugins {
  74. if plugin.IsDir() {
  75. configuration_path := path.Join(root_path, plugin.Name(), "manifest.yaml")
  76. configuration, err := parsePluginConfig(configuration_path)
  77. if err != nil {
  78. log.Error("parse plugin config error: %v", err)
  79. continue
  80. }
  81. status := verifyPluginStatus(configuration)
  82. if status.exist {
  83. continue
  84. }
  85. // check if .verified file exists
  86. verified_path := path.Join(root_path, plugin.Name(), ".verified")
  87. _, err = os.Stat(verified_path)
  88. ch <- entities.PluginRuntime{
  89. Config: *configuration,
  90. State: entities.PluginRuntimeState{
  91. Status: entities.PLUGIN_RUNTIME_STATUS_PENDING,
  92. Restarts: 0,
  93. RelativePath: path.Join(root_path, plugin.Name()),
  94. ActiveAt: nil,
  95. Verified: err == nil,
  96. },
  97. }
  98. }
  99. }
  100. close(ch)
  101. })
  102. return ch
  103. }
  104. func parsePluginConfig(configuration_path string) (*plugin_entities.PluginDeclaration, error) {
  105. text, err := os.ReadFile(configuration_path)
  106. if err != nil {
  107. return nil, err
  108. }
  109. result, err := plugin_entities.UnmarshalPluginDeclarationFromYaml(text)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return result, nil
  114. }
  115. type pluginStatusResult struct {
  116. exist bool
  117. }
  118. func verifyPluginStatus(config *plugin_entities.PluginDeclaration) pluginStatusResult {
  119. _, exist := checkPluginExist(config.Identity())
  120. if exist {
  121. return pluginStatusResult{
  122. exist: true,
  123. }
  124. }
  125. return pluginStatusResult{
  126. exist: false,
  127. }
  128. }