Skip to content

Modern Docker Management, Designed for Everyone

refactor: add fs watcher utility to sync the stacks when a change is made on the filesystem

Kyle Mendellf465d3645e0d
diff --git a/backend/go.mod b/backend/go.mod
index 8ade76923..992e69ed7 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -6,6 +6,7 @@ require (
github.com/compose-spec/compose-go/v2 v2.6.4
github.com/docker/docker v28.2.2+incompatible
github.com/docker/go-connections v0.5.0
+ github.com/fsnotify/fsnotify v1.9.0
github.com/gin-contrib/cors v1.7.5
github.com/gin-gonic/gin v1.10.1
github.com/glebarez/sqlite v1.11.0
diff --git a/backend/go.sum b/backend/go.sum
index abd824328..4eb77e6f0 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -37,6 +37,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
+github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
+github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
github.com/gin-contrib/cors v1.7.5 h1:cXC9SmofOrRg0w9PigwGlHG3ztswH6bqq4vJVXnvYMk=
diff --git a/backend/internal/bootstrap/jobs_bootstrap.go b/backend/internal/bootstrap/jobs_bootstrap.go
index a0d2e5649..b57e30df3 100644
--- a/backend/internal/bootstrap/jobs_bootstrap.go
+++ b/backend/internal/bootstrap/jobs_bootstrap.go
@@ -29,4 +29,8 @@ func registerJobs(appCtx context.Context, scheduler *job.Scheduler, appServices
if err := job.RegisterEventCleanupJob(appCtx, scheduler, appServices.Event); err != nil {
slog.ErrorContext(appCtx, "Failed to register event cleanup job", slog.Any("error", err))
}
+
+ if err := job.RegisterFilesystemWatcherJob(appCtx, scheduler, appServices.Stack, appServices.Settings); err != nil {
+ slog.ErrorContext(appCtx, "Failed to register filesystem watcher job", slog.Any("error", err))
+ }
}
diff --git a/backend/internal/job/filesystem_watcher_job.go b/backend/internal/job/filesystem_watcher_job.go
new file mode 100644
index 000000000..a362109fa
--- /dev/null
+++ b/backend/internal/job/filesystem_watcher_job.go
@@ -0,0 +1,107 @@
+package job
+
+import (
+ "context"
+ "log/slog"
+ "os"
+ "time"
+
+ "github.com/ofkm/arcane-backend/internal/services"
+ "github.com/ofkm/arcane-backend/internal/utils"
+)
+
+type FilesystemWatcherJob struct {
+ stackService *services.StackService
+ settingsService *services.SettingsService
+ watcher *utils.FilesystemWatcher
+}
+
+func NewFilesystemWatcherJob(
+ stackService *services.StackService,
+ settingsService *services.SettingsService,
+) *FilesystemWatcherJob {
+ return &FilesystemWatcherJob{
+ stackService: stackService,
+ settingsService: settingsService,
+ }
+}
+
+func RegisterFilesystemWatcherJob(ctx context.Context, scheduler *Scheduler, stackService *services.StackService, settingsService *services.SettingsService) error {
+ job := NewFilesystemWatcherJob(stackService, settingsService)
+
+ // Start the filesystem watcher as a background task
+ go func() {
+ if err := job.Start(ctx); err != nil {
+ slog.ErrorContext(ctx, "Filesystem watcher failed", "error", err)
+ }
+ }()
+
+ slog.InfoContext(ctx, "Filesystem watcher job registered")
+ return nil
+}
+
+func (j *FilesystemWatcherJob) Start(ctx context.Context) error {
+ // Get stacks directory from settings
+ stacksDir, err := j.getStacksDirectory(ctx)
+ if err != nil {
+ return err
+ }
+
+ // Ensure directory exists
+ if _, err := os.Stat(stacksDir); os.IsNotExist(err) {
+ if err := os.MkdirAll(stacksDir, 0755); err != nil {
+ return err
+ }
+ slog.InfoContext(ctx, "Created stacks directory", "path", stacksDir)
+ }
+
+ // Create filesystem watcher
+ watcher, err := utils.NewFilesystemWatcher(stacksDir, utils.WatcherOptions{
+ Debounce: 3 * time.Second, // Wait 3 seconds after last change before syncing
+ OnChange: j.handleFilesystemChange,
+ })
+ if err != nil {
+ return err
+ }
+
+ j.watcher = watcher
+
+ // Start watching
+ if err := j.watcher.Start(ctx); err != nil {
+ return err
+ }
+
+ slog.InfoContext(ctx, "Filesystem watcher started for stacks directory",
+ "path", stacksDir)
+
+ // Keep the watcher running until context is cancelled
+ <-ctx.Done()
+
+ return j.Stop()
+}
+
+func (j *FilesystemWatcherJob) Stop() error {
+ if j.watcher != nil {
+ return j.watcher.Stop()
+ }
+ return nil
+}
+
+func (j *FilesystemWatcherJob) handleFilesystemChange(ctx context.Context) {
+ slog.InfoContext(ctx, "Filesystem change detected, syncing stacks")
+
+ if err := j.stackService.SyncAllStacksFromFilesystem(ctx); err != nil {
+ slog.ErrorContext(ctx, "Failed to sync stacks after filesystem change",
+ "error", err)
+ } else {
+ slog.InfoContext(ctx, "Stack sync completed after filesystem change")
+ }
+}
+
+func (j *FilesystemWatcherJob) getStacksDirectory(ctx context.Context) (string, error) {
+ settings, err := j.settingsService.GetSettings(ctx)
+ if err != nil {
+ return "data/projects", err
+ }
+ return settings.StacksDirectory.Value, nil
+}
diff --git a/backend/internal/services/stack_service.go b/backend/internal/services/stack_service.go
index 4d1142409..987094e1b 100644
--- a/backend/internal/services/stack_service.go
+++ b/backend/internal/services/stack_service.go
@@ -446,6 +446,7 @@ processResult:
return result, pagination, nil
}
+//nolint:gocognit
func (s *StackService) SyncAllStacksFromFilesystem(ctx context.Context) error {
stacksDir, dirErr := s.getStacksDirectory(ctx)
if dirErr != nil {
diff --git a/backend/internal/utils/filesystem_watcher.go b/backend/internal/utils/filesystem_watcher.go
new file mode 100644
index 000000000..2f261cfaa
--- /dev/null
+++ b/backend/internal/utils/filesystem_watcher.go
@@ -0,0 +1,208 @@
+package utils
+
+import (
+ "context"
+ "log/slog"
+ "os"
+ "path/filepath"
+ "time"
+
+ "github.com/fsnotify/fsnotify"
+)
+
+type FilesystemWatcher struct {
+ watcher *fsnotify.Watcher
+ watchedPath string
+ onChange func(ctx context.Context)
+ debounce time.Duration
+ stopCh chan struct{}
+ stoppedCh chan struct{}
+}
+
+type WatcherOptions struct {
+ Debounce time.Duration
+ OnChange func(ctx context.Context)
+}
+
+func NewFilesystemWatcher(watchPath string, opts WatcherOptions) (*FilesystemWatcher, error) {
+ watcher, err := fsnotify.NewWatcher()
+ if err != nil {
+ return nil, err
+ }
+
+ if opts.Debounce == 0 {
+ opts.Debounce = 2 * time.Second
+ }
+
+ return &FilesystemWatcher{
+ watcher: watcher,
+ watchedPath: watchPath,
+ onChange: opts.OnChange,
+ debounce: opts.Debounce,
+ stopCh: make(chan struct{}),
+ stoppedCh: make(chan struct{}),
+ }, nil
+}
+
+func (fw *FilesystemWatcher) Start(ctx context.Context) error {
+ if err := fw.watcher.Add(fw.watchedPath); err != nil {
+ return err
+ }
+
+ if err := fw.addExistingDirectories(fw.watchedPath); err != nil {
+ slog.WarnContext(ctx, "Failed to add some existing directories to watcher",
+ "path", fw.watchedPath,
+ "error", err)
+ }
+
+ go fw.watchLoop(ctx)
+
+ slog.InfoContext(ctx, "Filesystem watcher started", "path", fw.watchedPath)
+ return nil
+}
+
+func (fw *FilesystemWatcher) Stop() error {
+ close(fw.stopCh)
+ <-fw.stoppedCh // Wait for watchLoop to finish
+ return fw.watcher.Close()
+}
+
+func (fw *FilesystemWatcher) watchLoop(ctx context.Context) {
+ defer close(fw.stoppedCh)
+
+ debounceTimer := time.NewTimer(0)
+ if !debounceTimer.Stop() {
+ <-debounceTimer.C
+ }
+
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case <-fw.stopCh:
+ return
+ case event, ok := <-fw.watcher.Events:
+ if !ok {
+ return
+ }
+ if fw.shouldHandleEvent(event) {
+ fw.handleEvent(ctx, event, debounceTimer)
+ }
+ case err, ok := <-fw.watcher.Errors:
+ if !ok {
+ return
+ }
+ slog.ErrorContext(ctx, "Filesystem watcher error", "error", err)
+ }
+ }
+}
+
+func (fw *FilesystemWatcher) handleEvent(ctx context.Context, event fsnotify.Event, debounceTimer *time.Timer) {
+ if event.Has(fsnotify.Create) {
+ if info, err := os.Stat(event.Name); err == nil && info.IsDir() {
+ if err := fw.watcher.Add(event.Name); err != nil {
+ slog.WarnContext(ctx, "Failed to add new directory to watcher",
+ "path", event.Name,
+ "error", err)
+ }
+ }
+ }
+
+ slog.DebugContext(ctx, "Filesystem change detected",
+ "path", event.Name,
+ "operation", event.Op.String())
+
+ // Reset debounce timer
+ if !debounceTimer.Stop() {
+ select {
+ case <-debounceTimer.C:
+ default:
+ }
+ }
+ debounceTimer.Reset(fw.debounce)
+
+ // Start debounce handler if not already running
+ select {
+ case <-debounceTimer.C:
+ // Timer expired, trigger sync
+ if fw.onChange != nil {
+ go fw.onChange(ctx)
+ }
+ default:
+ // Timer still running, will trigger later
+ go fw.handleDebounce(ctx, debounceTimer)
+ }
+}
+
+func (fw *FilesystemWatcher) handleDebounce(ctx context.Context, timer *time.Timer) {
+ select {
+ case <-ctx.Done():
+ return
+ case <-fw.stopCh:
+ return
+ case <-timer.C:
+ if fw.onChange != nil {
+ fw.onChange(ctx)
+ }
+ }
+}
+
+func (fw *FilesystemWatcher) shouldHandleEvent(event fsnotify.Event) bool {
+ name := filepath.Base(event.Name)
+
+ // Skip temporary files and hidden files
+ if len(name) > 0 && (name[0] == '.' || name[0] == '~') {
+ return false
+ }
+
+ // Skip backup files
+ if filepath.Ext(name) == ".bak" || filepath.Ext(name) == ".tmp" {
+ return false
+ }
+
+ // We care about:
+ // 1. Directory operations (create/remove/rename)
+ // 2. Compose file changes
+ // 3. .env file changes
+ return event.Has(fsnotify.Create) ||
+ event.Has(fsnotify.Remove) ||
+ event.Has(fsnotify.Rename) ||
+ event.Has(fsnotify.Write) ||
+ isComposeFile(name) ||
+ name == ".env"
+}
+
+func (fw *FilesystemWatcher) addExistingDirectories(root string) error {
+ return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ slog.Warn("Error walking directory",
+ "path", path,
+ "error", err)
+ return err
+ }
+
+ if info.IsDir() && path != root {
+ // Only add directories that contain compose files or might contain them
+ if err := fw.watcher.Add(path); err != nil {
+ slog.Warn("Failed to add directory to watcher",
+ "path", path,
+ "error", err)
+ }
+ }
+ return nil
+ })
+}
+
+func isComposeFile(filename string) bool {
+ composeFiles := []string{
+ "compose.yaml", "compose.yml",
+ "docker-compose.yaml", "docker-compose.yml",
+ }
+
+ for _, cf := range composeFiles {
+ if filename == cf {
+ return true
+ }
+ }
+ return false
+}