From 541ad90b3d0c5abe207ec49fb5be63767cd21e3c Mon Sep 17 00:00:00 2001 From: Harshit <92732265+Harshit-shrivastav@users.noreply.github.com> Date: Sun, 29 Mar 2026 22:05:22 +0200 Subject: [PATCH] fix: preserve media store across config reloads - Reuse existing MediaStore instance instead of recreating on every reload - Don't stop MediaStore on reload to preserve file references and allow cleanup to continue - Prevents loss of uploaded file references when using /reload command Note: Changes to media_cleanup config require restart to take effect. Co-authored-by: Qwen-Coder --- pkg/gateway/gateway.go | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) 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)