init.go 865 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package cluster
  2. import (
  3. "sync"
  4. "sync/atomic"
  5. "time"
  6. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  8. )
  9. type pluginLifeTime struct {
  10. lifetime entities.PluginRuntimeTimeLifeInterface
  11. last_scheduled_at time.Time
  12. }
  13. type Cluster struct {
  14. i_am_master bool
  15. port uint16
  16. plugins map[string]*pluginLifeTime
  17. plugin_lock sync.Mutex
  18. stop_chan chan bool
  19. stopped *int32
  20. }
  21. func NewCluster(config *app.Config) *Cluster {
  22. return &Cluster{
  23. port: uint16(config.ServerPort),
  24. plugins: make(map[string]*pluginLifeTime),
  25. stop_chan: make(chan bool),
  26. stopped: new(int32),
  27. }
  28. }
  29. func (c *Cluster) Launch(config *app.Config) {
  30. go c.clusterLifetime()
  31. }
  32. func (c *Cluster) Close() {
  33. if atomic.CompareAndSwapInt32(c.stopped, 0, 1) {
  34. close(c.stop_chan)
  35. }
  36. }