diff --git a/pkg/gateway/gateway.go b/pkg/gateway/gateway.go index c35b3e744..2b28130a8 100644 --- a/pkg/gateway/gateway.go +++ b/pkg/gateway/gateway.go @@ -281,13 +281,17 @@ func setupAndStartServices( } fmt.Println("✓ Heartbeat service started") - runningServices.MediaStore = media.NewFileMediaStoreWithCleanup(media.MediaCleanerConfig{ - Enabled: cfg.Tools.MediaCleanup.Enabled, - MaxAge: time.Duration(cfg.Tools.MediaCleanup.MaxAge) * time.Minute, - Interval: time.Duration(cfg.Tools.MediaCleanup.Interval) * time.Minute, - }) - if fms, ok := runningServices.MediaStore.(*media.FileMediaStore); ok { - fms.Start() + // Initialize MediaStore for file uploads + // Note: Changes to media_cleanup config require restart to take effect + if runningServices.MediaStore == nil { + runningServices.MediaStore = media.NewFileMediaStoreWithCleanup(media.MediaCleanerConfig{ + Enabled: cfg.Tools.MediaCleanup.Enabled, + MaxAge: time.Duration(cfg.Tools.MediaCleanup.MaxAge) * time.Minute, + Interval: time.Duration(cfg.Tools.MediaCleanup.Interval) * time.Minute, + }) + if fms, ok := runningServices.MediaStore.(*media.FileMediaStore); ok { + fms.Start() + } } runningServices.ChannelManager, err = channels.NewManager(cfg, msgBus, runningServices.MediaStore) @@ -359,7 +363,9 @@ func stopAndCleanupServices(runningServices *services, shutdownTimeout time.Dura if runningServices.CronService != nil { runningServices.CronService.Stop() } - if runningServices.MediaStore != nil { + // Don't stop MediaStore on reload to preserve file references + // and allow cleanup goroutine to continue running + if !isReload && runningServices.MediaStore != nil { if fms, ok := runningServices.MediaStore.(*media.FileMediaStore); ok { fms.Stop() } @@ -480,13 +486,17 @@ func restartServices( } fmt.Println(" ✓ Heartbeat service restarted") - runningServices.MediaStore = media.NewFileMediaStoreWithCleanup(media.MediaCleanerConfig{ - Enabled: cfg.Tools.MediaCleanup.Enabled, - MaxAge: time.Duration(cfg.Tools.MediaCleanup.MaxAge) * time.Minute, - Interval: time.Duration(cfg.Tools.MediaCleanup.Interval) * time.Minute, - }) - if fms, ok := runningServices.MediaStore.(*media.FileMediaStore); ok { - fms.Start() + // Reuse existing MediaStore to preserve file references across reloads + // Note: Changes to media_cleanup config require restart to take effect + if runningServices.MediaStore == nil { + runningServices.MediaStore = media.NewFileMediaStoreWithCleanup(media.MediaCleanerConfig{ + Enabled: cfg.Tools.MediaCleanup.Enabled, + MaxAge: time.Duration(cfg.Tools.MediaCleanup.MaxAge) * time.Minute, + Interval: time.Duration(cfg.Tools.MediaCleanup.Interval) * time.Minute, + }) + if fms, ok := runningServices.MediaStore.(*media.FileMediaStore); ok { + fms.Start() + } } al.SetMediaStore(runningServices.MediaStore)