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