watcher.go 3.3 KB

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