manager.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package plugin_manager
  2. import (
  3. "fmt"
  4. "sync"
  5. "github.com/langgenius/dify-plugin-daemon/internal/cluster"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/dify_invocation"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  11. )
  12. type PluginManager struct {
  13. m sync.Map
  14. cluster *cluster.Cluster
  15. }
  16. var (
  17. manager *PluginManager
  18. )
  19. func InitGlobalPluginManager(cluster *cluster.Cluster, configuration *app.Config) {
  20. manager = &PluginManager{
  21. cluster: cluster,
  22. }
  23. manager.Init(configuration)
  24. }
  25. func GetGlobalPluginManager() *PluginManager {
  26. return manager
  27. }
  28. func (p *PluginManager) Add(plugin entities.PluginRuntimeInterface) error {
  29. identity, err := plugin.Identity()
  30. if err != nil {
  31. return err
  32. }
  33. p.m.Store(identity, plugin)
  34. return nil
  35. }
  36. func (p *PluginManager) List() []entities.PluginRuntimeInterface {
  37. var runtimes []entities.PluginRuntimeInterface
  38. p.m.Range(func(key, value interface{}) bool {
  39. if v, ok := value.(entities.PluginRuntimeInterface); ok {
  40. runtimes = append(runtimes, v)
  41. }
  42. return true
  43. })
  44. return runtimes
  45. }
  46. func (p *PluginManager) Get(identity string) entities.PluginRuntimeInterface {
  47. if v, ok := p.m.Load(identity); ok {
  48. if r, ok := v.(entities.PluginRuntimeInterface); ok {
  49. return r
  50. }
  51. }
  52. return nil
  53. }
  54. func (p *PluginManager) Init(configuration *app.Config) {
  55. // TODO: init plugin manager
  56. log.Info("start plugin manager daemon...")
  57. // init redis client
  58. if err := cache.InitRedisClient(
  59. fmt.Sprintf("%s:%d", configuration.RedisHost, configuration.RedisPort),
  60. configuration.RedisPass,
  61. ); err != nil {
  62. log.Panic("init redis client failed: %s", err.Error())
  63. }
  64. if err := dify_invocation.InitDifyInvocationDaemon(
  65. configuration.PluginInnerApiURL, configuration.PluginInnerApiKey,
  66. ); err != nil {
  67. log.Panic("init dify invocation daemon failed: %s", err.Error())
  68. }
  69. // start local watcher
  70. p.startLocalWatcher(configuration)
  71. // start remote watcher
  72. p.startRemoteWatcher(configuration)
  73. }