fix rescheduling issue
This commit is contained in:
parent
1f3e1d284d
commit
7b0cdac7c3
3 changed files with 27 additions and 17 deletions
28
main.go
28
main.go
|
@ -45,9 +45,8 @@ type ScheduledJob struct {
|
|||
func schedulePlayout(s *store.Store) fiber.Handler {
|
||||
// TODO return custom error
|
||||
return func(c *fiber.Ctx) error {
|
||||
var p playout.Job
|
||||
var p *playout.Job
|
||||
job := new(Job)
|
||||
|
||||
jsonErr := json.Unmarshal(c.Body(), job)
|
||||
if jsonErr != nil {
|
||||
log.Println("got defective request: ", jsonErr)
|
||||
|
@ -60,6 +59,18 @@ func schedulePlayout(s *store.Store) fiber.Handler {
|
|||
return errors.New("Got Empty Source. I can't play »Nothing«")
|
||||
}
|
||||
|
||||
var output string
|
||||
newPlayoutJob := new(playout.Job)
|
||||
s.RLock()
|
||||
olPlayoutJob, playoutExists := s.Playouts[job.ID]
|
||||
s.RUnlock()
|
||||
if playoutExists {
|
||||
p = olPlayoutJob
|
||||
} else {
|
||||
p = newPlayoutJob
|
||||
p.ControlChannel = make(chan string)
|
||||
}
|
||||
|
||||
p.ID = job.ID
|
||||
p.Source = job.Source
|
||||
p.Version = job.Version
|
||||
|
@ -70,17 +81,14 @@ func schedulePlayout(s *store.Store) fiber.Handler {
|
|||
p.StopAt = job.StopAt
|
||||
}
|
||||
|
||||
var output string
|
||||
s.RLock()
|
||||
oldPlayout, playoutExists := s.Playouts[job.ID]
|
||||
s.RUnlock()
|
||||
if playoutExists {
|
||||
oldPlayout.ControlChannel <- "reschedule"
|
||||
output = oldPlayout.Output
|
||||
p.ControlChannel <- "reschedule"
|
||||
output = p.Output
|
||||
} else {
|
||||
var err error
|
||||
|
||||
s.Lock()
|
||||
output, err = s.AddPlayout(&p)
|
||||
output, err = s.AddPlayout(p)
|
||||
s.Unlock()
|
||||
if err != nil {
|
||||
c.SendStatus(500)
|
||||
|
@ -100,7 +108,7 @@ func schedulePlayout(s *store.Store) fiber.Handler {
|
|||
for {
|
||||
select {
|
||||
case ctrlMsg := <-p.ControlChannel:
|
||||
fmt.Println(ctrlMsg)
|
||||
log.Println(ctrlMsg)
|
||||
continue
|
||||
case <-time.After(time.Until(p.StartAt)):
|
||||
break Waiting
|
||||
|
|
|
@ -95,6 +95,8 @@ func (p *Job) Playout(cfg *Config) {
|
|||
playoutScript := path.Join(cfg.PlayoutScriptPath, cfg.PlayoutScript)
|
||||
for {
|
||||
log.Printf("Start Playout for %v", p.ID)
|
||||
log.Println(p.StopAt)
|
||||
log.Println(time.Until(p.StopAt))
|
||||
cmd := exec.Command(playoutScript, //nolint:gosec
|
||||
"-i", p.Source,
|
||||
"-o", p.Output,
|
||||
|
|
|
@ -38,13 +38,13 @@ func NewStore(o []string, defaultDuration string, playoutScriptPath string, play
|
|||
|
||||
func (s *Store) AddPlayout(p *playout.Job) (string, error) {
|
||||
outputs := s.Outputs
|
||||
for _, value := range s.Playouts {
|
||||
if len(outputs) != 0 &&
|
||||
(value.StartAt.After(p.StartAt) && value.StartAt.Before(p.StopAt) ||
|
||||
value.StopAt.After(p.StartAt) && value.StopAt.Before(p.StopAt) ||
|
||||
p.StartAt.After(value.StartAt) && p.StartAt.Before(value.StopAt) ||
|
||||
p.StopAt.After(value.StartAt) && p.StopAt.Before(value.StopAt)) {
|
||||
inSlice, index := stringInSlice(value.Output, outputs)
|
||||
for _, job := range s.Playouts {
|
||||
if p.StartAt.After(job.StartAt) && p.StartAt.Before(job.StopAt) ||
|
||||
p.StopAt.After(job.StartAt) && p.StopAt.Before(job.StopAt) ||
|
||||
job.StartAt.After(p.StartAt) && job.StartAt.Before(p.StopAt) ||
|
||||
job.StopAt.After(p.StartAt) && job.StopAt.Before(p.StopAt) ||
|
||||
job.StartAt == p.StartAt || job.StopAt == p.StopAt {
|
||||
inSlice, index := stringInSlice(job.Output, outputs)
|
||||
if inSlice {
|
||||
outputs = remove(outputs, index)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue