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 <qwen-coder@alibabacloud.com>
This commit is contained in:
Harshit 2026-03-29 22:05:22 +02:00
parent 93f4c4a843
commit 541ad90b3d

View file

@ -281,13 +281,17 @@ func setupAndStartServices(
} }
fmt.Println("✓ Heartbeat service started") fmt.Println("✓ Heartbeat service started")
runningServices.MediaStore = media.NewFileMediaStoreWithCleanup(media.MediaCleanerConfig{ // Initialize MediaStore for file uploads
Enabled: cfg.Tools.MediaCleanup.Enabled, // Note: Changes to media_cleanup config require restart to take effect
MaxAge: time.Duration(cfg.Tools.MediaCleanup.MaxAge) * time.Minute, if runningServices.MediaStore == nil {
Interval: time.Duration(cfg.Tools.MediaCleanup.Interval) * time.Minute, runningServices.MediaStore = media.NewFileMediaStoreWithCleanup(media.MediaCleanerConfig{
}) Enabled: cfg.Tools.MediaCleanup.Enabled,
if fms, ok := runningServices.MediaStore.(*media.FileMediaStore); ok { MaxAge: time.Duration(cfg.Tools.MediaCleanup.MaxAge) * time.Minute,
fms.Start() 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) runningServices.ChannelManager, err = channels.NewManager(cfg, msgBus, runningServices.MediaStore)
@ -359,7 +363,9 @@ func stopAndCleanupServices(runningServices *services, shutdownTimeout time.Dura
if runningServices.CronService != nil { if runningServices.CronService != nil {
runningServices.CronService.Stop() 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 { if fms, ok := runningServices.MediaStore.(*media.FileMediaStore); ok {
fms.Stop() fms.Stop()
} }
@ -480,13 +486,17 @@ func restartServices(
} }
fmt.Println(" ✓ Heartbeat service restarted") fmt.Println(" ✓ Heartbeat service restarted")
runningServices.MediaStore = media.NewFileMediaStoreWithCleanup(media.MediaCleanerConfig{ // Reuse existing MediaStore to preserve file references across reloads
Enabled: cfg.Tools.MediaCleanup.Enabled, // Note: Changes to media_cleanup config require restart to take effect
MaxAge: time.Duration(cfg.Tools.MediaCleanup.MaxAge) * time.Minute, if runningServices.MediaStore == nil {
Interval: time.Duration(cfg.Tools.MediaCleanup.Interval) * time.Minute, runningServices.MediaStore = media.NewFileMediaStoreWithCleanup(media.MediaCleanerConfig{
}) Enabled: cfg.Tools.MediaCleanup.Enabled,
if fms, ok := runningServices.MediaStore.(*media.FileMediaStore); ok { MaxAge: time.Duration(cfg.Tools.MediaCleanup.MaxAge) * time.Minute,
fms.Start() Interval: time.Duration(cfg.Tools.MediaCleanup.Interval) * time.Minute,
})
if fms, ok := runningServices.MediaStore.(*media.FileMediaStore); ok {
fms.Start()
}
} }
al.SetMediaStore(runningServices.MediaStore) al.SetMediaStore(runningServices.MediaStore)