Small Refactoring
This commit is contained in:
1 parent
9ccc1c1728
commit
24d35e0505
9 files changed
+66
-71
No files matched your search
+12
-4
@@ -2,6 +2,10 @@ package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
log_code "isp-script-service/codes"
|
||||
"isp-script-service/conf"
|
||||
"isp-script-service/domain"
|
||||
"isp-script-service/script"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
@@ -10,10 +14,6 @@ import (
|
||||
"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{}
|
||||
@@ -28,6 +28,7 @@ func (s *scriptService) ReceiveConfiguration(scriptDef []conf.ScriptDefinition)
|
||||
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
|
||||
@@ -40,6 +41,7 @@ func (s *scriptService) Execute(req domain.ExecuteRequest) *domain.ScriptResp {
|
||||
if err != nil {
|
||||
return s.respError(err, domain.ErrorCompile)
|
||||
}
|
||||
|
||||
return s.executeScript(scr, req.Arg)
|
||||
}
|
||||
|
||||
@@ -48,6 +50,7 @@ func (s *scriptService) ExecuteById(req domain.ExecuteByIdRequest) (*domain.Scri
|
||||
if scr == nil {
|
||||
return nil, status.Errorf(codes.NotFound, "not defined script for id %s", req.Id)
|
||||
}
|
||||
|
||||
return s.executeScript(scr, req.Arg), nil
|
||||
}
|
||||
|
||||
@@ -67,6 +70,7 @@ func (s *scriptService) BatchExecute(req []domain.ExecuteByIdRequest) []domain.S
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
@@ -86,12 +90,14 @@ func (s *scriptService) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []
|
||||
}(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)
|
||||
}
|
||||
|
||||
@@ -103,6 +109,7 @@ func (s *scriptService) executeScript(scr script.Script, arg interface{}) *domai
|
||||
if response == nil {
|
||||
return s.respError(errors.New("empty answer, maybe lost return"), domain.ErrorRunTime)
|
||||
}
|
||||
|
||||
return &domain.ScriptResp{
|
||||
Result: response,
|
||||
}
|
||||
@@ -113,6 +120,7 @@ func (*scriptService) respError(err error, errorType string) *domain.ScriptResp
|
||||
Type: errorType,
|
||||
Description: err.Error(),
|
||||
}
|
||||
|
||||
return &domain.ScriptResp{
|
||||
Error: &respError,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user