Skip to content

Commit

Permalink
Merge pull request #20 from jmillerv/7-scheduled-programming-loop
Browse files Browse the repository at this point in the history
7: implement schedule programming loop
  • Loading branch information
jmillerv authored Dec 6, 2022
2 parents 29e26a4 + 28a97cd commit 4779e5c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 41 deletions.
77 changes: 48 additions & 29 deletions content/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
25 changes: 15 additions & 10 deletions content/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"os"
"time"
)

const (
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 4779e5c

Please sign in to comment.