Migrate to isp-lib scripts

This commit is contained in:
Кирющенко Егор Александрович authored and Мержанов Максим Владимирович committed 2020-12-29 15:04:34 +03:00
1 parent fb1deb0bc9
commit 3501be7ebb
9 files changed
+53 -191

No files matched your search

+23 -18
View File
@@ -1,14 +1,14 @@
package service
import (
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/integration-system/isp-lib/v2/scripts"
log_code "isp-script-service/codes"
"isp-script-service/conf"
"isp-script-service/domain"
"isp-script-service/script"
"github.com/integration-system/isp-lib/v2/config"
log "github.com/integration-system/isp-log"
@@ -17,14 +17,17 @@ import (
"google.golang.org/grpc/status"
)
var Script = &scriptService{}
var Script = &scriptService{
scriptEngine: scripts.NewEngine(),
}
type scriptService struct {
store atomic.Value
store atomic.Value
scriptEngine *scripts.Engine
}
func (s *scriptService) ReceiveConfiguration(scriptDef []conf.ScriptDefinition) {
newStore := make(map[string]script.Script)
newStore := make(map[string]scripts.Script)
for i, value := range scriptDef {
scr, err := s.Create(value.Script)
if err != nil {
@@ -47,8 +50,8 @@ func (s *scriptService) Execute(req domain.ExecuteRequest) *domain.ScriptResp {
}
func (s *scriptService) ExecuteById(req domain.ExecuteByIdRequest) (*domain.ScriptResp, error) {
scr := s.store.Load().(map[string]script.Script)[req.Id]
if scr == nil {
scr, ok := s.store.Load().(map[string]scripts.Script)[req.Id]
if !ok {
return nil, status.Errorf(codes.NotFound, "not defined script for id %s", req.Id)
}
@@ -57,16 +60,16 @@ func (s *scriptService) ExecuteById(req domain.ExecuteByIdRequest) (*domain.Scri
func (s *scriptService) BatchExecute(req []domain.ExecuteByIdRequest) []domain.ScriptResp {
wg := sync.WaitGroup{}
store := s.store.Load().(map[string]script.Script)
store := s.store.Load().(map[string]scripts.Script)
response := make([]domain.ScriptResp, len(req))
for i := range req {
wg.Add(1)
go func(i int) {
defer wg.Done()
if store[req[i].Id] == nil {
if scr, ok := store[req[i].Id]; !ok {
response[i] = *s.respError(errors.Errorf("not defined script for id %s", req[i].Id), domain.ErrorCompile)
} else {
response[i] = *s.executeScript(store[req[i].Id], req[i].Arg)
response[i] = *s.executeScript(scr, req[i].Arg)
}
}(i)
}
@@ -77,16 +80,16 @@ func (s *scriptService) BatchExecute(req []domain.ExecuteByIdRequest) []domain.S
func (s *scriptService) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []domain.ScriptResp {
wg := sync.WaitGroup{}
store := s.store.Load().(map[string]script.Script)
store := s.store.Load().(map[string]scripts.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 {
if scr, ok := store[req.Ids[i]]; !ok {
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)
response[i] = *s.executeScript(scr, req.Arg)
}
}(i)
}
@@ -95,17 +98,19 @@ func (s *scriptService) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []
return response
}
func (*scriptService) Create(scr string) (script.Script, error) {
func (*scriptService) Create(scr string) (scripts.Script, error) {
cfg := config.GetRemote().(*conf.RemoteConfig)
scr = fmt.Sprintf("%s%s%s%s", cfg.SharedScript, "(function() {", scr, "})();")
return script.Default().Compile(scr)
return scripts.NewScript([]byte(cfg.SharedScript),
[]byte("(function() {\n"), []byte(scr), []byte("\n})();"))
}
var errEmpty = errors.New("empty answer, maybe lost return")
func (s *scriptService) executeScript(scr script.Script, arg interface{}) *domain.ScriptResp {
response, err := script.Default().Execute(scr, arg)
func (s *scriptService) executeScript(scr scripts.Script, arg interface{}) *domain.ScriptResp {
cfg := config.GetRemote().(*conf.RemoteConfig)
response, err := s.scriptEngine.Execute(scr, arg,
scripts.WithScriptTimeout(time.Duration(cfg.ScriptExecutionTimeoutMs)*time.Millisecond))
if err != nil {
return s.respError(err, domain.ErrorRunTime)
}