added reload group tasks

main
alex 2023-05-13 14:42:24 +02:00
parent 18b7d63363
commit 2ebeeb20ad
7 changed files with 74 additions and 37 deletions

View File

@ -48,7 +48,14 @@
"onFinish": "nextStep",
"undoPossible": false,
"scriptPath": "test2.py",
"parameters": [],
"parameters": [
{
"parameterName": "kundenname",
"type": "text",
"displayName": "Name des Kunden",
"global": true
}
],
"feedback": ""
},
{

View File

@ -1,4 +1,5 @@
import time
import sys
time.sleep(5)

View File

@ -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)
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")
}
}

View File

@ -74,7 +74,7 @@ func main() {
return fiber.ErrUpgradeRequired
})
grouptasks.ReadGroups()
grouptasks.LoadGroups("")
go socketserver.RunHub()
socketserver.WebSocketServer(app)

View File

@ -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()

View File

@ -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 (

View File

@ -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