From 983474cdb78ca348858f55a7c8adc75968b4d724 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Tue, 16 Jun 2020 14:17:31 +0300 Subject: [PATCH] additional batch method --- .version | 2 +- CHANGELOG.md | 2 ++ controller/script.go | 4 ++++ helper/handler.go | 2 ++ service/script.go | 19 +++++++++++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/.version b/.version index ccbccc3..276cbf9 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -2.2.0 +2.3.0 diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a6ba35..f44ccda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +### V2.3.0 +* additional batch method ### V2.2.0 * add batch execution ### V2.1.1 diff --git a/controller/script.go b/controller/script.go index 379be08..46df5b2 100644 --- a/controller/script.go +++ b/controller/script.go @@ -17,6 +17,10 @@ func (c scriptController) Execute(req domain.ExecuteRequest) *domain.ScriptResp return service.Script.Execute(req) } +func (c scriptController) BatchExecute(req []domain.ExecuteByIdRequest) []domain.ScriptResp { + return service.Script.BatchExecute(req) +} + func (c scriptController) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []domain.ScriptResp { return service.Script.BatchExecuteById(req) } diff --git a/helper/handler.go b/helper/handler.go index ae5521b..54235d0 100644 --- a/helper/handler.go +++ b/helper/handler.go @@ -8,6 +8,7 @@ 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"` + BatchExecute func(request []domain.ExecuteByIdRequest) []domain.ScriptResp `method:"batch_execute" group:"script"` BatchExecuteByIds func(request domain.BatchExecuteByIdsRequest) []domain.ScriptResp `method:"batch_execute_by_ids" group:"script"` } @@ -15,6 +16,7 @@ func GetTaskHandler() *ScriptHandlers { return &ScriptHandlers{ ExecuteById: controller.Script.ExecuteById, Execute: controller.Script.Execute, + BatchExecute: controller.Script.BatchExecute, BatchExecuteByIds: controller.Script.BatchExecuteById, } } diff --git a/service/script.go b/service/script.go index 6d7b367..c347d22 100644 --- a/service/script.go +++ b/service/script.go @@ -51,6 +51,25 @@ func (s *scriptService) ExecuteById(req domain.ExecuteByIdRequest) (*domain.Scri return s.executeScript(scr, req.Arg), nil } +func (s *scriptService) BatchExecute(req []domain.ExecuteByIdRequest) []domain.ScriptResp { + wg := sync.WaitGroup{} + store := s.store.Load().(map[string]script.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 { + 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) + } + }(i) + } + wg.Wait() + return response +} + func (s *scriptService) BatchExecuteById(req domain.BatchExecuteByIdsRequest) []domain.ScriptResp { wg := sync.WaitGroup{} store := s.store.Load().(map[string]script.Script)