Browse Source

fix 异常重启及部分bug

tags/v0.9.2
3136352472 5 years ago
parent
commit
c20041b32d
  1. 10
      govue/cmd/main.go
  2. 54
      jsruntime/runtime.go
  3. 36
      pool/pool.go
  4. 1
      static/use.js

10
govue/cmd/main.go

@ -96,9 +96,13 @@ func main() {
}) })
gv.StartPoolLog() gv.StartPoolLog()
err = r.Run(config.Addr)
if err != nil {
log.Fatalln("服务意外停止:", err)
for {
err = r.Run(config.Addr)
if err != nil {
log.Println("服务意外停止:", err)
}
} }
} }

54
jsruntime/runtime.go

@ -11,18 +11,16 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"time"
) )
type JsRuntime struct { type JsRuntime struct {
MainSrc string
UseFileName string
UseSrc string
ExtFileDir string
Relys Relys
TimeoutSec int64
runtime *goja.Runtime
mutex sync.Mutex
MainSrc string
UseSrcFun func() string
ExtFileDir string
Relys Relys
TimeoutSec int64
runtime *goja.Runtime
mutex sync.Mutex
} }
type Rely struct { type Rely struct {
@ -38,14 +36,13 @@ const (
//ModeAsync RunMode = 2 //ModeAsync RunMode = 2
) )
func NewJsRuntime(mainScript string, useFileName string, extFileDir string, rels Relys, mode RunMode) (jr *JsRuntime, err error) {
func NewJsRuntime(mainScript string, extFileDir string, rels Relys, mode RunMode) (jr *JsRuntime, err error) {
jr = &JsRuntime{ jr = &JsRuntime{
MainSrc: mainScript,
Relys: rels,
UseFileName: useFileName,
ExtFileDir: extFileDir,
TimeoutSec: 30,
MainSrc: mainScript,
Relys: rels,
ExtFileDir: extFileDir,
TimeoutSec: 30,
} }
jr.runtime = goja.New() jr.runtime = goja.New()
@ -90,16 +87,6 @@ func (jr *JsRuntime) InitJsCode() error {
} }
} }
useSrc, _ := ioutil.ReadFile(jr.UseFileName)
jr.UseSrc = string(useSrc)
go func() {
for {
useSrc, _ := ioutil.ReadFile(jr.UseFileName)
jr.UseSrc = string(useSrc)
time.Sleep(time.Second * 2)
}
}()
return nil return nil
} }
@ -137,22 +124,19 @@ func (jr *JsRuntime) Render(filePath, href, tplSrc string, cb func(data string))
defer jr.mutex.Unlock() defer jr.mutex.Unlock()
runtime := jr.runtime runtime := jr.runtime
timeoutSec := jr.TimeoutSec
useSrc := jr.UseSrc
mainSrc := jr.MainSrc mainSrc := jr.MainSrc
isLock := true isLock := true
go func() {
time.Sleep(time.Duration(timeoutSec) * time.Second)
if isLock {
log.Println("timeout")
runtime.Interrupt("timeout")
runtime.ClearInterrupt()
}
}()
//timeoutSec := jr.TimeoutSec
defer func() { defer func() {
isLock = false isLock = false
jr.runtime.ClearInterrupt()
}() }()
var useSrc string
if jr.UseSrcFun != nil {
useSrc = jr.UseSrcFun()
}
if useSrc != "" { if useSrc != "" {
err = jr.RunCode(useSrc) err = jr.RunCode(useSrc)
if err != nil { if err != nil {

36
pool/pool.go

@ -4,6 +4,7 @@ import (
"context" "context"
"git.ouxuan.net/3136352472/go-service-template/jsruntime" "git.ouxuan.net/3136352472/go-service-template/jsruntime"
"github.com/jolestar/go-commons-pool" "github.com/jolestar/go-commons-pool"
"io/ioutil"
"log" "log"
"time" "time"
) )
@ -20,13 +21,17 @@ type Config struct {
var DefaultConfig Config var DefaultConfig Config
var useSrc string
func NewJsRuntimePool(mainScript string, useFileName string, staticPath string, relys jsruntime.Relys, mode jsruntime.RunMode) (jrp *JsRuntimePool) { func NewJsRuntimePool(mainScript string, useFileName string, staticPath string, relys jsruntime.Relys, mode jsruntime.RunMode) (jrp *JsRuntimePool) {
jrp = &JsRuntimePool{} jrp = &JsRuntimePool{}
factory := pool.NewPooledObjectFactorySimple( factory := pool.NewPooledObjectFactorySimple(
func(context.Context) (interface{}, error) { func(context.Context) (interface{}, error) {
render, err := jsruntime.NewJsRuntime(mainScript, useFileName, staticPath, relys, mode)
render, err := jsruntime.NewJsRuntime(mainScript, staticPath, relys, mode)
render.UseSrcFun = func() string {
return useSrc
}
return render, err return render, err
}) })
@ -51,13 +56,34 @@ func NewJsRuntimePool(mainScript string, useFileName string, staticPath string,
jrp.jsRuntimePool.Config.MaxTotal = DefaultConfig.MaxTotal jrp.jsRuntimePool.Config.MaxTotal = DefaultConfig.MaxTotal
jrp.jsRuntimePool.Config.BlockWhenExhausted = true jrp.jsRuntimePool.Config.BlockWhenExhausted = true
go func() {
var preparePoolTask func()
preparePoolTask = func() {
defer func() {
recover()
time.Sleep(time.Second)
preparePoolTask()
}()
for { for {
jrp.jsRuntimePool.PreparePool(jrp.ctx) jrp.jsRuntimePool.PreparePool(jrp.ctx)
time.Sleep(time.Second / 5) time.Sleep(time.Second / 5)
} }
}()
}
go preparePoolTask()
var useSrcTask func()
useSrcTask = func() {
defer func() {
recover()
time.Sleep(time.Second)
useSrcTask()
}()
for {
raw, _ := ioutil.ReadFile(useFileName)
useSrc = string(raw)
time.Sleep(time.Second)
}
}
go useSrcTask()
return return
} }

1
static/use.js

@ -20,7 +20,6 @@ GoUse(function () {
//分页面 //分页面
GoUseRegistered("index", function (query) { GoUseRegistered("index", function (query) {
console.log("query:", JSON.stringify(query));
new Vue({ new Vue({
el: "#app", el: "#app",

Loading…
Cancel
Save