diff --git a/content/schedule.go b/content/schedule.go index 2c3b9a1..9b5fff3 100644 --- a/content/schedule.go +++ b/content/schedule.go @@ -17,11 +17,12 @@ var Shuffled bool type Scheduler struct { Content struct { - Programs []*Program + CheckInterval string + Programs []*Program } } -func (s *Scheduler) Run(currentTime time.Time) error { +func (s *Scheduler) Run() error { log.Info("Starting Daemon") log.Infof("Press ESC to quit") @@ -31,37 +32,55 @@ func (s *Scheduler) Run(currentTime time.Time) error { signal.Notify(sigchnl) exitchnl := make(chan int) - // check content from scheduler and run through it - for _, p := range s.Content.Programs { - now := currentTime - log.Debugf("program %v", formatter.StructToIndentedString(p)) + totalPrograms := len(s.Content.Programs) + programIndex := 0 + + // run operation in loop + for programIndex <= totalPrograms { + // check content from scheduler and run through it + for _, p := range s.Content.Programs { + now := time.Now() + log.Debugf("program %v", formatter.StructToIndentedString(p)) + + if p.Timeslot.IsScheduledNow(now) { + log.Infof("getting media type: %v", p.Type) + content := p.GetMedia() + log.Debugf("media struct: %v", content) + err := content.Get() // retrieve contents from file + if err != nil { + return err + } + go func() { + for { + stop := <-sigchnl + s.Stop(stop, content) + } + }() + err = content.Play() + if err != nil { + return err + } // play will block until done + } - if p.Timeslot.IsScheduledNow(now) { - log.Infof("getting media type: %v", p.Type) - content := p.GetMedia() - log.Debugf("media struct: %v", content) - err := content.Get() // retrieve contents from file - if err != nil { - return err + if !p.Timeslot.IsScheduledNow(now) { + log.WithField("IsScheduledNow", p.Timeslot.IsScheduledNow(now)). + WithField("current time", time.Now(). + Format(time.Kitchen)).Infof("media not scheduled") } - go func() { - for { - stop := <-sigchnl - s.Stop(stop, content) + programIndex++ // increment index + if programIndex > totalPrograms { + programIndex = 0 + + // get the scheduled check interval from the scheduler + interval, err := time.ParseDuration(s.Content.CheckInterval) + if err != nil { + return err } - }() - err = content.Play() - if err != nil { - return err - } // play will block until done - } - - if !p.Timeslot.IsScheduledNow(now) { - log.WithField("IsScheduledNow", p.Timeslot.IsScheduledNow(now)). - WithField("current time", time.Now(). - Format(time.Kitchen)).Infof("media not scheduled") + // pause the loop + log.WithField("pause interval", s.Content.CheckInterval).Info("loop paused, will resume after pause interval") + time.Sleep(interval) + } } - // TODO make these checks run in a loop and always check if programs should be playing } exitcode := <-exitchnl diff --git a/content/schedule_test.go b/content/schedule_test.go index 073a949..856bb60 100644 --- a/content/schedule_test.go +++ b/content/schedule_test.go @@ -27,17 +27,22 @@ func TestNewScheduler(t *testing.T) { }, want: &want{ scheduler: &Scheduler{ - Content: struct{ Programs []*Program }{Programs: []*Program{ - { - Name: "gettysburg10", - Source: "./static/gettysburg10.wav", - Timeslot: &Timeslot{ - Begin: "11:00PM", - End: "11:30PM", + Content: struct { + CheckInterval string + Programs []*Program + }{ + CheckInterval: "1m", + Programs: []*Program{ + { + Name: "gettysburg10", + Source: "./static/gettysburg10.wav", + Timeslot: &Timeslot{ + Begin: "11:00PM", + End: "11:30PM", + }, + Type: MediaType("file"), }, - Type: MediaType("file"), - }, - }}, + }}, }, }, wantErr: false, diff --git a/main.go b/main.go index b10ef55..cb2c96b 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( log "github.com/sirupsen/logrus" "github.com/urfave/cli" "os" - "time" ) const ( @@ -38,7 +37,7 @@ func main() { return } // run content normally - err = scheduler.Run(time.Now()) + err = scheduler.Run() if err != nil { log.WithError(err).Error("unable to run go-dj") }