Skip to content

Modern Docker Management, Designed for Everyone

fix: remove double loading of env overides and settings, use in memory cache instead (#2562)

Kyle Mendellc6aab174e50f
diff --git a/backend/internal/services/settings_service.go b/backend/internal/services/settings_service.go
index 48dad819d..1408c9ea4 100644
--- a/backend/internal/services/settings_service.go
+++ b/backend/internal/services/settings_service.go
@@ -503,7 +503,7 @@ func (s *SettingsService) isEnvOverrideActiveInternal(key string) bool {
}
func (s *SettingsService) GetSettings(ctx context.Context) (*models.Settings, error) {
- settings := s.getEffectiveSettingsConfig(ctx)
+ settings := s.getEffectiveSettingsConfigInternal(ctx)
return settings, nil
}
@@ -522,7 +522,7 @@ func (s *SettingsService) GetSettingsOrDefaults(ctx context.Context) *models.Set
return cfg
}
-func (s *SettingsService) getEffectiveSettingsConfig(ctx context.Context) *models.Settings {
+func (s *SettingsService) getEffectiveSettingsConfigInternal(ctx context.Context) *models.Settings {
settings := s.GetSettingsConfig().Clone()
s.applyEnvOverrides(ctx, settings)
return settings
@@ -656,7 +656,6 @@ func (s *SettingsService) updateSettingValueNoRefreshInternal(ctx context.Contex
func (s *SettingsService) UpdateSettings(ctx context.Context, updates settings.Update) ([]models.SettingVariable, error) {
defaultCfg := s.getDefaultSettings()
cfg := s.GetSettingsConfig().Clone()
- s.applyEnvOverrides(ctx, cfg)
valuesToUpdate, changedPolling, changedAutoUpdate, changedScheduledPrune, changedVulnerabilityScan, changedAutoHeal, changedTimeouts, err := s.prepareUpdateValues(updates, cfg, defaultCfg)
if err != nil {
@@ -1080,7 +1079,7 @@ func (s *SettingsService) setupInstanceID(ctx context.Context) error {
}
func (s *SettingsService) GetBoolSetting(ctx context.Context, key string, defaultValue bool) bool {
- cfg := s.getEffectiveSettingsConfig(ctx)
+ cfg := s.getEffectiveSettingsConfigInternal(ctx)
val, _, _, err := cfg.FieldByKey(key)
if err != nil || val == "" {
return defaultValue
@@ -1093,7 +1092,7 @@ func (s *SettingsService) GetBoolSetting(ctx context.Context, key string, defaul
}
func (s *SettingsService) GetIntSetting(ctx context.Context, key string, defaultValue int) int {
- cfg := s.getEffectiveSettingsConfig(ctx)
+ cfg := s.getEffectiveSettingsConfigInternal(ctx)
val, _, _, err := cfg.FieldByKey(key)
if err != nil || val == "" {
return defaultValue
@@ -1106,7 +1105,7 @@ func (s *SettingsService) GetIntSetting(ctx context.Context, key string, default
}
func (s *SettingsService) GetStringSetting(ctx context.Context, key, defaultValue string) string {
- cfg := s.getEffectiveSettingsConfig(ctx)
+ cfg := s.getEffectiveSettingsConfigInternal(ctx)
val, _, _, err := cfg.FieldByKey(key)
if err != nil || val == "" {
return defaultValue
diff --git a/backend/pkg/fswatch/watcher.go b/backend/pkg/fswatch/watcher.go
index a661f18d3..69ba667bd 100644
--- a/backend/pkg/fswatch/watcher.go
+++ b/backend/pkg/fswatch/watcher.go
@@ -24,6 +24,7 @@ type Watcher struct {
debounce time.Duration
stopCh chan struct{}
stoppedCh chan struct{}
+ watchAliases map[string]string
mu sync.Mutex
started bool
stopped bool
@@ -61,6 +62,7 @@ func NewWatcher(watchPath string, opts WatcherOptions) (*Watcher, error) {
debounce: opts.Debounce,
stopCh: make(chan struct{}),
stoppedCh: make(chan struct{}),
+ watchAliases: make(map[string]string),
}, nil
}
@@ -201,9 +203,10 @@ func (fw *Watcher) fireDebounceInternal(ctx context.Context, debouncePending *bo
// shouldHandleEventInternal's job.
func (fw *Watcher) handleEventInternal(ctx context.Context, event fsnotify.Event) {
if event.Has(fsnotify.Create) {
+ logicalName := fw.logicalPathForWatchEventInternal(event.Name)
if fw.isWatchableDirectory(event.Name) {
- if fw.shouldWatchDir(event.Name) {
- if err := fw.addExistingDirectories(event.Name); err != nil {
+ if fw.shouldWatchDir(logicalName) {
+ if err := fw.addExistingDirectoriesRecursiveInternal(event.Name, logicalName, map[string]struct{}{}); err != nil {
slog.WarnContext(ctx, "Failed to add new directory to watcher",
"path", event.Name,
"error", err)
@@ -233,10 +236,10 @@ func (fw *Watcher) shouldHandleEventInternal(event fsnotify.Event) bool {
}
func (fw *Watcher) addExistingDirectories(root string) error {
- return fw.addExistingDirectoriesRecursiveInternal(root, map[string]struct{}{})
+ return fw.addExistingDirectoriesRecursiveInternal(root, root, map[string]struct{}{})
}
-func (fw *Watcher) addExistingDirectoriesRecursiveInternal(path string, ancestors map[string]struct{}) error {
+func (fw *Watcher) addExistingDirectoriesRecursiveInternal(path string, logicalPath string, ancestors map[string]struct{}) error {
identity, err := projects.ResolveDirectoryIdentityInternal(path)
if err != nil {
return err
@@ -249,7 +252,7 @@ func (fw *Watcher) addExistingDirectoriesRecursiveInternal(path string, ancestor
defer delete(ancestors, identity)
if path != fw.watchedPath {
- depth := fw.dirDepth(path)
+ depth := fw.dirDepth(logicalPath)
if depth < 0 {
return nil
}
@@ -257,9 +260,11 @@ func (fw *Watcher) addExistingDirectoriesRecursiveInternal(path string, ancestor
return nil
}
- if err := fw.watcher.Add(path); err != nil {
+ watchPath := fw.resolveWatchPathInternal(path)
+ fw.watchAliases[watchPath] = filepath.Clean(logicalPath)
+ if err := fw.watcher.Add(watchPath); err != nil {
slog.Warn("Failed to add directory to watcher",
- "path", path,
+ "path", watchPath,
"error", err)
}
@@ -278,7 +283,8 @@ func (fw *Watcher) addExistingDirectoriesRecursiveInternal(path string, ancestor
if !projects.IsProjectDirectoryEntry(entry, childPath, fw.followSymlinks) {
continue
}
- if err := fw.addExistingDirectoriesRecursiveInternal(childPath, ancestors); err != nil {
+ childLogicalPath := filepath.Join(logicalPath, entry.Name())
+ if err := fw.addExistingDirectoriesRecursiveInternal(childPath, childLogicalPath, ancestors); err != nil {
return err
}
}
@@ -286,6 +292,48 @@ func (fw *Watcher) addExistingDirectoriesRecursiveInternal(path string, ancestor
return nil
}
+func (fw *Watcher) resolveWatchPathInternal(path string) string {
+ if !fw.followSymlinks {
+ return path
+ }
+
+ resolvedPath, err := filepath.EvalSymlinks(path)
+ if err != nil {
+ return path
+ }
+
+ return resolvedPath
+}
+
+func (fw *Watcher) logicalPathForWatchEventInternal(path string) string {
+ cleanPath := filepath.Clean(path)
+ bestWatchPath := ""
+ bestLogicalPath := ""
+
+ for watchPath, logicalPath := range fw.watchAliases {
+ cleanWatchPath := filepath.Clean(watchPath)
+ if cleanPath != cleanWatchPath && !strings.HasPrefix(cleanPath, cleanWatchPath+string(os.PathSeparator)) {
+ continue
+ }
+ if len(cleanWatchPath) <= len(bestWatchPath) {
+ continue
+ }
+ bestWatchPath = cleanWatchPath
+ bestLogicalPath = logicalPath
+ }
+
+ if bestWatchPath == "" {
+ return cleanPath
+ }
+
+ rel, err := filepath.Rel(bestWatchPath, cleanPath)
+ if err != nil || rel == "." {
+ return filepath.Clean(bestLogicalPath)
+ }
+
+ return filepath.Join(bestLogicalPath, rel)
+}
+
func (fw *Watcher) dirDepth(path string) int {
cleanRoot := fw.watchedPath
cleanPath := filepath.Clean(path)
diff --git a/backend/pkg/fswatch/watcher_test.go b/backend/pkg/fswatch/watcher_test.go
index d352a3999..df921a1ce 100644
--- a/backend/pkg/fswatch/watcher_test.go
+++ b/backend/pkg/fswatch/watcher_test.go
@@ -48,6 +48,48 @@ func TestWatcher_StartWatchesExistingSymlinkDirectoriesWhenEnabled(t *testing.T)
}
}
+func TestWatcher_StartWatchesNewDirectoriesInsideSymlinkDirectoriesWhenEnabled(t *testing.T) {
+ root := t.TempDir()
+ targetRoot := t.TempDir()
+ targetPath := filepath.Join(targetRoot, "real-project")
+ require.NoError(t, os.MkdirAll(targetPath, 0o755))
+
+ linkPath := filepath.Join(root, "linked-project")
+ require.NoError(t, os.Symlink(targetPath, linkPath))
+
+ changeCh := make(chan struct{}, 1)
+ ctx := t.Context()
+
+ watcher, err := NewWatcher(root, WatcherOptions{
+ Debounce: 25 * time.Millisecond,
+ MaxDepth: 2,
+ FollowSymlinkDirs: true,
+ OnChange: func(context.Context) {
+ select {
+ case changeCh <- struct{}{}:
+ default:
+ }
+ },
+ })
+ require.NoError(t, err)
+ require.NoError(t, watcher.Start(ctx))
+ defer func() {
+ require.NoError(t, watcher.Stop())
+ }()
+
+ nestedPath := filepath.Join(targetPath, "nested-project")
+ require.NoError(t, os.MkdirAll(nestedPath, 0o755))
+ time.Sleep(100 * time.Millisecond)
+
+ require.NoError(t, os.WriteFile(filepath.Join(nestedPath, "compose.yaml"), []byte("services: {}\n"), 0o644))
+
+ select {
+ case <-changeCh:
+ case <-time.After(2 * time.Second):
+ t.Fatal("expected compose file in new symlinked directory to trigger watcher callback")
+ }
+}
+
func TestWatcher_StartIgnoresNonProjectFileWritesInsideProjectDir(t *testing.T) {
root := t.TempDir()
projectDir := filepath.Join(root, "demo")