Merge branch 'add_batch' into 'master'
add batch execution See merge request integration-system/isp-script-service!3
This commit is contained in:
commit
e52217a99b
13 files changed
+132
-89
No files matched your search
@@ -1,3 +1,5 @@
|
||||
### V2.2.0
|
||||
* add batch execution
|
||||
### V2.1.1
|
||||
* update script engine
|
||||
### v2.1.0
|
||||
|
||||
@@ -10,11 +10,11 @@ import (
|
||||
"github.com/integration-system/isp-lib/v2/config"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"isp-script-service/compile"
|
||||
"isp-script-service/conf"
|
||||
"isp-script-service/controller"
|
||||
"isp-script-service/domain"
|
||||
"isp-script-service/script"
|
||||
"isp-script-service/service"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -85,7 +85,7 @@ func BenchmarkGoCustomData(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkJsCustomData(b *testing.B) {
|
||||
scr, err := compile.Script.Create(request.Script)
|
||||
scr, err := service.Script.Create(request.Script)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -95,7 +95,7 @@ func BenchmarkJsCustomData(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkInlineJson(b *testing.B) {
|
||||
c, err := compile.Script.Create("var b = JSON.parse(arg); return JSON.stringify(b)")
|
||||
c, err := service.Script.Create("var b = JSON.parse(arg); return JSON.stringify(b)")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -112,7 +112,7 @@ func BenchmarkInlineJson(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkJsoniterJson(b *testing.B) {
|
||||
c, err := compile.Script.Create("var b = arg; return b")
|
||||
c, err := service.Script.Create("var b = arg; return b")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package compile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/integration-system/isp-lib/v2/config"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"isp-script-service/codes"
|
||||
"isp-script-service/conf"
|
||||
"isp-script-service/script"
|
||||
)
|
||||
|
||||
var (
|
||||
Script compileScript
|
||||
remoteScripts = make(map[string]script.Script)
|
||||
)
|
||||
|
||||
type compileScript struct{}
|
||||
|
||||
func (compileScript) GetById(id string) script.Script {
|
||||
return remoteScripts[id]
|
||||
}
|
||||
|
||||
func (compileScript) Init(scriptDef []conf.ScriptDefinition) {
|
||||
for i, value := range scriptDef {
|
||||
prog, err := Script.Create(value.Script)
|
||||
if err != nil {
|
||||
log.Errorf(codes.CreateScriptFromConfigError, "create script from config (number %d): %v", i, err)
|
||||
continue
|
||||
}
|
||||
remoteScripts[value.Id] = prog
|
||||
}
|
||||
}
|
||||
|
||||
func (compileScript) Create(scr string) (script.Script, error) {
|
||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
||||
scr = fmt.Sprintf("%s%s%s%s", cfg.SharedScript, "(function() {", scr, "})();")
|
||||
return script.Default().Compile(scr)
|
||||
}
|
||||
+5
-36
@@ -1,12 +1,8 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"isp-script-service/compile"
|
||||
"isp-script-service/domain"
|
||||
"isp-script-service/script"
|
||||
"isp-script-service/service"
|
||||
)
|
||||
|
||||
var Script scriptController
|
||||
@@ -14,40 +10,13 @@ var Script scriptController
|
||||
type scriptController struct{}
|
||||
|
||||
func (c scriptController) ExecuteById(req domain.ExecuteByIdRequest) (*domain.ScriptResp, error) {
|
||||
scr := compile.Script.GetById(req.Id)
|
||||
if scr == nil {
|
||||
return nil, status.Errorf(codes.NotFound, "not defined script for id %s", req.Id)
|
||||
}
|
||||
return c.executeScript(scr, req.Arg), nil
|
||||
return service.Script.ExecuteById(req)
|
||||
}
|
||||
|
||||
func (c scriptController) Execute(req domain.ExecuteRequest) *domain.ScriptResp {
|
||||
scr, err := compile.Script.Create(req.Script)
|
||||
if err != nil {
|
||||
return c.respError(err, domain.ErrorCompile)
|
||||
}
|
||||
return c.executeScript(scr, req.Arg)
|
||||
return service.Script.Execute(req)
|
||||
}
|
||||
|
||||
func (c scriptController) executeScript(scr script.Script, arg interface{}) *domain.ScriptResp {
|
||||
response, err := script.Default().Execute(scr, arg)
|
||||
if err != nil {
|
||||
return c.respError(err, domain.ErrorRunTime)
|
||||
}
|
||||
if response == nil {
|
||||
return c.respError(errors.New("empty answer, maybe lost return"), domain.ErrorRunTime)
|
||||
}
|
||||
return &domain.ScriptResp{
|
||||
Result: response,
|
||||
}
|
||||
}
|
||||
|
||||
func (c scriptController) respError(err error, errorType string) *domain.ScriptResp {
|
||||
respError := domain.Error{
|
||||
Type: errorType,
|
||||
Description: err.Error(),
|
||||
}
|
||||
return &domain.ScriptResp{
|
||||
Error: &respError,
|
||||
}
|
||||
func (c scriptController) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []domain.ScriptResp {
|
||||
return service.Script.BatchExecuteById(req)
|
||||
}
|
||||
@@ -9,3 +9,8 @@ type ExecuteRequest struct {
|
||||
Script string `valid:"required~Required"`
|
||||
Arg interface{}
|
||||
}
|
||||
|
||||
type BatchExecuteByIdsRequest struct {
|
||||
Ids []string `valid:"required~Required"`
|
||||
Arg interface{}
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
json2 "encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/robertkrimen/otto"
|
||||
"github.com/robertkrimen/otto/parser"
|
||||
@@ -9,8 +12,6 @@ import (
|
||||
"github.com/yuin/gopher-lua"
|
||||
"github.com/yuin/gopher-lua/parse"
|
||||
"layeh.com/gopher-json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -10,6 +10,7 @@ require (
|
||||
github.com/integration-system/isp-log v1.1.3
|
||||
github.com/jmoiron/jsonq v0.0.0-20150511023944-e874b168d07e
|
||||
github.com/json-iterator/go v1.1.9
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff
|
||||
github.com/stretchr/testify v1.5.1
|
||||
github.com/yuin/gopher-lua v0.0.0-20191220021717-ab39c6098bdb
|
||||
|
||||
@@ -216,6 +216,7 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181
|
||||
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
|
||||
+6
-4
@@ -6,14 +6,16 @@ import (
|
||||
)
|
||||
|
||||
type ScriptHandlers struct {
|
||||
ExecuteById func(request domain.ExecuteByIdRequest) (*domain.ScriptResp, error) `method:"execute_by_id" group:"script"`
|
||||
Execute func(request domain.ExecuteRequest) *domain.ScriptResp `method:"execute" group:"script"`
|
||||
ExecuteById func(request domain.ExecuteByIdRequest) (*domain.ScriptResp, error) `method:"execute_by_id" group:"script"`
|
||||
Execute func(request domain.ExecuteRequest) *domain.ScriptResp `method:"execute" group:"script"`
|
||||
BatchExecuteByIds func(request domain.BatchExecuteByIdsRequest) []domain.ScriptResp `method:"batch_execute_by_ids" group:"script"`
|
||||
}
|
||||
|
||||
func GetTaskHandler() *ScriptHandlers {
|
||||
return &ScriptHandlers{
|
||||
ExecuteById: controller.Script.ExecuteById,
|
||||
Execute: controller.Script.Execute,
|
||||
ExecuteById: controller.Script.ExecuteById,
|
||||
Execute: controller.Script.Execute,
|
||||
BatchExecuteByIds: controller.Script.BatchExecuteById,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,10 +12,10 @@ import (
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/integration-system/isp-log/stdcodes"
|
||||
"google.golang.org/grpc"
|
||||
"isp-script-service/compile"
|
||||
"isp-script-service/conf"
|
||||
"isp-script-service/helper"
|
||||
"isp-script-service/router"
|
||||
"isp-script-service/service"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -55,7 +55,7 @@ func onShutdown(_ context.Context, _ os.Signal) {
|
||||
}
|
||||
|
||||
func onRemoteConfigReceive(remoteConfig, oldRemoteConfig *conf.RemoteConfig) {
|
||||
compile.Script.Init(remoteConfig.Scripts)
|
||||
service.Script.ReceiveConfiguration(remoteConfig.Scripts)
|
||||
metric.InitCollectors(remoteConfig.Metrics, oldRemoteConfig.Metrics)
|
||||
metric.InitHttpServer(remoteConfig.Metrics)
|
||||
}
|
||||
|
||||
+2
-1
@@ -3,9 +3,10 @@ package script
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGoja_AddFunction(t *testing.T) {
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/integration-system/isp-lib/v2/config"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
log_code "isp-script-service/codes"
|
||||
"isp-script-service/conf"
|
||||
"isp-script-service/domain"
|
||||
"isp-script-service/script"
|
||||
)
|
||||
|
||||
var Script = &scriptService{}
|
||||
|
||||
type scriptService struct {
|
||||
store atomic.Value
|
||||
}
|
||||
|
||||
func (s *scriptService) ReceiveConfiguration(scriptDef []conf.ScriptDefinition) {
|
||||
newStore := make(map[string]script.Script)
|
||||
for i, value := range scriptDef {
|
||||
scr, err := s.Create(value.Script)
|
||||
if err != nil {
|
||||
log.Errorf(log_code.CreateScriptFromConfigError, "create script from config (number %d): %v", i, err)
|
||||
continue
|
||||
}
|
||||
newStore[value.Id] = scr
|
||||
}
|
||||
s.store.Store(newStore)
|
||||
}
|
||||
|
||||
func (s *scriptService) Execute(req domain.ExecuteRequest) *domain.ScriptResp {
|
||||
scr, err := s.Create(req.Script)
|
||||
if err != nil {
|
||||
return s.respError(err, domain.ErrorCompile)
|
||||
}
|
||||
return s.executeScript(scr, req.Arg)
|
||||
}
|
||||
|
||||
func (s *scriptService) ExecuteById(req domain.ExecuteByIdRequest) (*domain.ScriptResp, error) {
|
||||
scr := s.store.Load().(map[string]script.Script)[req.Id]
|
||||
if scr == nil {
|
||||
return nil, status.Errorf(codes.NotFound, "not defined script for id %s", req.Id)
|
||||
}
|
||||
return s.executeScript(scr, req.Arg), nil
|
||||
}
|
||||
|
||||
func (s *scriptService) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []domain.ScriptResp {
|
||||
wg := sync.WaitGroup{}
|
||||
store := s.store.Load().(map[string]script.Script)
|
||||
response := make([]domain.ScriptResp, len(req.Ids))
|
||||
for i := range req.Ids {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
if store[req.Ids[i]] == nil {
|
||||
response[i] = *s.respError(errors.Errorf("not defined script for id %s", req.Ids[i]), domain.ErrorCompile)
|
||||
} else {
|
||||
response[i] = *s.executeScript(store[req.Ids[i]], req.Arg)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
return response
|
||||
}
|
||||
|
||||
func (*scriptService) Create(scr string) (script.Script, error) {
|
||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
||||
scr = fmt.Sprintf("%s%s%s%s", cfg.SharedScript, "(function() {", scr, "})();")
|
||||
return script.Default().Compile(scr)
|
||||
}
|
||||
|
||||
func (s *scriptService) executeScript(scr script.Script, arg interface{}) *domain.ScriptResp {
|
||||
response, err := script.Default().Execute(scr, arg)
|
||||
if err != nil {
|
||||
return s.respError(err, domain.ErrorRunTime)
|
||||
}
|
||||
if response == nil {
|
||||
return s.respError(errors.New("empty answer, maybe lost return"), domain.ErrorRunTime)
|
||||
}
|
||||
return &domain.ScriptResp{
|
||||
Result: response,
|
||||
}
|
||||
}
|
||||
|
||||
func (*scriptService) respError(err error, errorType string) *domain.ScriptResp {
|
||||
respError := domain.Error{
|
||||
Type: errorType,
|
||||
Description: err.Error(),
|
||||
}
|
||||
return &domain.ScriptResp{
|
||||
Error: &respError,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user