mediamtx/internal/externalcmd/cmd.go

80 lines
1.0 KiB
Go

package externalcmd
import (
"time"
)
const (
retryPause = 5 * time.Second
)
// Environment is a Cmd environment.
type Environment struct {
Path string
Port string
}
// Cmd is an external command.
type Cmd struct {
cmdstr string
restart bool
env Environment
// in
terminate chan struct{}
// out
done chan struct{}
}
// New allocates an Cmd.
func New(cmdstr string, restart bool, env Environment) *Cmd {
e := &Cmd{
cmdstr: cmdstr,
restart: restart,
env: env,
terminate: make(chan struct{}),
done: make(chan struct{}),
}
go e.run()
return e
}
// Close closes an Cmd.
func (e *Cmd) Close() {
close(e.terminate)
<-e.done
}
func (e *Cmd) run() {
defer close(e.done)
for {
ok := func() bool {
ok := e.runInner()
if !ok {
return false
}
if !e.restart {
<-e.terminate
return false
}
t := time.NewTimer(retryPause)
defer t.Stop()
select {
case <-t.C:
return true
case <-e.terminate:
return false
}
}()
if !ok {
break
}
}
}