Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7: implement schedule programming loop #20

Merged
merged 2 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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