aboutsummaryrefslogtreecommitdiff
path: root/weed/worker/tasks/config_update_registry.go
blob: 649c8b384683f8ff3ac42b6b5a9fa5f3af4371db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package tasks

import (
	"sync"

	"github.com/seaweedfs/seaweedfs/weed/glog"
	"github.com/seaweedfs/seaweedfs/weed/worker/types"
)

// ConfigUpdateFunc is a function type for updating task configurations
type ConfigUpdateFunc func(configPersistence interface{}) error

// ConfigUpdateRegistry manages config update functions for all task types
type ConfigUpdateRegistry struct {
	updaters map[types.TaskType]ConfigUpdateFunc
	mutex    sync.RWMutex
}

var (
	globalConfigUpdateRegistry *ConfigUpdateRegistry
	configUpdateRegistryOnce   sync.Once
)

// GetGlobalConfigUpdateRegistry returns the global config update registry (singleton)
func GetGlobalConfigUpdateRegistry() *ConfigUpdateRegistry {
	configUpdateRegistryOnce.Do(func() {
		globalConfigUpdateRegistry = &ConfigUpdateRegistry{
			updaters: make(map[types.TaskType]ConfigUpdateFunc),
		}
		glog.V(1).Infof("Created global config update registry")
	})
	return globalConfigUpdateRegistry
}

// RegisterConfigUpdater registers a config update function for a task type
func (r *ConfigUpdateRegistry) RegisterConfigUpdater(taskType types.TaskType, updateFunc ConfigUpdateFunc) {
	r.mutex.Lock()
	defer r.mutex.Unlock()
	r.updaters[taskType] = updateFunc
	glog.V(1).Infof("Registered config updater for task type: %s", taskType)
}

// UpdateAllConfigs updates configurations for all registered task types
func (r *ConfigUpdateRegistry) UpdateAllConfigs(configPersistence interface{}) {
	r.mutex.RLock()
	updaters := make(map[types.TaskType]ConfigUpdateFunc)
	for k, v := range r.updaters {
		updaters[k] = v
	}
	r.mutex.RUnlock()

	for taskType, updateFunc := range updaters {
		if err := updateFunc(configPersistence); err != nil {
			glog.Warningf("Failed to load %s configuration from persistence: %v", taskType, err)
		} else {
			glog.V(1).Infof("Loaded %s configuration from persistence", taskType)
		}
	}

	glog.V(1).Infof("All task configurations loaded from persistence")
}

// AutoRegisterConfigUpdater is a convenience function for registering config updaters
func AutoRegisterConfigUpdater(taskType types.TaskType, updateFunc ConfigUpdateFunc) {
	registry := GetGlobalConfigUpdateRegistry()
	registry.RegisterConfigUpdater(taskType, updateFunc)
}