1
0

Do not overwrite mostlistenedsong file if a song wasn't changed.

This commit is contained in:
Alexander Andreev 2023-09-10 18:23:06 +04:00
parent cbf8eb8747
commit c020031127
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 13 additions and 3 deletions

View File

@ -74,9 +74,12 @@ func main() {
} }
defer func() { defer func() {
err := os.WriteFile(*mostListenedSongPath, radio.StoreMostListenedSong(), fs.ModePerm) fileData := radio.StoreMostListenedSong()
if err != nil { if fileData != nil {
log.Println("failed to store a most listened song:", err) err := os.WriteFile(*mostListenedSongPath, fileData, fs.ModePerm)
if err != nil {
log.Println("failed to store a most listened song:", err)
}
} }
if err := srv.Stop(); err != nil { if err := srv.Stop(); err != nil {

View File

@ -9,6 +9,8 @@ import (
const MostListenedDateFormat = "02 January 2006" const MostListenedDateFormat = "02 January 2006"
var mlsChanged = false
type MostListenedSong struct { type MostListenedSong struct {
Listeners int Listeners int
Date time.Time Date time.Time
@ -43,6 +45,7 @@ func CheckAndUpdateMostListenedSong(cur, prev Song) {
Date: time.Now().UTC(), Date: time.Now().UTC(),
Song: prev.Song} Song: prev.Song}
} }
mlsChanged = true
} }
// MostListened returns song that currently is the song with most simultaneous // MostListened returns song that currently is the song with most simultaneous
@ -68,6 +71,10 @@ func LoadMostListenedSong(data []byte) (err error) {
} }
func StoreMostListenedSong() []byte { func StoreMostListenedSong() []byte {
if !mlsChanged {
return nil
}
buf := make([]byte, 0, 30+len(mostListened.Song)) buf := make([]byte, 0, 30+len(mostListened.Song))
b := bytes.NewBuffer(buf) b := bytes.NewBuffer(buf)