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 ( import (
"bufio" "bufio"
"bytes"
"context" "context"
"log/slog"
"os/exec" "os/exec"
"sync" "sync"
@ -13,15 +13,43 @@ import (
"git.lastassault.de/speatzle/morffix/types" "git.lastassault.de/speatzle/morffix/types"
) )
type HealthCheckData struct { // dropCR drops a terminal \r from the data.
Command types.FFmpegCommand `json:"command"` 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() ctx := context.TODO()
l := log.GetTaskLogger(t) l := log.GetTaskLogger(t)
l.InfoContext(ctx, "Running ffmpeg", "command", data.Command) l.InfoContext(ctx, "Running ffmpeg", "args", data.Command.GetArgs())
cmd := exec.CommandContext(ctx, conf.Worker.FFmpegPath, data.Command.GetArgs()...) path := conf.Worker.FFmpegPath
if path == "" {
path = "ffmpeg"
}
cmd := exec.CommandContext(ctx, path, data.Command.GetArgs()...)
var wg sync.WaitGroup var wg sync.WaitGroup
@ -33,9 +61,10 @@ func RunHealthCheck(conf config.Config, t *types.Task, data HealthCheckData) {
wg.Add(1) wg.Add(1)
outScanner := bufio.NewScanner(stdout) outScanner := bufio.NewScanner(stdout)
outScanner.Split(scanLines)
go func() { go func() {
for outScanner.Scan() { for outScanner.Scan() {
slog.InfoContext(ctx, outScanner.Text(), "pipe", "out") l.InfoContext(ctx, outScanner.Text())
} }
wg.Done() wg.Done()
}() }()
@ -47,9 +76,10 @@ func RunHealthCheck(conf config.Config, t *types.Task, data HealthCheckData) {
} }
wg.Add(1) wg.Add(1)
errScanner := bufio.NewScanner(stderr) errScanner := bufio.NewScanner(stderr)
errScanner.Split(scanLines)
go func() { go func() {
for errScanner.Scan() { for errScanner.Scan() {
slog.InfoContext(ctx, errScanner.Text(), "pipe", "err") l.InfoContext(ctx, errScanner.Text())
} }
wg.Done() wg.Done()
}() }()
@ -67,5 +97,7 @@ func RunHealthCheck(conf config.Config, t *types.Task, data HealthCheckData) {
l.ErrorContext(ctx, "Error Running ffmpeg", "err", err) l.ErrorContext(ctx, "Error Running ffmpeg", "err", err)
return return
} }
l.InfoContext(ctx, "Task Success")
t.Status = constants.TASK_STATUS_SUCCESS t.Status = constants.TASK_STATUS_SUCCESS
} }

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"runtime"
"sync" "sync"
"time" "time"
@ -62,12 +61,6 @@ func (h *TaskHandler) Handle(ctx context.Context, r slog.Record) error {
if !r.Time.IsZero() { if !r.Time.IsZero() {
buf = h.appendAttr(buf, slog.Time(slog.TimeKey, r.Time)) buf = h.appendAttr(buf, slog.Time(slog.TimeKey, r.Time))
} }
buf = h.appendAttr(buf, slog.Any(slog.LevelKey, r.Level))
if r.PC != 0 {
fs := runtime.CallersFrames([]uintptr{r.PC})
f, _ := fs.Next()
buf = h.appendAttr(buf, slog.String(slog.SourceKey, fmt.Sprintf("%s:%d", f.File, f.Line)))
}
buf = h.appendAttr(buf, slog.String(slog.MessageKey, r.Message)) buf = h.appendAttr(buf, slog.String(slog.MessageKey, r.Message))
// Handle state from WithGroup and WithAttrs. // Handle state from WithGroup and WithAttrs.
goas := h.goas goas := h.goas
@ -108,7 +101,7 @@ func (h *TaskHandler) appendAttr(buf []byte, a slog.Attr) []byte {
buf = fmt.Appendf(buf, "%s: %q\n", a.Key, a.Value.String()) buf = fmt.Appendf(buf, "%s: %q\n", a.Key, a.Value.String())
case slog.KindTime: case slog.KindTime:
// Write times in a standard way, without the monotonic time. // Write times in a standard way, without the monotonic time.
buf = fmt.Appendf(buf, "%s: %s\n", a.Key, a.Value.Time().Format(time.RFC3339Nano)) buf = fmt.Appendf(buf, "%s: %s\n", a.Key, a.Value.Time().Format(time.DateTime))
case slog.KindGroup: case slog.KindGroup:
attrs := a.Value.Group() attrs := a.Value.Group()
// Ignore empty groups. // Ignore empty groups.