Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
fix(luavm): config
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Jan 3, 2023
1 parent 56efb92 commit 1f3209e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 41 deletions.
5 changes: 4 additions & 1 deletion app/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func NewHandler(ctx context.Context, cfg *config.Config) http.Handler {
if err == nil {
log.Warn("Use Lua File Path:", cfg.LuaFilePath)
}
worker := luavm.NewWorker(luavm.DefaultConfig(), s, rpcServer, luaFile)
worker := luavm.NewWorker(luavm.Config{
Instance: cfg.Instance,
BaseURL: cfg.BaseURL,
}, s, rpcServer, luaFile)
go worker.Run(ctx)

srv := service.NewService(orm, rdb, worker)
Expand Down
29 changes: 15 additions & 14 deletions app/luavm/wconfig.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package luavm

import "os"
const (
defaultFileLua = "default.lua"
defaultFileKey = "lua"
)

// Order is important
var (
libs = []string{
"lib_task.lua",
"lib_node.lua",
"lib_geo.lua",
"lib_log.lua",
"lib_main.lua",
}
)

type Config struct {
Instance string
LuaFile string
LuaTask string
Timeout string
BaseURL string
}

func DefaultConfig() Config {
return Config{
Instance: "gosd.0",
LuaFile: "test_min.lua",
LuaTask: "lua",
Timeout: "2h",
BaseURL: os.Getenv("BASE_URL"),
}
}
26 changes: 2 additions & 24 deletions app/luavm/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"strconv"
"sync"
"time"

"sb.im/gosd/rpc2mqtt"

Expand All @@ -24,16 +23,6 @@ import (
luar "layeh.com/gopher-luar"
)

var (
libs = []string{
"lib_task.lua",
"lib_node.lua",
"lib_geo.lua",
"lib_log.lua",
"lib_main.lua",
}
)

type Worker struct {
cfg Config
ctx context.Context
Expand All @@ -44,8 +33,6 @@ type Worker struct {
script []byte
mutex *sync.Mutex

timeout time.Duration

rpc *rpc2mqtt.Rpc2mqtt
Running map[string]*Service
}
Expand All @@ -54,19 +41,12 @@ func NewWorker(cfg Config, s *store.Store, rpc *rpc2mqtt.Rpc2mqtt, script []byte
ctx := context.TODO()
// default LuaFile: input > default
if len(script) == 0 {
if data, err := lualib.LuaFile.ReadFile(cfg.LuaFile); err != nil {
if data, err := lualib.LuaFile.ReadFile(defaultFileLua); err != nil {
logger.WithContext(ctx).Error(err)
} else {
script = data
}
}

timeout, err := time.ParseDuration(cfg.Timeout)
if err != nil {
logger.WithContext(ctx).Error(err)
timeout = 2 * time.Hour
}

return &Worker{
cfg: cfg,
ctx: ctx,
Expand All @@ -77,8 +57,6 @@ func NewWorker(cfg Config, s *store.Store, rpc *rpc2mqtt.Rpc2mqtt, script []byte
script: script,
mutex: &sync.Mutex{},

timeout: timeout,

rpc: rpc,
Running: make(map[string]*Service),
}
Expand Down Expand Up @@ -112,7 +90,7 @@ func (w *Worker) AddTask(ctx context.Context, task *model.Task) error {
func (w *Worker) getScript(task *model.Task) (script []byte) {
files := make(map[string]string)
if err := json.Unmarshal(task.Files, &files); err == nil {
if key, ok := files[w.cfg.LuaTask]; ok {
if key, ok := files[defaultFileKey]; ok {
if data, err := w.ofs.Get(key); err == nil {
script = data
}
Expand Down
5 changes: 4 additions & 1 deletion app/luavm/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func helpTestNewWorker(t *testing.T, script []byte) *Worker {
}

// use tempDir
return NewWorker(DefaultConfig(), store.NewStore(cfg, orm, redis.NewClient(redisOpt), storage.NewStorage(t.TempDir())), nil, script)
return NewWorker(Config{
Instance: cfg.Instance,
BaseURL: cfg.BaseURL,
}, store.NewStore(cfg, orm, redis.NewClient(redisOpt), storage.NewStorage(t.TempDir())), nil, script)
}

func newTestTask(t *testing.T) *model.Task {
Expand Down
5 changes: 4 additions & 1 deletion tests/e2e/luavm/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ var _ = Describe("LuaVM Rpc", func() {
if err == nil {
log.Warn("Use Lua File Path:", cfg.LuaFilePath)
}
worker := luavm.NewWorker(luavm.DefaultConfig(), store.NewStore(nil, orm, rdb, ofs), rpcServer, luaFile)
worker := luavm.NewWorker(luavm.Config{
Instance: cfg.Instance,
BaseURL: cfg.BaseURL,
}, store.NewStore(nil, orm, rdb, ofs), rpcServer, luaFile)
go worker.Run(ctx)

go func() {
Expand Down

0 comments on commit 1f3209e

Please sign in to comment.