From 9979a1eba8489eb20f9398f8f7461f8ff48a15eb Mon Sep 17 00:00:00 2001 From: speatzle Date: Thu, 9 May 2024 19:06:51 +0200 Subject: [PATCH] fix logs --- task/healthcheck.go | 48 +++++++++++++++++++++++++++++++++++++-------- task/log/log.go | 9 +-------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/task/healthcheck.go b/task/healthcheck.go index 0cb2431..824d609 100644 --- a/task/healthcheck.go +++ b/task/healthcheck.go @@ -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 } diff --git a/task/log/log.go b/task/log/log.go index 5372f60..6ef30dc 100644 --- a/task/log/log.go +++ b/task/log/log.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log/slog" - "runtime" "sync" "time" @@ -62,12 +61,6 @@ func (h *TaskHandler) Handle(ctx context.Context, r slog.Record) error { if !r.Time.IsZero() { 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)) // Handle state from WithGroup and WithAttrs. 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()) case slog.KindTime: // 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: attrs := a.Value.Group() // Ignore empty groups.