From 86ba486f4789ffe5942ec1c97f4f13c15d83f10b Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Thu, 9 May 2024 04:47:39 +0200 Subject: [PATCH] Implement Healthcheck Command Execution --- task/healthcheck.go | 68 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 7 deletions(-) diff --git a/task/healthcheck.go b/task/healthcheck.go index 19e0266..0cb2431 100644 --- a/task/healthcheck.go +++ b/task/healthcheck.go @@ -1,17 +1,71 @@ package task -import "git.lastassault.de/speatzle/morffix/constants" +import ( + "bufio" + "context" + "log/slog" + "os/exec" + "sync" + + "git.lastassault.de/speatzle/morffix/config" + "git.lastassault.de/speatzle/morffix/constants" + "git.lastassault.de/speatzle/morffix/task/log" + "git.lastassault.de/speatzle/morffix/types" +) type HealthCheckData struct { - Command string `json:"command"` + Command types.FFmpegCommand `json:"command"` } -func (t *Task) RunHealthCheck(data HealthCheckData) { - defer func() { - if t.Status == constants.TASK_STATUS_RUNNING { - t.Status = constants.TASK_STATUS_FAILED - t.Log = append(t.Log, "Task Status Failed by Defer") +func RunHealthCheck(conf config.Config, t *types.Task, data 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()...) + + var wg sync.WaitGroup + + stdout, err := cmd.StdoutPipe() + if err != nil { + l.ErrorContext(ctx, "Error getting StdoutPipe", "err", err) + return + } + + wg.Add(1) + outScanner := bufio.NewScanner(stdout) + go func() { + for outScanner.Scan() { + slog.InfoContext(ctx, outScanner.Text(), "pipe", "out") } + wg.Done() }() + stderr, err := cmd.StderrPipe() + if err != nil { + l.ErrorContext(ctx, "Error getting StderrPipe", "err", err) + return + } + wg.Add(1) + errScanner := bufio.NewScanner(stderr) + go func() { + for errScanner.Scan() { + slog.InfoContext(ctx, errScanner.Text(), "pipe", "err") + } + wg.Done() + }() + + err = cmd.Start() + if err != nil { + l.ErrorContext(ctx, "Error Starting ffmpeg", "err", err) + return + } + + wg.Wait() + + err = cmd.Wait() + if err != nil { + l.ErrorContext(ctx, "Error Running ffmpeg", "err", err) + return + } + t.Status = constants.TASK_STATUS_SUCCESS }