added compile-checking

This commit is contained in:
riabovvo committed 2021-02-28 11:51:13 +03:00
1 parent f8ebd710d6
commit ff3dbca538
3 files changed
+23 -11

No files matched your search

+1 -1
View File
@@ -1 +1 @@
2.5.2 2.5.3
+2
View File
@@ -1,3 +1,5 @@
### v2.5.3
* added "invalid script configuration" error instead of "404:Not Found" error when invalid script was requested.
### v2.5.2 ### v2.5.2
* updated isp-lib * updated isp-lib
### v2.5.1 ### v2.5.1
+20 -10
View File
@@ -26,16 +26,22 @@ type scriptService struct {
scriptEngine *scripts.Engine scriptEngine *scripts.Engine
} }
type CompiledScript struct {
scripts.Script
Compiled bool
}
func (s *scriptService) ReceiveConfiguration(scriptDef []conf.ScriptDefinition) { func (s *scriptService) ReceiveConfiguration(scriptDef []conf.ScriptDefinition) {
newStore := make(map[string]scripts.Script) var compiled bool
newStore := make(map[string]CompiledScript)
for i, value := range scriptDef { for i, value := range scriptDef {
compiled = true
scr, err := s.Create(value.Script) scr, err := s.Create(value.Script)
if err != nil { if err != nil {
log.Errorf(log_code.CreateScriptFromConfigError, "create script from config (number %d): %v", i, err) log.Errorf(log_code.CreateScriptFromConfigError, "create script from config (number %d): %v", i, err)
compiled = false
continue
} }
newStore[value.Id] = scr newStore[value.Id] = CompiledScript{scr, compiled}
} }
s.store.Store(newStore) s.store.Store(newStore)
} }
@@ -46,11 +52,11 @@ func (s *scriptService) Execute(req domain.ExecuteRequest) *domain.ScriptResp {
return s.respError(err, domain.ErrorCompile) return s.respError(err, domain.ErrorCompile)
} }
return s.executeScript(scr, req.Arg) return s.executeScript(CompiledScript{scr, true}, req.Arg)
} }
func (s *scriptService) ExecuteById(req domain.ExecuteByIdRequest) (*domain.ScriptResp, error) { func (s *scriptService) ExecuteById(req domain.ExecuteByIdRequest) (*domain.ScriptResp, error) {
scr, ok := s.store.Load().(map[string]scripts.Script)[req.Id] scr, ok := s.store.Load().(map[string]CompiledScript)[req.Id]
if !ok { if !ok {
return nil, status.Errorf(codes.NotFound, "not defined script for id %s", req.Id) return nil, status.Errorf(codes.NotFound, "not defined script for id %s", req.Id)
} }
@@ -60,7 +66,7 @@ func (s *scriptService) ExecuteById(req domain.ExecuteByIdRequest) (*domain.Scri
func (s *scriptService) BatchExecute(req []domain.ExecuteByIdRequest) []domain.ScriptResp { func (s *scriptService) BatchExecute(req []domain.ExecuteByIdRequest) []domain.ScriptResp {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
store := s.store.Load().(map[string]scripts.Script) store := s.store.Load().(map[string]CompiledScript)
response := make([]domain.ScriptResp, len(req)) response := make([]domain.ScriptResp, len(req))
for i := range req { for i := range req {
wg.Add(1) wg.Add(1)
@@ -80,7 +86,7 @@ func (s *scriptService) BatchExecute(req []domain.ExecuteByIdRequest) []domain.S
func (s *scriptService) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []domain.ScriptResp { func (s *scriptService) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []domain.ScriptResp {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
store := s.store.Load().(map[string]scripts.Script) store := s.store.Load().(map[string]CompiledScript)
response := make([]domain.ScriptResp, len(req.Ids)) response := make([]domain.ScriptResp, len(req.Ids))
for i := range req.Ids { for i := range req.Ids {
wg.Add(1) wg.Add(1)
@@ -107,9 +113,13 @@ func (*scriptService) Create(scr string) (scripts.Script, error) {
var errEmpty = errors.New("empty answer, maybe lost return") var errEmpty = errors.New("empty answer, maybe lost return")
func (s *scriptService) executeScript(scr scripts.Script, arg interface{}) *domain.ScriptResp { func (s *scriptService) executeScript(scr CompiledScript, arg interface{}) *domain.ScriptResp {
if !scr.Compiled {
return s.respError(errors.New("invalid script configuration"), domain.ErrorCompile)
}
cfg := config.GetRemote().(*conf.RemoteConfig) cfg := config.GetRemote().(*conf.RemoteConfig)
response, err := s.scriptEngine.Execute(scr, arg, response, err := s.scriptEngine.Execute(scr.Script, arg,
scripts.WithScriptTimeout(time.Duration(cfg.ScriptExecutionTimeoutMs)*time.Millisecond)) scripts.WithScriptTimeout(time.Duration(cfg.ScriptExecutionTimeoutMs)*time.Millisecond))
if err != nil { if err != nil {
return s.respError(err, domain.ErrorRunTime) return s.respError(err, domain.ErrorRunTime)