state.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package cluster
  2. import (
  3. "sync/atomic"
  4. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  5. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  6. )
  7. // RegisterPlugin registers a plugin to the cluster, and start to be scheduled
  8. func (c *Cluster) RegisterPlugin(lifetime entities.PluginRuntimeTimeLifeInterface) error {
  9. identity, err := lifetime.Identity()
  10. if err != nil {
  11. return err
  12. }
  13. done := make(chan bool)
  14. closed := new(int32)
  15. close := func() {
  16. if atomic.CompareAndSwapInt32(closed, 0, 1) {
  17. close(done)
  18. }
  19. }
  20. lifetime.OnStop(func() {
  21. c.plugin_lock.Lock()
  22. delete(c.plugins, identity)
  23. c.plugin_lock.Unlock()
  24. close()
  25. })
  26. c.plugin_lock.Lock()
  27. if !lifetime.Stopped() {
  28. c.plugins[identity] = &pluginLifeTime{
  29. lifetime: lifetime,
  30. }
  31. } else {
  32. close()
  33. }
  34. c.plugin_lock.Unlock()
  35. log.Info("start to schedule plugin %s", identity)
  36. return nil
  37. }
  38. // SchedulePlugin schedules a plugin to the cluster
  39. func (c *Cluster) schedulePlugins() error {
  40. return nil
  41. }
  42. // doPluginUpdate updates the plugin state and schedule the plugin
  43. func (c *Cluster) doPluginStateUpdate(lifetime *pluginLifeTime) error {
  44. return nil
  45. }