diff --git a/.github/workflows/pr-test.yml b/.github/workflows/pr-test.yml index 5e7ce935..2846dea2 100644 --- a/.github/workflows/pr-test.yml +++ b/.github/workflows/pr-test.yml @@ -45,9 +45,20 @@ env: DEMO_APP: ${{ github.WORKSPACE }}/../app + # Application Setting + + ## Path YAO_TEST_APPLICATION: ${{ github.WORKSPACE }}/../app YAO_EXTENSION_ROOT: ${{ github.WORKSPACE }}/../extension + ## Runtime + YAO_RUNTIME_MIN: 3 + YAO_RUNTIME_MAX: 6 + YAO_RUNTIME_HEAP_LIMIT: 1500000000 + YAO_RUNTIME_HEAP_RELEASE: 10000000 + YAO_RUNTIME_HEAP_AVAILABLE: 550000000 + YAO_RUNTIME_PRECOMPILE: true + jobs: UnitTest: runs-on: ubuntu-20.04 @@ -62,10 +73,10 @@ jobs: github.event.workflow_run.conclusion == 'success' }} steps: - name: "Download artifact" - uses: actions/github-script@v3.1.0 + uses: actions/github-script@v6 with: script: | - var artifacts = await github.actions.listWorkflowRunArtifacts({ + var artifacts = await github.rest.actions.listWorkflowRunArtifacts({ owner: context.repo.owner, repo: context.repo.repo, run_id: ${{github.event.workflow_run.id }}, @@ -73,7 +84,7 @@ jobs: var matchArtifact = artifacts.data.artifacts.filter((artifact) => { return artifact.name == "pr" })[0]; - var download = await github.actions.downloadArtifact({ + var download = await github.rest.actions.downloadArtifact({ owner: context.repo.owner, repo: context.repo.repo, artifact_id: matchArtifact.id, @@ -91,14 +102,14 @@ jobs: echo NR=$(cat NR) >> $GITHUB_ENV - name: "Comment on PR" - uses: actions/github-script@v3 + uses: actions/github-script@v6 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | const { NR } = process.env var fs = require('fs'); var issue_number = NR; - await github.issues.createComment({ + await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: issue_number, @@ -106,7 +117,7 @@ jobs: }); - name: Setup Cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: | ~/.cache/go-build @@ -116,19 +127,19 @@ jobs: ${{ runner.os }}-go- - name: Checkout Kun - uses: actions/checkout@v2 + uses: actactions/checkout@v3 with: repository: yaoapp/kun path: kun - name: Checkout Xun - uses: actions/checkout@v2 + uses: actactions/checkout@v3 with: repository: yaoapp/xun path: xun - name: Checkout Gou - uses: actions/checkout@v2 + uses: actactions/checkout@v3 with: repository: yaoapp/gou path: gou @@ -141,7 +152,7 @@ jobs: path: v8go - name: Checkout Demo App - uses: actions/checkout@v2 + uses: actactions/checkout@v3 with: repository: yaoapp/yao-dev-app path: app @@ -164,7 +175,7 @@ jobs: ls -l ../ - name: Checkout pull request HEAD commit - uses: actions/checkout@v2 + uses: actactions/checkout@v3 with: ref: ${{ env.HEAD }} @@ -243,14 +254,14 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos - name: "Comment on PR" - uses: actions/github-script@v3 + uses: actions/github-script@v6 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | const { NR } = process.env var fs = require('fs'); var issue_number = NR; - await github.issues.createComment({ + await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: issue_number, diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 060501db..bc0db6c7 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -49,9 +49,20 @@ env: DEMO_APP: ${{ github.WORKSPACE }}/../app + # Application Setting + + ## Path YAO_TEST_APPLICATION: ${{ github.WORKSPACE }}/../app YAO_EXTENSION_ROOT: ${{ github.WORKSPACE }}/../extension + ## Runtime + YAO_RUNTIME_MIN: 3 + YAO_RUNTIME_MAX: 6 + YAO_RUNTIME_HEAP_LIMIT: 1500000000 + YAO_RUNTIME_HEAP_RELEASE: 10000000 + YAO_RUNTIME_HEAP_AVAILABLE: 550000000 + YAO_RUNTIME_PRECOMPILE: true + jobs: unit-test: runs-on: ubuntu-20.04 diff --git a/Makefile b/Makefile index dcec010b..539c346f 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ COMMIT := $(shell git log | head -n 1 | awk '{print substr($$2, 0, 12)}') NOW := $(shell date +"%FT%T%z") # ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) -TESTFOLDER := $(shell $(GO) list ./... | grep -E 'api|model|flow|script|fs|i18n|connector|query|plugin|cert|crypto|task|schedule' | grep -vE 'examples|tests*|config|widgets') +TESTFOLDER := $(shell $(GO) list ./... | grep -E 'api|model|flow|script|fs|i18n|connector|query|plugin|cert|crypto|task|schedule|runtime' | grep -vE 'examples|tests*|config|widgets') TESTTAGS ?= "" # TESTWIDGETS := $(shell $(GO) list ./widgets/...) diff --git a/config/types.go b/config/types.go index a981318c..430f4b3a 100644 --- a/config/types.go +++ b/config/types.go @@ -2,42 +2,43 @@ package config // Config 象传应用引擎配置 type Config struct { - Mode string `json:"mode,omitempty" env:"YAO_ENV" envDefault:"production"` // 象传引擎启动模式 production/development - Root string `json:"root,omitempty" env:"YAO_ROOT" envDefault:"."` // 应用根目录 - Lang string `json:"lang,omitempty" env:"YAO_LANG" envDefault:"en-us"` // Default language setting - TimeZone string `json:"timezone,omitempty" env:"YAO_TIMEZONE"` // Default TimeZone - DataRoot string `json:"data_root,omitempty" env:"YAO_DATA_ROOT" envDefault:""` // DATA PATH - ExtensionRoot string `json:"extension_root,omitempty" env:"YAO_EXTENSION_ROOT" envDefault:""` // Plugin, Wasm root PATH, Default is (/plugins /wasms) - Host string `json:"host,omitempty" env:"YAO_HOST" envDefault:"0.0.0.0"` // 服务监听地址 - Port int `json:"port,omitempty" env:"YAO_PORT" envDefault:"5099"` // 服务监听端口 - Cert string `json:"cert,omitempty" env:"YAO_CERT"` // HTTPS 证书文件地址 - Key string `json:"key,omitempty" env:"YAO_KEY"` // HTTPS 证书密钥地址 - Log string `json:"log,omitempty" env:"YAO_LOG"` // 服务日志地址 - LogMode string `json:"log_mode,omitempty" env:"YAO_LOG_MODE" envDefault:"TEXT"` // 服务日志模式 JSON|TEXT - JWTSecret string `json:"jwt_secret,omitempty" env:"YAO_JWT_SECRET"` // JWT 密钥 - DB DBConfig `json:"db,omitempty"` // 数据库配置 - AllowFrom []string `json:"allowfrom,omitempty" envSeparator:"|" env:"YAO_ALLOW_FROM"` // Domain list the separator is | - Session SessionConfig `json:"session,omitempty"` // Session Config - Studio StudioConfig `json:"studio,omitempty"` // Studio config + Mode string `json:"mode,omitempty" env:"YAO_ENV" envDefault:"production"` // 象传引擎启动模式 production/development + Root string `json:"root,omitempty" env:"YAO_ROOT" envDefault:"."` // 应用根目录 + Lang string `json:"lang,omitempty" env:"YAO_LANG" envDefault:"en-us"` // Default language setting + TimeZone string `json:"timezone,omitempty" env:"YAO_TIMEZONE"` // Default TimeZone + DataRoot string `json:"data_root,omitempty" env:"YAO_DATA_ROOT" envDefault:""` // DATA PATH + ExtensionRoot string `json:"extension_root,omitempty" env:"YAO_EXTENSION_ROOT" envDefault:""` // Plugin, Wasm root PATH, Default is (/plugins /wasms) + Host string `json:"host,omitempty" env:"YAO_HOST" envDefault:"0.0.0.0"` // 服务监听地址 + Port int `json:"port,omitempty" env:"YAO_PORT" envDefault:"5099"` // 服务监听端口 + Cert string `json:"cert,omitempty" env:"YAO_CERT"` // HTTPS 证书文件地址 + Key string `json:"key,omitempty" env:"YAO_KEY"` // HTTPS 证书密钥地址 + Log string `json:"log,omitempty" env:"YAO_LOG"` // 服务日志地址 + LogMode string `json:"log_mode,omitempty" env:"YAO_LOG_MODE" envDefault:"TEXT"` // 服务日志模式 JSON|TEXT + JWTSecret string `json:"jwt_secret,omitempty" env:"YAO_JWT_SECRET"` // JWT 密钥 + DB Database `json:"db,omitempty"` // 数据库配置 + AllowFrom []string `json:"allowfrom,omitempty" envSeparator:"|" env:"YAO_ALLOW_FROM"` // Domain list the separator is | + Session Session `json:"session,omitempty"` // Session Config + Studio Studio `json:"studio,omitempty"` // Studio config + Runtime Runtime `json:"runtime,omitempty"` // Runtime config } -// StudioConfig the studio config -type StudioConfig struct { +// Studio the studio config +type Studio struct { Port int `json:"studio_port,omitempty" env:"YAO_STUDIO_PORT" envDefault:"5077"` // Studio port Secret []byte `json:"studio_secret,omitempty" env:"YAO_STUDIO_SECRET"` // Studio Secret, if does not set, auto-generate a secret Auto bool `json:"-"` } -// DBConfig 数据库配置 -type DBConfig struct { +// Database 数据库配置 +type Database struct { Driver string `json:"driver,omitempty" env:"YAO_DB_DRIVER" envDefault:"sqlite3"` // 数据库驱动 sqlite3| mysql| postgres Primary []string `json:"primary,omitempty" env:"YAO_DB_PRIMARY" envSeparator:"|" envDefault:"./db/yao.db"` // 主库连接DSN Secondary []string `json:"secondary,omitempty" env:"YAO_DB_SECONDARY" envSeparator:"|"` // 从库连接DSN AESKey string `json:"aeskey,omitempty" env:"YAO_DB_AESKEY"` // 加密存储KEY } -// SessionConfig 会话服务器 -type SessionConfig struct { +// Session 会话服务器 +type Session struct { Store string `json:"store,omitempty" env:"YAO_SESSION_STORE" envDefault:"file"` // The session store. redis | file File string `json:"file,omitempty" env:"YAO_SESSION_FILE"` // The file path Host string `json:"host,omitempty" env:"YAO_SESSION_HOST" envDefault:"127.0.0.1"` // The redis host @@ -47,3 +48,13 @@ type SessionConfig struct { DB string `json:"db,omitempty" env:"YAO_SESSION_DB" envDefault:"1"` // The redis username IsCLI bool `json:"iscli,omitempty" env:"YAO_SESSION_ISCLI" envDefault:"false"` // Command Line Start } + +// Runtime Config +type Runtime struct { + MinSize int `json:"minSize,omitempty" env:"YAO_RUNTIME_MIN" envDefault:"2"` // the number of V8 VM when runtime start. max value is 100, the default value is 2 + MaxSize int `json:"maxSize,omitempty" env:"YAO_RUNTIME_MAX" envDefault:"5"` // the maximum of V8 VM should be smaller than minSize, the default value is 10 + HeapSizeLimit uint64 `json:"heapSizeLimit,omitempty" env:"YAO_RUNTIME_HEAP_LIMIT" envDefault:"1518338048"` // the isolate heap size limit should be smaller than 1.5G, and the default value is 1518338048 (1.5G) + HeapSizeRelease uint64 `json:"heapSizeRelease,omitempty" env:"YAO_RUNTIME_HEAP_RELEASE" envDefault:"52428800"` // the isolate will be re-created when reaching this value, and the default value is 52428800 (50M) + HeapAvailableSize uint64 `json:"heapAvailableSize,omitempty" env:"YAO_RUNTIME_HEAP_AVAILABLE" envDefault:"524288000"` // the isolate will be re-created when the available size is smaller than this value, and the default value is 524288000 (500M) + Precompile bool `json:"precompile,omitempty" env:"YAO_RUNTIME_PRECOMPILE" envDefault:"false"` // if true compile scripts when the VM is created. this will increase the load time, but the script will run faster. the default value is false +} diff --git a/runtime/runtime.go b/runtime/runtime.go index 6ef3a9fa..cd6c75ce 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -1,11 +1,39 @@ package runtime -// // Load runtime -// func Load(cfg config.Config) error { -// dataRoot, err := fs.Root(cfg) -// if err != nil { -// return err -// } -// gou.LoadRuntime(runtime.Option{FileRoot: dataRoot}) -// return nil -// } +import ( + v8 "github.com/yaoapp/gou/runtime/v8" + "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/fs" +) + +// Start v8 runtime +func Start(cfg config.Config) error { + + dataRoot, err := fs.Root(cfg) + if err != nil { + return err + } + + option := &v8.Option{ + MinSize: cfg.Runtime.MinSize, + MaxSize: cfg.Runtime.MaxSize, + HeapSizeLimit: cfg.Runtime.HeapSizeLimit, + HeapAvailableSize: cfg.Runtime.HeapAvailableSize, + HeapSizeRelease: cfg.Runtime.HeapSizeRelease, + Precompile: cfg.Runtime.Precompile, + DataRoot: dataRoot, + } + + err = v8.Start(option) + if err != nil { + return err + } + + return nil +} + +// Stop v8 runtime +func Stop() error { + v8.Stop() + return nil +} diff --git a/runtime/runtime_test.go b/runtime/runtime_test.go new file mode 100644 index 00000000..3adf0664 --- /dev/null +++ b/runtime/runtime_test.go @@ -0,0 +1,15 @@ +package runtime + +import ( + "testing" + + "github.com/yaoapp/yao/config" +) + +func TestStart(t *testing.T) { + defer Stop() + err := Start(config.Conf) + if err != nil { + t.Fatal(err) + } +} diff --git a/share/db.go b/share/db.go index 93992833..f5750cd7 100644 --- a/share/db.go +++ b/share/db.go @@ -11,7 +11,7 @@ import ( ) // DBConnect 建立数据库连接 -func DBConnect(dbconfig config.DBConfig) (err error) { +func DBConnect(dbconfig config.Database) (err error) { if dbconfig.Primary == nil { return fmt.Errorf("YAO_DB_PRIMARY was not set") diff --git a/test/test.go b/test/test.go index 27c9d066..33b65931 100644 --- a/test/test.go +++ b/test/test.go @@ -12,6 +12,7 @@ import ( "github.com/yaoapp/kun/exception" "github.com/yaoapp/xun/capsule" "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/runtime" "github.com/yaoapp/yao/share" ) @@ -39,11 +40,13 @@ func Prepare(t *testing.T, cfg config.Config) { dbconnect(t, cfg) load(t, cfg) + start(t, cfg) } // Clean the test environment func Clean() { dbclose() + runtime.Stop() } func dbclose() { @@ -71,6 +74,13 @@ func dbconnect(t *testing.T, cfg config.Config) { } +func start(t *testing.T, cfg config.Config) { + err := runtime.Start(cfg) + if err != nil { + t.Fatal(err) + } +} + func load(t *testing.T, cfg config.Config) { loadModel(t, cfg) loadQuery(t, cfg)