Skip to content

Commit

Permalink
Implement notification system for Config
Browse files Browse the repository at this point in the history
I want cloud-init to depend on the Conf, and not vice-versa. So I must
somehow call the cloud-init utilities from the config. My solution:
callbacks
  • Loading branch information
EduardGomezEscandell committed Jan 23, 2024
1 parent 3a502e3 commit 7bfe8f5
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions windows-agent/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type Config struct {

// Sync
mu *sync.Mutex

// notifiers are called after any configuration changes.
notifiers []func()
}

// New creates and initializes a new Config object.
Expand All @@ -44,6 +47,11 @@ func New(ctx context.Context, cachePath string) (m *Config) {
return m
}

// Notify appends a callback. It'll be called very time any configuration changes.
func (c *Config) Notify(f func()) {
c.notifiers = append(c.notifiers, f)
}

// Subscription returns the ProToken and the method it was acquired with (if any).
func (c *Config) Subscription(ctx context.Context) (token string, source Source, err error) {
c.mu.Lock()
Expand Down Expand Up @@ -130,6 +138,10 @@ func (c *Config) set(ctx context.Context, field *string, value string) error {
old := *field
*field = value

for _, f := range c.notifiers {
f()
}

if err := c.dump(); err != nil {
log.Errorf(ctx, "Could not update settings: %v", err)
*field = old
Expand Down Expand Up @@ -250,6 +262,10 @@ func (c *Config) collectRegistrySettingsTasks(ctx context.Context, data Registry
log.Warningf(ctx, "Could not obtain some updated registry settings: %v", errs)
}

for _, f := range c.notifiers {
f()
}

// Dump updated checksums
if err := c.dump(); err != nil {
return nil, fmt.Errorf("could not store updated registry settings: %v", err)
Expand Down

0 comments on commit 7bfe8f5

Please sign in to comment.