Playlist log watcher was made into a struct.
This commit is contained in:
parent
3727f44381
commit
f631fdfa7c
@ -51,10 +51,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer logErr.Close()
|
defer logErr.Close()
|
||||||
|
|
||||||
if err := radio.IcecastWatchPlaylist(config.Icecast.Playlist, config.ListLastNSongs); err != nil {
|
playlistWatcher := radio.NewPlaylistLogWatcher()
|
||||||
|
if err := playlistWatcher.Watch(config.Icecast.Playlist, config.ListLastNSongs); err != nil {
|
||||||
logErr.Fatalln(err)
|
logErr.Fatalln(err)
|
||||||
}
|
}
|
||||||
defer radio.IcecastWatchClose()
|
defer playlistWatcher.Close()
|
||||||
|
|
||||||
hand := handlers.NewRadioHandlers(config, logErr)
|
hand := handlers.NewRadioHandlers(config, logErr)
|
||||||
srv := server.NewHttpServer()
|
srv := server.NewHttpServer()
|
||||||
|
@ -166,35 +166,45 @@ func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) {
|
|||||||
return songs, nil
|
return songs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var playlistWatcher watcher.InotifyWatcher
|
type PlaylistLogWatcher struct {
|
||||||
var playlistChanged chan uint32 = make(chan uint32)
|
watcher *watcher.InotifyWatcher
|
||||||
|
changed chan uint32
|
||||||
|
}
|
||||||
|
|
||||||
func IcecastWatchPlaylist(playlistPath string, lastNSongs int) error {
|
func NewPlaylistLogWatcher() *PlaylistLogWatcher {
|
||||||
playlistWatcher, err := watcher.NewInotifyWatcher()
|
return &PlaylistLogWatcher{changed: make(chan uint32)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pw *PlaylistLogWatcher) Watch(playlistPath string, n int) (err error) {
|
||||||
|
if pw.watcher != nil {
|
||||||
|
pw.watcher.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
pw.watcher, err = watcher.NewInotifyWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "cannot instantiate inotify watcher")
|
return errors.Wrap(err, "cannot instantiate inotify watcher")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = playlistWatcher.AddWatch(playlistPath, watcher.ModIgnMask)
|
err = pw.watcher.AddWatch(playlistPath, watcher.ModIgnMask)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "cannot set a playlist to watch")
|
return errors.Wrap(err, "cannot set a playlist to watch")
|
||||||
}
|
}
|
||||||
|
|
||||||
playlistWatcher.WatchForMask(playlistChanged, watcher.ModIgnMask)
|
pw.watcher.WatchForMask(pw.changed, watcher.ModIgnMask)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case mask := <-playlistChanged:
|
case mask := <-pw.changed:
|
||||||
if mask&syscall.IN_MODIFY > 0 {
|
if mask&syscall.IN_MODIFY > 0 {
|
||||||
lastPlayedCacheMutex.Lock()
|
lastPlayedCacheMutex.Lock()
|
||||||
if songs, err := icecastLastPlayedSongs(playlistPath, lastNSongs); err == nil {
|
if songs, err := icecastLastPlayedSongs(playlistPath, n); err == nil {
|
||||||
lastPlayedCache = songs
|
lastPlayedCache = songs
|
||||||
}
|
}
|
||||||
lastPlayedCacheMutex.Unlock()
|
lastPlayedCacheMutex.Unlock()
|
||||||
} else if mask&syscall.IN_IGNORED > 0 {
|
} else if mask&syscall.IN_IGNORED > 0 {
|
||||||
playlistWatcher.Close()
|
pw.Close()
|
||||||
IcecastWatchPlaylist(playlistPath, lastNSongs)
|
pw.Watch(playlistPath, n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -204,6 +214,6 @@ func IcecastWatchPlaylist(playlistPath string, lastNSongs int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func IcecastWatchClose() {
|
func (pw *PlaylistLogWatcher) Close() {
|
||||||
playlistWatcher.Close()
|
pw.watcher.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user