forked from hootrhino/rulex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
244 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package appstack | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/i4de/rulex/glogger" | ||
"github.com/i4de/rulex/typex" | ||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
// lua 虚拟机的参数 | ||
const _VM_Registry_Size int = 1024 * 1024 // 默认堆栈大小 | ||
const _VM_Registry_MaxSize int = 1024 * 1024 // 默认最大堆栈 | ||
const _VM_Registry_GrowStep int = 32 // 默认CPU消耗 | ||
|
||
/* | ||
* | ||
* 轻量级应用 | ||
* | ||
*/ | ||
type Application struct { | ||
UUID string `json:"uuid"` // 名称 | ||
Name string `json:"name"` // 名称 | ||
Version string `json:"version"` // 版本号 | ||
Filepath string `json:"filepath"` // 文件路径, 是相对于main的apps目录 | ||
luaMainFunc *lua.LFunction `json:"-"` | ||
vm *lua.LState `json:"-"` // lua 环境 | ||
ctx context.Context `json:"-"` | ||
cancel context.CancelFunc `json:"-"` | ||
} | ||
|
||
func NewApplication(uuid, Name, Version, Filepath string) *Application { | ||
app := new(Application) | ||
app.Name = Name | ||
app.Version = Version | ||
app.Filepath = Filepath | ||
app.vm = lua.NewState(lua.Options{ | ||
RegistrySize: _VM_Registry_Size, | ||
RegistryMaxSize: _VM_Registry_MaxSize, | ||
RegistryGrowStep: _VM_Registry_GrowStep, | ||
}) | ||
return app | ||
} | ||
|
||
/* | ||
* | ||
* 管理器 | ||
* | ||
*/ | ||
type AppStack struct { | ||
Applications map[string]*Application | ||
} | ||
|
||
func NewAppStack() *AppStack { | ||
as := new(AppStack) | ||
as.Applications = map[string]*Application{} | ||
return as | ||
} | ||
|
||
/* | ||
* | ||
* 加载本地文件到lua虚拟机 | ||
* | ||
*/ | ||
func (as *AppStack) LoadApp(app *Application) error { | ||
|
||
// 临时校验语法 | ||
tempVm := lua.NewState() | ||
bytes, err := os.ReadFile("./apps/" + app.Filepath) | ||
if err != nil { | ||
return err | ||
} | ||
if err := tempVm.DoString(string(bytes)); err != nil { | ||
return err | ||
} | ||
// 检查名称 | ||
AppNAME := tempVm.GetGlobal("AppNAME") | ||
if AppNAME == nil { | ||
return fmt.Errorf("'AppNAME' field not exists") | ||
} | ||
if AppNAME.Type() != lua.LTString { | ||
return fmt.Errorf("'AppNAME' must be string") | ||
} | ||
// 检查类型 | ||
AppVERSION := tempVm.GetGlobal("AppVERSION") | ||
if AppVERSION == nil { | ||
return fmt.Errorf("'AppVERSION' field not exists") | ||
} | ||
if AppVERSION.Type() != lua.LTString { | ||
return fmt.Errorf("'AppVERSION' must be string") | ||
} | ||
// 检查描述信息 | ||
AppDESCRIPTION := tempVm.GetGlobal("AppDESCRIPTION") | ||
if AppDESCRIPTION == nil { | ||
if AppDESCRIPTION.Type() != lua.LTString { | ||
return fmt.Errorf("'AppDESCRIPTION' must be string") | ||
} | ||
} | ||
|
||
// 检查函数入口 | ||
AppMain := tempVm.GetGlobal("Main") | ||
if AppMain == nil { | ||
return fmt.Errorf("'Main' field not exists") | ||
} | ||
if AppMain.Type() != lua.LTFunction { | ||
return fmt.Errorf("'Main' must be function(arg)") | ||
} | ||
|
||
// 加载到内存里 | ||
fMain := *AppMain.(*lua.LFunction) | ||
app.luaMainFunc = &fMain | ||
if err := as.startApp(app); err != nil { | ||
return err | ||
} | ||
as.Applications[app.UUID] = app | ||
// | ||
tempVm.Close() | ||
tempVm = nil | ||
return nil | ||
} | ||
|
||
/* | ||
* | ||
* 启动 function Main(args) --do-some-thing-- return 0 end | ||
* | ||
*/ | ||
func (as *AppStack) startApp(app *Application) error { | ||
// args := lua.LBool(false) // Main的参数,未来准备扩展 | ||
ctx, cancel := context.WithCancel(typex.GCTX) | ||
app.ctx = ctx | ||
app.cancel = cancel | ||
go func(ctx context.Context) { | ||
app.vm.SetContext(ctx) | ||
err := app.vm.CallByParam(lua.P{ | ||
Fn: app.luaMainFunc, // 回调函数 | ||
NRet: 1, // 一个返回值 | ||
Protect: true, // 受保护 | ||
}) | ||
if err != nil { | ||
glogger.GLogger.Error("startApp error:", err) | ||
return | ||
} | ||
}(app.ctx) | ||
|
||
return nil | ||
} | ||
|
||
/* | ||
* | ||
* 删除APP | ||
* | ||
*/ | ||
func (as *AppStack) RemoveApp(uuid string) error { | ||
if app, ok := as.Applications[uuid]; ok { | ||
app.cancel() | ||
app.vm.Close() | ||
delete(as.Applications, uuid) | ||
app.vm = nil | ||
} | ||
return nil | ||
} | ||
|
||
/* | ||
* | ||
* 更新应用信息 | ||
* | ||
*/ | ||
func (as *AppStack) UpdateApp(app *Application) error { | ||
if _, ok := as.Applications[app.UUID]; ok { | ||
as.Applications[app.UUID] = app | ||
} | ||
return fmt.Errorf("update failed, app not exists:%s", app.UUID) | ||
|
||
} | ||
|
||
/* | ||
* | ||
* 获取列表 | ||
* | ||
*/ | ||
func (as *AppStack) ListApp() []Application { | ||
apps := []Application{} | ||
for _, v := range as.Applications { | ||
apps = append(apps, *v) | ||
} | ||
return apps | ||
} | ||
|
||
func (as *AppStack) Stop() { | ||
for _, app := range as.Applications { | ||
app.cancel() | ||
app.vm.Close() | ||
app.vm = nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
AppNAME = "test_demo" | ||
AppVERSION = "1.0.0" | ||
AppDESCRIPTION = "A demo app" | ||
function Main(arg) | ||
while true do | ||
print("Message From Main:", AppNAME, AppVERSION, AppDESCRIPTION) | ||
end | ||
return 0 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/i4de/rulex/appstack" | ||
) | ||
|
||
// go test -timeout 30s -run ^Test_appStack github.com/i4de/rulex/test -v -count=1 | ||
func Test_appStack(t *testing.T) { | ||
as := appstack.NewAppStack() | ||
|
||
err := as.LoadApp(appstack.NewApplication("test-uuid-1", "test-name", | ||
"1.0.1", "helloworld_1.0.0.lua")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Log(as) | ||
go func() { | ||
for i := 0; i < 1000; i++ { | ||
|
||
t.Log("Test_appStack ======>") | ||
} | ||
}() | ||
time.Sleep(30 * time.Second) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters