some changes

This commit is contained in:
2025-12-14 16:34:42 +02:00
parent 6aae5f9fb0
commit 18a31be0b1
11 changed files with 207 additions and 34 deletions

View File

@@ -1,25 +1,49 @@
package worker
import (
"bufio"
"bytes"
"fmt"
"os/exec"
)
func RunFunction(path string, fc *FuncConfig, input []byte) ([]byte, error) {
if fc.Runtime != "exec" {
return nil, fmt.Errorf("unsupported runtime: %s", fc.Runtime)
type RunOps struct {
Log Logger
Path string
FuncConfig *FuncConfig
Env []string
}
func RunFunction(opt *RunOps, input []byte) ([]byte, error) {
if opt.FuncConfig.Runtime != "exec" {
return nil, fmt.Errorf("unsupported runtime: %s", opt.FuncConfig.Runtime)
}
cmd := exec.Command(path)
cmd := exec.Command(opt.Path)
cmd.Env = opt.Env
cmd.Stdin = bytes.NewReader(input)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
stderrPipe, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
go func() {
scanner := bufio.NewScanner(stderrPipe)
for scanner.Scan() {
line := scanner.Text()
opt.Log.Write(line)
}
}()
if err := cmd.Run(); err != nil {
if err := cmd.Start(); err != nil {
return nil, err
}
if err := cmd.Wait(); err != nil {
return nil, fmt.Errorf("failed: %w\noutput: %s", err, out.String())
}
return out.Bytes(), nil
}