From 2ebeeb20ad0371c2a937b87a326ea54cae457a1a Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 13 May 2023 14:42:24 +0200 Subject: [PATCH] added reload group tasks --- groupTasks/groups/production1/index.json | 9 +++- groupTasks/groups/production1/test2.py | 3 +- grouptasks/grouptasks.go | 55 +++++++++++------------- main.go | 2 +- modules/cache/categorygroup.go | 23 ++++++++++ modules/utils/globals.go | 3 ++ socketserver/hub.go | 16 +++++-- 7 files changed, 74 insertions(+), 37 deletions(-) diff --git a/groupTasks/groups/production1/index.json b/groupTasks/groups/production1/index.json index 573f8bf..40c7f3a 100644 --- a/groupTasks/groups/production1/index.json +++ b/groupTasks/groups/production1/index.json @@ -48,7 +48,14 @@ "onFinish": "nextStep", "undoPossible": false, "scriptPath": "test2.py", - "parameters": [], + "parameters": [ + { + "parameterName": "kundenname", + "type": "text", + "displayName": "Name des Kunden", + "global": true + } + ], "feedback": "" }, { diff --git a/groupTasks/groups/production1/test2.py b/groupTasks/groups/production1/test2.py index 401b0cd..1d99267 100644 --- a/groupTasks/groups/production1/test2.py +++ b/groupTasks/groups/production1/test2.py @@ -1,5 +1,6 @@ import time +import sys time.sleep(5) -print("hello this is test2 lul") \ No newline at end of file +print("hello this is test2 lul") diff --git a/grouptasks/grouptasks.go b/grouptasks/grouptasks.go index c7cc507..3229080 100644 --- a/grouptasks/grouptasks.go +++ b/grouptasks/grouptasks.go @@ -18,7 +18,7 @@ import ( var root = "./groupTasks/groups/" -func ReadGroups() { +func LoadGroups(category string) { entries, err := os.ReadDir(root) if err != nil { @@ -26,9 +26,13 @@ func ReadGroups() { return } - for _, entry := range entries { - log.Info().Msgf("Entry: %s", entry.Name()) + if category != "" { + cache.RemoveAllCategoryGroupsByCategory(category) + } + var updatedGroups []structs.Group + + for _, entry := range entries { files, err := os.ReadDir(root + entry.Name()) if err != nil { @@ -37,8 +41,6 @@ func ReadGroups() { } for _, file := range files { - log.Info().Msgf("File: %s", file.Name()) - if file.Name() == "index.json" { content, err := os.ReadFile(root + entry.Name() + "/index.json") @@ -53,12 +55,27 @@ func ReadGroups() { group.Id = entry.Name() - log.Info().Msgf("Group: %s", group) + if category == "" || group.Category == category { + cache.AddCategoryGroup(group) - cache.AddCategoryGroup(group) + updatedGroups = append(updatedGroups, group) + } } } } + + if category != "" { + socketclients.BroadcastMessage(structs.SendSocketMessage{ + Cmd: utils.SendCmdGroupTasksReloaded, + Body: struct { + Category string + UpdatedGroups []structs.Group + }{ + Category: category, + UpdatedGroups: updatedGroups, + }, + }) + } } const ( @@ -83,13 +100,8 @@ type InputParameters struct { } func RunGroupTask(args RunGroupTaskArgs) { - log.Debug().Msgf("global input: %v", args.GlobalInputs) - categoryGroup := GetCategoryGroupTaskByCategoryAndGroupId(args.Category, args.GroupId) - log.Debug().Msgf("RunGroupTask %s", categoryGroup) - log.Debug().Msgf("script path %s", root+categoryGroup.Id+"/"+categoryGroup.Tasks[args.Step-1].ScriptPath) - groupTaskStep := structs.GroupTaskSteps{ GroupTasksId: args.GroupTaskId, Step: args.Step, @@ -122,28 +134,19 @@ func RunGroupTask(args RunGroupTaskArgs) { var globalInputParameters []InputParameters if len(args.GlobalInputs) > 0 { // global inputs given in args because the group task was just created - log.Info().Msgf("global inputs given %s", args.GlobalInputs) - if err := json.Unmarshal([]byte(args.GlobalInputs), &globalInputParameters); err != nil { log.Error().Msgf("err unmarshalling global inputs %s", err.Error()) } } else { // global inputs not given in args - fetch it from the database - log.Info().Msgf("global inputs not given in args %s", dbGroupTask.GlobalInputs) - if err := json.Unmarshal([]byte(dbGroupTask.GlobalInputs), &globalInputParameters); err != nil { log.Error().Msgf("err unmarshalling global inputs %s", err.Error()) } } // task parameters - log.Info().Msgf("unmarshalled global inputs %s", globalInputParameters) - log.Info().Msgf("task parameters %s", categoryGroup.Tasks[args.Step-1].Parameters) - commandArgs := []string{root + categoryGroup.Id + "/" + categoryGroup.Tasks[args.Step-1].ScriptPath} if len(categoryGroup.Tasks[args.Step-1].Parameters) != 0 && len(args.TaskParameters) == 0 { - log.Error().Msg("task parameters not specified") - updateGroupTask(groupTaskStep.GroupTasksId, structs.GroupTasks{ Status: structs.GroupTasksStatusInputRequired, }) @@ -159,14 +162,10 @@ func RunGroupTask(args RunGroupTaskArgs) { log.Error().Msgf("err unmarshalling task parameter inputs %s", err.Error()) } - log.Info().Msgf("task parameters %s", taskParameterInputs) - for _, taskParameterInput := range taskParameterInputs { //commandArgs = append(commandArgs, "--"+taskParameterInput.ParameterName) commandArgs = append(commandArgs, taskParameterInput.Value) } - - log.Info().Msgf("task parameters %s", commandArgs) } // execute script @@ -194,8 +193,6 @@ func RunGroupTask(args RunGroupTaskArgs) { updateGroupTaskSteps(groupTaskStep) - log.Info().Msgf("run next task") - if int(args.Step) < len(categoryGroup.Tasks) { if groupTaskStep.Status == structs.GroupTasksStatusFailed { // set group task to failed @@ -210,8 +207,6 @@ func RunGroupTask(args RunGroupTaskArgs) { CurrentTasksStep: args.Step, }) - log.Debug().Msgf("RUN NEXT TASK %s", groupTaskStep) - // clear task parameters, because otherwise the next task would have the parameters from the previous task args.TaskParameters = "" @@ -223,8 +218,6 @@ func RunGroupTask(args RunGroupTaskArgs) { Status: structs.GroupTasksStatusFinished, EndedAt: time.Now(), }) - - log.Info().Msg("SET TO FINISHED") } } diff --git a/main.go b/main.go index 4f073a9..6e4615e 100644 --- a/main.go +++ b/main.go @@ -74,7 +74,7 @@ func main() { return fiber.ErrUpgradeRequired }) - grouptasks.ReadGroups() + grouptasks.LoadGroups("") go socketserver.RunHub() socketserver.WebSocketServer(app) diff --git a/modules/cache/categorygroup.go b/modules/cache/categorygroup.go index a716e2d..2dc6bd1 100644 --- a/modules/cache/categorygroup.go +++ b/modules/cache/categorygroup.go @@ -2,6 +2,7 @@ package cache import ( "janex/admin-dashboard-backend/modules/structs" + "sort" "sync" ) @@ -28,6 +29,28 @@ func AddCategoryGroup(group structs.Group) { CategoryGroups = append(CategoryGroups, categoryGroup) } +func RemoveAllCategoryGroupsByCategory(category string) { + for index, categoryGroup := range GetCategoryGroups() { + if categoryGroup.Category == category { + cgMu.Lock() + defer cgMu.Unlock() + + CategoryGroups = append(CategoryGroups[:index], CategoryGroups[index+1:]...) + } + } +} + +func GetCategoryGroupsSorted() []structs.CategoryGroup { + categoryGroups := GetCategoryGroups() + + cgMu.Lock() + defer cgMu.Unlock() + + sort.SliceStable(categoryGroups, func(i, j int) bool { return categoryGroups[i].Category < categoryGroups[j].Category }) + + return categoryGroups +} + func GetCategoryGroups() []structs.CategoryGroup { cgMu.RLock() defer cgMu.RUnlock() diff --git a/modules/utils/globals.go b/modules/utils/globals.go index 25c20ac..195baf9 100644 --- a/modules/utils/globals.go +++ b/modules/utils/globals.go @@ -22,6 +22,8 @@ const ( SentCmdNewGroupTaskStep = 4 SentCmdUpdateGroupTaskStep = 5 SentCmdUpdateGroupTask = 6 + SentCmdReloadingGroupTasks = 7 + SendCmdGroupTasksReloaded = 8 ) // commands received from web clients @@ -29,6 +31,7 @@ const ( ReceivedCmdStartGroupTasks = 1 ReceivedCmdTaskFailedTryAgainRunTaskStep = 2 ReceivedCmdTaskContinueTaskStep = 3 + ReceivedCmdReloadGroupTasks = 4 ) var ( diff --git a/socketserver/hub.go b/socketserver/hub.go index 82451da..ccfd3e8 100644 --- a/socketserver/hub.go +++ b/socketserver/hub.go @@ -52,7 +52,7 @@ func RunHub() { Username: user.Username, Email: user.Email, }, - CategoryGroups: cache.CategoryGroups, + CategoryGroups: cache.GetCategoryGroupsSorted(), GroupTasks: grouptasks.GetAllGroupTasks(), GroupTasksSteps: grouptasks.GetAllGroupTasksSteps(), }, @@ -134,8 +134,6 @@ func RunHub() { }) break case utils.ReceivedCmdTaskContinueTaskStep: - log.Info().Msgf("task continue task %s", receivedMessage.Body) - taskInputs := receivedMessage.Body["taskInputs"] var taskInputsJsonString string @@ -162,6 +160,18 @@ func RunHub() { }) break + case utils.ReceivedCmdReloadGroupTasks: + category := receivedMessage.Body["category"].(string) + + socketclients.BroadcastMessage(structs.SendSocketMessage{ + Cmd: utils.SentCmdReloadingGroupTasks, + Body: category, + }) + + grouptasks.LoadGroups(category) + + break + default: log.Error().Msgf("Received unknown message: %s", receivedMessage) break