Merge branch 'fix_on_config_update' into 'master'
fix receiving new configuration See merge request integration-system/isp-script-service!10
This commit is contained in:
commit
0efd7b247b
4 files changed
+40
-26
No files matched your search
@@ -1,3 +1,5 @@
|
|||||||
|
### v2.5.7
|
||||||
|
* fix receiving new configuration
|
||||||
### v2.5.6
|
### v2.5.6
|
||||||
* updated dependencies
|
* updated dependencies
|
||||||
### v2.5.5
|
### v2.5.5
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ func onShutdown(_ context.Context, _ os.Signal) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func onRemoteConfigReceive(remoteConfig, oldRemoteConfig *conf.RemoteConfig) {
|
func onRemoteConfigReceive(remoteConfig, oldRemoteConfig *conf.RemoteConfig) {
|
||||||
service.Script.ReceiveConfiguration(remoteConfig.Scripts)
|
service.Script.ReceiveConfiguration(remoteConfig.Scripts, remoteConfig.SharedScript, remoteConfig.ScriptExecutionTimeoutMs)
|
||||||
metric.InitCollectors(remoteConfig.Metrics, oldRemoteConfig.Metrics)
|
metric.InitCollectors(remoteConfig.Metrics, oldRemoteConfig.Metrics)
|
||||||
metric.InitHttpServer(remoteConfig.Metrics)
|
metric.InitHttpServer(remoteConfig.Metrics)
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-24
@@ -9,7 +9,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/integration-system/isp-lib/v2/config"
|
|
||||||
"github.com/integration-system/isp-lib/v2/scripts"
|
"github.com/integration-system/isp-lib/v2/scripts"
|
||||||
log "github.com/integration-system/isp-log"
|
log "github.com/integration-system/isp-log"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -30,56 +29,68 @@ type scriptService struct {
|
|||||||
scriptEngine *scripts.Engine
|
scriptEngine *scripts.Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type scriptServiceCfg struct {
|
||||||
|
Scripts map[string]CompiledScript
|
||||||
|
ExecutionTimeout time.Duration
|
||||||
|
SharedScript string
|
||||||
|
}
|
||||||
|
|
||||||
type CompiledScript struct {
|
type CompiledScript struct {
|
||||||
scripts.Script
|
scripts.Script
|
||||||
Compiled bool
|
Compiled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *scriptService) ReceiveConfiguration(scriptDef []conf.ScriptDefinition) {
|
func (s *scriptService) ReceiveConfiguration(scriptDef []conf.ScriptDefinition, sharedScript string, executionTimeoutMs int) {
|
||||||
var compiled bool
|
var compiled bool
|
||||||
newStore := make(map[string]CompiledScript)
|
cfg := &scriptServiceCfg{
|
||||||
|
Scripts: make(map[string]CompiledScript),
|
||||||
|
ExecutionTimeout: time.Duration(executionTimeoutMs) * time.Millisecond,
|
||||||
|
SharedScript: sharedScript,
|
||||||
|
}
|
||||||
for i, value := range scriptDef {
|
for i, value := range scriptDef {
|
||||||
compiled = true
|
compiled = true
|
||||||
scr, err := s.Create(value.Script)
|
scr, err := s.Create(value.Script, sharedScript)
|
||||||
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
|
compiled = false
|
||||||
}
|
}
|
||||||
newStore[value.Id] = CompiledScript{scr, compiled}
|
cfg.Scripts[value.Id] = CompiledScript{scr, compiled}
|
||||||
}
|
}
|
||||||
s.store.Store(newStore)
|
s.store.Store(cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *scriptService) Execute(req domain.ExecuteRequest) *domain.ScriptResp {
|
func (s *scriptService) Execute(req domain.ExecuteRequest) *domain.ScriptResp {
|
||||||
scr, err := s.Create(req.Script)
|
cfg := s.loadCfg()
|
||||||
|
scr, err := s.Create(req.Script, cfg.SharedScript)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return s.respError(err, domain.ErrorCompile)
|
return s.respError(err, domain.ErrorCompile)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.executeScript(CompiledScript{scr, true}, req.Arg)
|
return s.executeScript(CompiledScript{scr, true}, req.Arg, cfg.ExecutionTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
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]CompiledScript)[req.Id]
|
cfg := s.loadCfg()
|
||||||
|
scr, ok := cfg.Scripts[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)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.executeScript(scr, req.Arg), nil
|
return s.executeScript(scr, req.Arg, cfg.ExecutionTimeout), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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]CompiledScript)
|
cfg := s.loadCfg()
|
||||||
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)
|
||||||
go func(i int) {
|
go func(i int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if scr, ok := store[req[i].Id]; !ok {
|
if scr, ok := cfg.Scripts[req[i].Id]; !ok {
|
||||||
response[i] = *s.respError(errors.Errorf("not defined script for id %s", req[i].Id), domain.ErrorCompile)
|
response[i] = *s.respError(errors.Errorf("not defined script for id %s", req[i].Id), domain.ErrorCompile)
|
||||||
} else {
|
} else {
|
||||||
response[i] = *s.executeScript(scr, req[i].Arg)
|
response[i] = *s.executeScript(scr, req[i].Arg, cfg.ExecutionTimeout)
|
||||||
}
|
}
|
||||||
}(i)
|
}(i)
|
||||||
}
|
}
|
||||||
@@ -90,16 +101,16 @@ 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]CompiledScript)
|
cfg := s.loadCfg()
|
||||||
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)
|
||||||
go func(i int) {
|
go func(i int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if scr, ok := store[req.Ids[i]]; !ok {
|
if scr, ok := cfg.Scripts[req.Ids[i]]; !ok {
|
||||||
response[i] = *s.respError(errors.Errorf("not defined script for id %s", req.Ids[i]), domain.ErrorCompile)
|
response[i] = *s.respError(errors.Errorf("not defined script for id %s", req.Ids[i]), domain.ErrorCompile)
|
||||||
} else {
|
} else {
|
||||||
response[i] = *s.executeScript(scr, req.Arg)
|
response[i] = *s.executeScript(scr, req.Arg, cfg.ExecutionTimeout)
|
||||||
}
|
}
|
||||||
}(i)
|
}(i)
|
||||||
}
|
}
|
||||||
@@ -108,23 +119,20 @@ func (s *scriptService) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []
|
|||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*scriptService) Create(scr string) (scripts.Script, error) {
|
func (*scriptService) Create(script, sharedScript string) (scripts.Script, error) {
|
||||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
return scripts.NewScript([]byte(sharedScript),
|
||||||
|
[]byte("(function() {\n"), []byte(script), []byte("\n})();"))
|
||||||
return scripts.NewScript([]byte(cfg.SharedScript),
|
|
||||||
[]byte("(function() {\n"), []byte(scr), []byte("\n})();"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var errEmpty = errors.New("empty answer, maybe lost return")
|
var errEmpty = errors.New("empty answer, maybe lost return")
|
||||||
|
|
||||||
func (s *scriptService) executeScript(scr CompiledScript, arg interface{}) *domain.ScriptResp {
|
func (s *scriptService) executeScript(scr CompiledScript, arg interface{}, timeout time.Duration) *domain.ScriptResp {
|
||||||
if !scr.Compiled {
|
if !scr.Compiled {
|
||||||
return s.respError(errors.New("invalid script configuration"), domain.ErrorCompile)
|
return s.respError(errors.New("invalid script configuration"), domain.ErrorCompile)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := config.GetRemote().(*conf.RemoteConfig)
|
|
||||||
response, err := s.scriptEngine.Execute(scr.Script, arg,
|
response, err := s.scriptEngine.Execute(scr.Script, arg,
|
||||||
scripts.WithScriptTimeout(time.Duration(cfg.ScriptExecutionTimeoutMs)*time.Millisecond),
|
scripts.WithScriptTimeout(timeout),
|
||||||
// TODO: remove. invoke is deprecated, all functions should be inside `external` object
|
// TODO: remove. invoke is deprecated, all functions should be inside `external` object
|
||||||
scripts.WithSet("invoke", router.Invoke),
|
scripts.WithSet("invoke", router.Invoke),
|
||||||
scripts.WithSet("external", map[string]interface{}{
|
scripts.WithSet("external", map[string]interface{}{
|
||||||
@@ -157,6 +165,10 @@ func (*scriptService) respError(err error, errorType string) *domain.ScriptResp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *scriptService) loadCfg() *scriptServiceCfg {
|
||||||
|
return s.store.Load().(*scriptServiceCfg)
|
||||||
|
}
|
||||||
|
|
||||||
func Sha256(value string) string {
|
func Sha256(value string) string {
|
||||||
sum := sha256.Sum256([]byte(value))
|
sum := sha256.Sum256([]byte(value))
|
||||||
return hex.EncodeToString(sum[:])
|
return hex.EncodeToString(sum[:])
|
||||||
|
|||||||
Reference in new issue
Block a user