fix some controling issues
This commit is contained in:
parent
3c82aa0308
commit
1f3e1d284d
6 changed files with 142 additions and 92 deletions
1
go.mod
1
go.mod
|
@ -6,6 +6,7 @@ require (
|
||||||
github.com/andybalholm/brotli v1.0.1 // indirect
|
github.com/andybalholm/brotli v1.0.1 // indirect
|
||||||
github.com/fsnotify/fsnotify v1.4.9
|
github.com/fsnotify/fsnotify v1.4.9
|
||||||
github.com/gofiber/fiber/v2 v2.1.3
|
github.com/gofiber/fiber/v2 v2.1.3
|
||||||
|
github.com/grafov/bcast v0.0.0-20190217190352-1447f067e08d
|
||||||
github.com/ilyakaznacheev/cleanenv v1.2.5
|
github.com/ilyakaznacheev/cleanenv v1.2.5
|
||||||
github.com/json-iterator/go v1.1.10
|
github.com/json-iterator/go v1.1.10
|
||||||
github.com/klauspost/compress v1.11.2 // indirect
|
github.com/klauspost/compress v1.11.2 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -12,6 +12,8 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
|
||||||
github.com/gofiber/fiber/v2 v2.1.3 h1:d2fkRf6fkLa1uXgzXqN5iqAydjytoocS83hm1o00Ocg=
|
github.com/gofiber/fiber/v2 v2.1.3 h1:d2fkRf6fkLa1uXgzXqN5iqAydjytoocS83hm1o00Ocg=
|
||||||
github.com/gofiber/fiber/v2 v2.1.3/go.mod h1:MMiSv1HrDkN8Pv7NeVDYK+T/lwXOEKAvPBbLvJPCEfA=
|
github.com/gofiber/fiber/v2 v2.1.3/go.mod h1:MMiSv1HrDkN8Pv7NeVDYK+T/lwXOEKAvPBbLvJPCEfA=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/grafov/bcast v0.0.0-20190217190352-1447f067e08d h1:Q2+KsA/1GLC9xyLsDun3/EOJ+83rY/IHRsO1DToPrdo=
|
||||||
|
github.com/grafov/bcast v0.0.0-20190217190352-1447f067e08d/go.mod h1:RInr+B3/Tx70hYm0rpNPMTD7vH0pBG5ny/JsHAs2KcQ=
|
||||||
github.com/ilyakaznacheev/cleanenv v1.2.5 h1:/SlcF9GaIvefWqFJzsccGG/NJdoaAwb7Mm7ImzhO3DM=
|
github.com/ilyakaznacheev/cleanenv v1.2.5 h1:/SlcF9GaIvefWqFJzsccGG/NJdoaAwb7Mm7ImzhO3DM=
|
||||||
github.com/ilyakaznacheev/cleanenv v1.2.5/go.mod h1:/i3yhzwZ3s7hacNERGFwvlhwXMDcaqwIzmayEhbRplk=
|
github.com/ilyakaznacheev/cleanenv v1.2.5/go.mod h1:/i3yhzwZ3s7hacNERGFwvlhwXMDcaqwIzmayEhbRplk=
|
||||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||||
|
|
23
main.go
23
main.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/ilyakaznacheev/cleanenv"
|
"github.com/ilyakaznacheev/cleanenv"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,6 +23,7 @@ type Config struct {
|
||||||
PlayoutScript string `yaml:"playoutscript"`
|
PlayoutScript string `yaml:"playoutscript"`
|
||||||
PlayoutScriptPath string `yaml:"playoutscriptpath" env:""`
|
PlayoutScriptPath string `yaml:"playoutscriptpath" env:""`
|
||||||
ProgressDir string `yaml:"tmpdir"`
|
ProgressDir string `yaml:"tmpdir"`
|
||||||
|
OutputFormat string `yaml:"outputFormat"`
|
||||||
PrometheusPushGateway string `yaml:"prometheusPushGateway"`
|
PrometheusPushGateway string `yaml:"prometheusPushGateway"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,10 +93,23 @@ func schedulePlayout(s *store.Store) fiber.Handler {
|
||||||
Port: output,
|
Port: output,
|
||||||
Version: p.Version,
|
Version: p.Version,
|
||||||
})
|
})
|
||||||
|
if !playoutExists {
|
||||||
go func() {
|
go func() {
|
||||||
|
log.Printf("Start Scheduling %v", p.ID)
|
||||||
|
Waiting:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case ctrlMsg := <-p.ControlChannel:
|
||||||
|
fmt.Println(ctrlMsg)
|
||||||
|
continue
|
||||||
|
case <-time.After(time.Until(p.StartAt)):
|
||||||
|
break Waiting
|
||||||
|
}
|
||||||
|
}
|
||||||
p.Playout(s.Config)
|
p.Playout(s.Config)
|
||||||
s.DeletePlayout(p.ID)
|
s.DeletePlayout(p.ID)
|
||||||
}()
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// staus 409 when no schedule possible
|
// staus 409 when no schedule possible
|
||||||
|
|
||||||
|
@ -110,7 +125,7 @@ func main() {
|
||||||
log.Fatal("No configfile: ", err)
|
log.Fatal("No configfile: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err := store.NewStore(cfg.Outputs, cfg.DefaultDuration, cfg.PlayoutScriptPath, cfg.PlayoutScript, cfg.ProgressDir, cfg.PrometheusPushGateway)
|
s, err := store.NewStore(cfg.Outputs, cfg.DefaultDuration, cfg.PlayoutScriptPath, cfg.PlayoutScript, cfg.ProgressDir, cfg.PrometheusPushGateway, cfg.OutputFormat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to init Store: ", err.Error())
|
log.Fatal("Failed to init Store: ", err.Error())
|
||||||
}
|
}
|
||||||
|
@ -123,5 +138,9 @@ func main() {
|
||||||
})
|
})
|
||||||
app.Post("/schedulePlayout", schedulePlayout(s))
|
app.Post("/schedulePlayout", schedulePlayout(s))
|
||||||
|
|
||||||
log.Fatal(app.Listen(cfg.Address))
|
ln, err := net.Listen("tcp", cfg.Address)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Fatal(app.Listener(ln))
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"ffmpeg-playout/status"
|
"ffmpeg-playout/status"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/grafov/bcast"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -11,6 +12,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,42 +31,17 @@ type Config struct {
|
||||||
PlayoutScript string
|
PlayoutScript string
|
||||||
ProgressDir string
|
ProgressDir string
|
||||||
PrometheusPushGateway string
|
PrometheusPushGateway string
|
||||||
|
OutputFormat string
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:funlen
|
func monitorFFmpeg(cfg *Config, progressPath string, f *bcast.Member, output string) {
|
||||||
func (p *Job) Playout(cfg *Config) {
|
|
||||||
// TODO delete playout Job from store after finishing/aborting playout
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case ctrlMsg := <-p.ControlChannel:
|
|
||||||
fmt.Println(ctrlMsg)
|
|
||||||
continue
|
|
||||||
case <-time.After(time.Until(p.StartAt)):
|
|
||||||
log.Printf("Start Playout for %v", p.ID)
|
|
||||||
progressPath := path.Join(cfg.ProgressDir, strconv.Itoa(p.ID))
|
|
||||||
playoutScript := path.Join(cfg.PlayoutScriptPath, cfg.PlayoutScript)
|
|
||||||
cmd := exec.Command(playoutScript, //nolint:gosec
|
|
||||||
fmt.Sprintf("-i %v", p.Source),
|
|
||||||
fmt.Sprintf("-o %v", p.Output),
|
|
||||||
fmt.Sprintf("-p %v", progressPath),
|
|
||||||
)
|
|
||||||
cmd.Dir = cfg.PlayoutScriptPath
|
|
||||||
|
|
||||||
err := cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to start Playout %v: %v", p.ID, err)
|
|
||||||
}
|
|
||||||
pid := cmd.Process.Pid
|
|
||||||
log.Printf("PID for %v: %v", p.ID, pid)
|
|
||||||
log.Println(cmd.Args)
|
|
||||||
go func() {
|
|
||||||
stats := make(chan status.Status)
|
stats := make(chan status.Status)
|
||||||
c := 0
|
c := 0
|
||||||
for {
|
for {
|
||||||
if c > 15 {
|
if c > 15 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) {
|
if _, err := os.Stat(progressPath); os.IsNotExist(err) {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
c++
|
c++
|
||||||
continue
|
continue
|
||||||
|
@ -78,47 +55,94 @@ func (p *Job) Playout(cfg *Config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
u, _ := url.Parse(cfg.PrometheusPushGateway)
|
u, _ := url.Parse(cfg.PrometheusPushGateway)
|
||||||
u.Path = path.Join(u.Path, "metrics", "job", "Decklink_Outputs", p.Output)
|
u.Path = path.Join(u.Path, "metrics", "job", "Decklink_Outputs", output)
|
||||||
c = 0
|
c = 0
|
||||||
|
|
||||||
for c < 4 {
|
for c < 4 {
|
||||||
select {
|
select {
|
||||||
|
case <-f.Read:
|
||||||
|
return
|
||||||
case stat := <-stats:
|
case stat := <-stats:
|
||||||
metric := fmt.Sprintf("frame %v\n dup_frames %v\n bitrate %v\n fps %v\n speed %v\n", stat.Frame, stat.Dupframes, stat.Bitrate, stat.FPS, stat.Speed)
|
metric := fmt.Sprintf("frame %v\n dup_frames %v\n bitrate %v\n fps %v\n speed %v\n", stat.Frame, stat.Dupframes, stat.Bitrate, stat.FPS, stat.Speed)
|
||||||
resp, err := http.Post(u.Path, "text/plain", bytes.NewBuffer([]byte(metric)))
|
resp, err := http.Post(u.String(), "text/plain", bytes.NewBuffer([]byte(metric)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
// log.Println(err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
log.Println(metric)
|
resp.Body.Close()
|
||||||
defer resp.Body.Close()
|
|
||||||
case <-time.After(4 * time.Second):
|
case <-time.After(4 * time.Second):
|
||||||
c++
|
c++
|
||||||
continue
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
f.Send("finished")
|
||||||
ProcessManagement:
|
f.Close()
|
||||||
for {
|
}
|
||||||
select {
|
|
||||||
case ctrlMsg := <-p.ControlChannel:
|
func killProcess(cmd *exec.Cmd, id int) {
|
||||||
log.Println(ctrlMsg)
|
|
||||||
continue
|
|
||||||
case <-time.After(time.Until(p.StopAt)):
|
|
||||||
log.Printf("Kill %v", cmd.Process.Pid)
|
log.Printf("Kill %v", cmd.Process.Pid)
|
||||||
err := cmd.Process.Kill()
|
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to kill %v: %v", cmd.Process.Pid, err)
|
log.Printf("Failed to kill %v: %v", cmd.Process.Pid, err)
|
||||||
}
|
}
|
||||||
break ProcessManagement
|
log.Println(cmd.ProcessState.ExitCode())
|
||||||
|
log.Printf("Finish %d", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:funlen
|
||||||
|
func (p *Job) Playout(cfg *Config) {
|
||||||
|
progressPath := path.Join(cfg.ProgressDir, strconv.Itoa(p.ID))
|
||||||
|
playoutScript := path.Join(cfg.PlayoutScriptPath, cfg.PlayoutScript)
|
||||||
|
for {
|
||||||
|
log.Printf("Start Playout for %v", p.ID)
|
||||||
|
cmd := exec.Command(playoutScript, //nolint:gosec
|
||||||
|
"-i", p.Source,
|
||||||
|
"-o", p.Output,
|
||||||
|
"-f", cfg.OutputFormat,
|
||||||
|
"-p", progressPath,
|
||||||
|
)
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||||
|
cmd.Dir = cfg.PlayoutScriptPath
|
||||||
|
var outbuf, errbuf bytes.Buffer
|
||||||
|
cmd.Stdout = &outbuf
|
||||||
|
cmd.Stderr = &errbuf
|
||||||
|
|
||||||
|
err := cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to start Playout %v: %v", p.ID, err)
|
||||||
}
|
}
|
||||||
}
|
defer killProcess(cmd, p.ID)
|
||||||
err = cmd.Wait()
|
pid := cmd.Process.Pid
|
||||||
|
log.Printf("PID for %v: %v", p.ID, pid)
|
||||||
|
log.Println(cmd.Args)
|
||||||
|
|
||||||
|
finishChannel := bcast.NewGroup()
|
||||||
|
go finishChannel.Broadcast(0)
|
||||||
|
go func(cmd *exec.Cmd, f *bcast.Member) {
|
||||||
|
err := cmd.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
out, _ := cmd.CombinedOutput()
|
f.Send("finished")
|
||||||
log.Println(string(out))
|
f.Close()
|
||||||
log.Println(cmd.ProcessState.ExitCode())
|
}(cmd, finishChannel.Join())
|
||||||
log.Printf("Finish %v", p.ID)
|
|
||||||
|
go monitorFFmpeg(cfg, progressPath, finishChannel.Join(), p.Output)
|
||||||
|
fChannelMember := finishChannel.Join()
|
||||||
|
ProcessManagement:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-fChannelMember.Read:
|
||||||
|
break ProcessManagement
|
||||||
|
case ctrlMsg := <-p.ControlChannel:
|
||||||
|
log.Println(ctrlMsg)
|
||||||
|
break
|
||||||
|
case <-time.After(time.Until(p.StopAt)):
|
||||||
|
break ProcessManagement
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if time.Now().After(p.StopAt) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,13 @@ package status
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Status struct {
|
type Status struct {
|
||||||
|
@ -61,21 +61,24 @@ func FFmpegStatusWatcher(c chan<- Status, path string) error {
|
||||||
case "fps":
|
case "fps":
|
||||||
ffmpegStatus.FPS, err = strconv.ParseFloat(value, 64)
|
ffmpegStatus.FPS, err = strconv.ParseFloat(value, 64)
|
||||||
case "bitrate":
|
case "bitrate":
|
||||||
ffmpegStatus.Bitrate, err = strconv.ParseFloat(strings.TrimRight(value, "kbits/s"), 64)
|
ffmpegStatus.Bitrate, err = strconv.ParseFloat(strings.TrimSuffix(value, "kbits/s"), 64)
|
||||||
case "out_time_ms":
|
case "out_time_ms":
|
||||||
// test["Progress.Out_time_ms"], err = strconv.Atoi(value)
|
// test["Progress.Out_time_ms"], err = strconv.Atoi(value)
|
||||||
continue
|
continue
|
||||||
case "dup_frames":
|
case "dup_frames":
|
||||||
ffmpegStatus.Dupframes, err = strconv.Atoi(value)
|
ffmpegStatus.Dupframes, err = strconv.Atoi(value)
|
||||||
case "speed":
|
case "speed":
|
||||||
ffmpegStatus.Speed, err = strconv.ParseFloat(strings.TrimRight(value, "x"), 64)
|
ffmpegStatus.Speed, err = strconv.ParseFloat(strings.TrimSuffix(value, "x"), 64)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c <- ffmpegStatus
|
c <- ffmpegStatus
|
||||||
}
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
log.Println("Timeout of Status Reading")
|
||||||
|
return
|
||||||
case err, ok := <-watcher.Errors:
|
case err, ok := <-watcher.Errors:
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
|
|
|
@ -16,7 +16,7 @@ type Store struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStore(o []string, defaultDuration string, playoutScriptPath string, playoutScript string, tmpDir string, prometheus string) (*Store, error) {
|
func NewStore(o []string, defaultDuration string, playoutScriptPath string, playoutScript string, tmpDir string, prometheus string, format string) (*Store, error) {
|
||||||
playouts := make(map[int]*playout.Job)
|
playouts := make(map[int]*playout.Job)
|
||||||
|
|
||||||
var d time.Duration
|
var d time.Duration
|
||||||
|
@ -28,6 +28,7 @@ func NewStore(o []string, defaultDuration string, playoutScriptPath string, play
|
||||||
PlayoutScriptPath: playoutScriptPath,
|
PlayoutScriptPath: playoutScriptPath,
|
||||||
PlayoutScript: playoutScript,
|
PlayoutScript: playoutScript,
|
||||||
ProgressDir: tmpDir,
|
ProgressDir: tmpDir,
|
||||||
|
OutputFormat: format,
|
||||||
PrometheusPushGateway: prometheus,
|
PrometheusPushGateway: prometheus,
|
||||||
}
|
}
|
||||||
store := &Store{Playouts: playouts, DefaultDuration: d, Outputs: o, Config: &pcfg}
|
store := &Store{Playouts: playouts, DefaultDuration: d, Outputs: o, Config: &pcfg}
|
||||||
|
|
Loading…
Reference in a new issue