registry.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package tasks
  2. import (
  3. "sync"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/worker/types"
  6. )
  7. var (
  8. globalTypesRegistry *types.TaskRegistry
  9. globalUIRegistry *types.UIRegistry
  10. globalTaskRegistry *TaskRegistry
  11. typesRegistryOnce sync.Once
  12. uiRegistryOnce sync.Once
  13. taskRegistryOnce sync.Once
  14. )
  15. // GetGlobalTypesRegistry returns the global types registry (singleton)
  16. func GetGlobalTypesRegistry() *types.TaskRegistry {
  17. typesRegistryOnce.Do(func() {
  18. globalTypesRegistry = types.NewTaskRegistry()
  19. glog.V(1).Infof("Created global types registry")
  20. })
  21. return globalTypesRegistry
  22. }
  23. // GetGlobalUIRegistry returns the global UI registry (singleton)
  24. func GetGlobalUIRegistry() *types.UIRegistry {
  25. uiRegistryOnce.Do(func() {
  26. globalUIRegistry = types.NewUIRegistry()
  27. glog.V(1).Infof("Created global UI registry")
  28. })
  29. return globalUIRegistry
  30. }
  31. // GetGlobalTaskRegistry returns the global task registry (singleton)
  32. func GetGlobalTaskRegistry() *TaskRegistry {
  33. taskRegistryOnce.Do(func() {
  34. globalTaskRegistry = NewTaskRegistry()
  35. glog.V(1).Infof("Created global task registry")
  36. })
  37. return globalTaskRegistry
  38. }
  39. // AutoRegister registers a task with the global task registry
  40. func AutoRegister(taskType types.TaskType, factory types.TaskFactory) {
  41. registry := GetGlobalTaskRegistry()
  42. registry.Register(taskType, factory)
  43. glog.V(1).Infof("Auto-registered task type: %s", taskType)
  44. }
  45. // AutoRegisterTypes registers a task with the global types registry
  46. func AutoRegisterTypes(registerFunc func(*types.TaskRegistry)) {
  47. registry := GetGlobalTypesRegistry()
  48. registerFunc(registry)
  49. glog.V(1).Infof("Auto-registered task with types registry")
  50. }
  51. // AutoRegisterUI registers a UI provider with the global UI registry
  52. func AutoRegisterUI(registerFunc func(*types.UIRegistry)) {
  53. registry := GetGlobalUIRegistry()
  54. registerFunc(registry)
  55. glog.V(1).Infof("Auto-registered task UI provider")
  56. }
  57. // SetDefaultCapabilitiesFromRegistry sets the default worker capabilities
  58. // based on all registered task types
  59. func SetDefaultCapabilitiesFromRegistry() {
  60. typesRegistry := GetGlobalTypesRegistry()
  61. var capabilities []types.TaskType
  62. for taskType := range typesRegistry.GetAllDetectors() {
  63. capabilities = append(capabilities, taskType)
  64. }
  65. // Set the default capabilities in the types package
  66. types.SetDefaultCapabilities(capabilities)
  67. glog.V(1).Infof("Set default worker capabilities from registry: %v", capabilities)
  68. }
  69. // BuildMaintenancePolicyFromTasks creates a maintenance policy with default configurations
  70. // from all registered tasks using their UI providers
  71. func BuildMaintenancePolicyFromTasks() *types.MaintenancePolicy {
  72. policy := types.NewMaintenancePolicy()
  73. // Get all registered task types from the UI registry
  74. uiRegistry := GetGlobalUIRegistry()
  75. for taskType, provider := range uiRegistry.GetAllProviders() {
  76. // Get the default configuration from the UI provider
  77. defaultConfig := provider.GetCurrentConfig()
  78. // Set the configuration in the policy
  79. policy.SetTaskConfig(taskType, defaultConfig)
  80. glog.V(3).Infof("Added default config for task type %s to policy", taskType)
  81. }
  82. glog.V(2).Infof("Built maintenance policy with %d task configurations", len(policy.TaskConfigs))
  83. return policy
  84. }
  85. // SetMaintenancePolicyFromTasks sets the default maintenance policy from registered tasks
  86. func SetMaintenancePolicyFromTasks() {
  87. // This function can be called to initialize the policy from registered tasks
  88. // For now, we'll just log that this should be called by the integration layer
  89. glog.V(1).Infof("SetMaintenancePolicyFromTasks called - policy should be built by the integration layer")
  90. }
  91. // TaskRegistry manages task factories
  92. type TaskRegistry struct {
  93. factories map[types.TaskType]types.TaskFactory
  94. mutex sync.RWMutex
  95. }
  96. // NewTaskRegistry creates a new task registry
  97. func NewTaskRegistry() *TaskRegistry {
  98. return &TaskRegistry{
  99. factories: make(map[types.TaskType]types.TaskFactory),
  100. }
  101. }
  102. // Register adds a factory to the registry
  103. func (r *TaskRegistry) Register(taskType types.TaskType, factory types.TaskFactory) {
  104. r.mutex.Lock()
  105. defer r.mutex.Unlock()
  106. r.factories[taskType] = factory
  107. }
  108. // Get returns a factory from the registry
  109. func (r *TaskRegistry) Get(taskType types.TaskType) types.TaskFactory {
  110. r.mutex.RLock()
  111. defer r.mutex.RUnlock()
  112. return r.factories[taskType]
  113. }
  114. // GetAll returns all registered factories
  115. func (r *TaskRegistry) GetAll() map[types.TaskType]types.TaskFactory {
  116. r.mutex.RLock()
  117. defer r.mutex.RUnlock()
  118. result := make(map[types.TaskType]types.TaskFactory)
  119. for k, v := range r.factories {
  120. result[k] = v
  121. }
  122. return result
  123. }