From 007691c534a9949369ddced31ed31795923ede8d Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 21 Aug 2023 04:39:38 +0400 Subject: [PATCH] Defer server Stop and writing of MLS instead of call after doneSignal fired. --- cmd/dwelling-radio/main.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/dwelling-radio/main.go b/cmd/dwelling-radio/main.go index ef8fdd3..27c42d5 100644 --- a/cmd/dwelling-radio/main.go +++ b/cmd/dwelling-radio/main.go @@ -73,14 +73,19 @@ func main() { log.Fatalln(err) } + defer func() { + err := os.WriteFile(*mostListenedSongPath, radio.StoreMostListenedSong(), fs.ModePerm) + if err != nil { + log.Println("Failed to store most listened song:", err) + } + + if err := srv.Stop(); err != nil { + log.Fatalln(err) + } + }() + doneSignal := make(chan os.Signal, 1) signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGSEGV) <-doneSignal - - os.WriteFile(*mostListenedSongPath, radio.StoreMostListenedSong(), fs.ModePerm) - - if err := srv.Stop(); err != nil { - log.Fatalln(err) - } }