mediamtx/internal/externalcmd/cmd.go

90 lines
1.2 KiB
Go
Raw Normal View History

2022-09-17 19:19:45 +00:00
// Package externalcmd allows to launch external commands.
package externalcmd
import (
"strings"
"time"
)
const (
restartPause = 5 * time.Second
)
2020-11-21 12:35:33 +00:00
// Environment is a Cmd environment.
type Environment map[string]string
2020-11-01 16:33:06 +00:00
2020-11-21 12:35:33 +00:00
// Cmd is an external command.
type Cmd struct {
pool *Pool
2020-11-01 16:33:06 +00:00
cmdstr string
restart bool
env Environment
onExit func(int)
// in
terminate chan struct{}
}
// NewCmd allocates a Cmd.
func NewCmd(
pool *Pool,
cmdstr string,
restart bool,
env Environment,
onExit func(int),
) *Cmd {
for key, val := range env {
cmdstr = strings.ReplaceAll(cmdstr, "$"+key, val)
}
2020-11-21 12:35:33 +00:00
e := &Cmd{
pool: pool,
cmdstr: cmdstr,
restart: restart,
2020-11-01 16:33:06 +00:00
env: env,
onExit: onExit,
terminate: make(chan struct{}),
}
pool.wg.Add(1)
go e.run()
2021-08-01 14:58:46 +00:00
return e
}
// Close closes the command. It doesn't wait for the command to exit.
2020-11-21 12:35:33 +00:00
func (e *Cmd) Close() {
close(e.terminate)
}
2020-11-21 12:35:33 +00:00
func (e *Cmd) run() {
defer e.pool.wg.Done()
for {
2020-10-31 16:03:03 +00:00
ok := func() bool {
c, ok := e.runInner()
2020-10-31 16:03:03 +00:00
if !ok {
return false
}
e.onExit(c)
2020-10-31 16:03:03 +00:00
if !e.restart {
<-e.terminate
return false
}
2020-10-31 16:03:03 +00:00
select {
case <-time.After(restartPause):
2020-10-31 16:03:03 +00:00
return true
case <-e.terminate:
return false
}
}()
if !ok {
break
}
}
}