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

@@ -10,11 +10,18 @@ import (
"gorm.io/gorm"
)
type Logger interface {
Write(line string)
}
type RootConfig struct {
Data struct {
Driver string `json:"driver"`
Path string `json:"path"`
} `json:"data"`
Log struct {
Path string `json:"log_root_path"`
} `json:"log"`
}
type Function struct {
@@ -30,6 +37,9 @@ type FuncConfig struct {
Version string `json:"version"`
Entry string `json:"entry"`
Runtime string `json:"runtime"`
Log struct {
Output string `json:"output"`
} `json:"log"`
}
func LoadTreeConfig(root string) (*RootConfig, error) {
@@ -61,9 +71,18 @@ func OpenDB(cfg *RootConfig, root string) (*gorm.DB, error) {
func FindFunction(db *gorm.DB, name, version string) (*Function, error) {
var f Function
if err := db.Where("function_name = ? AND version = ? AND deleted_at IS NULL", name, version).
First(&f).Error; err != nil {
return nil, err
if version == "latest" {
err := db.Where("function_name = ? AND deleted_at IS NULL", name).
Order("created_at DESC").
First(&f).Error
if err != nil {
return nil, err
}
} else {
if err := db.Where("function_name = ? AND version = ? AND deleted_at IS NULL", name, version).
First(&f).Error; err != nil {
return nil, err
}
}
return &f, nil
}

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
}