This commit is contained in:
speatzle 2024-05-09 19:06:51 +02:00
parent 7a0564a210
commit 9979a1eba8
2 changed files with 41 additions and 16 deletions

View file

@ -2,8 +2,8 @@ package task
import (
"bufio"
"bytes"
"context"
"log/slog"
"os/exec"
"sync"
@ -13,15 +13,43 @@ import (
"git.lastassault.de/speatzle/morffix/types"
)
type HealthCheckData struct {
Command types.FFmpegCommand `json:"command"`
// dropCR drops a terminal \r from the data.
func dropCR(data []byte) []byte {
if len(data) > 0 && data[len(data)-1] == '\r' {
return data[0 : len(data)-1]
}
return data
}
func RunHealthCheck(conf config.Config, t *types.Task, data HealthCheckData) {
func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, '\n'); i >= 0 {
// We have a full newline-terminated line.
return i + 1, dropCR(data[0:i]), nil
}
if i := bytes.IndexByte(data, '\r'); i >= 0 {
// We have a return line.
return i + 1, dropCR(data[0:i]), nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), dropCR(data), nil
}
// Request more data.
return 0, nil, nil
}
func RunHealthCheck(conf config.Config, t *types.Task, data types.HealthCheckData) {
ctx := context.TODO()
l := log.GetTaskLogger(t)
l.InfoContext(ctx, "Running ffmpeg", "command", data.Command)
cmd := exec.CommandContext(ctx, conf.Worker.FFmpegPath, data.Command.GetArgs()...)
l.InfoContext(ctx, "Running ffmpeg", "args", data.Command.GetArgs())
path := conf.Worker.FFmpegPath
if path == "" {
path = "ffmpeg"
}
cmd := exec.CommandContext(ctx, path, data.Command.GetArgs()...)
var wg sync.WaitGroup
@ -33,9 +61,10 @@ func RunHealthCheck(conf config.Config, t *types.Task, data HealthCheckData) {
wg.Add(1)
outScanner := bufio.NewScanner(stdout)
outScanner.Split(scanLines)
go func() {
for outScanner.Scan() {
slog.InfoContext(ctx, outScanner.Text(), "pipe", "out")
l.InfoContext(ctx, outScanner.Text())
}
wg.Done()
}()
@ -47,9 +76,10 @@ func RunHealthCheck(conf config.Config, t *types.Task, data HealthCheckData) {
}
wg.Add(1)
errScanner := bufio.NewScanner(stderr)
errScanner.Split(scanLines)
go func() {
for errScanner.Scan() {
slog.InfoContext(ctx, errScanner.Text(), "pipe", "err")
l.InfoContext(ctx, errScanner.Text())
}
wg.Done()
}()
@ -67,5 +97,7 @@ func RunHealthCheck(conf config.Config, t *types.Task, data HealthCheckData) {
l.ErrorContext(ctx, "Error Running ffmpeg", "err", err)
return
}
l.InfoContext(ctx, "Task Success")
t.Status = constants.TASK_STATUS_SUCCESS
}