From 3e1485faf5ef3da758668beed4740796b55c5220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Roth?= Date: Sat, 8 Jun 2024 12:27:59 +0200 Subject: [PATCH] queue sync calls --- api/api.go | 14 +++++++++++--- task/list.go | 12 ++++++++++++ task/task.go | 1 + 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/api/api.go b/api/api.go index 6124256df..b173986dd 100644 --- a/api/api.go +++ b/api/api.go @@ -164,9 +164,17 @@ func maybeRunTaskInBackground(c *gin.Context, name string, resources []string, p c.JSON(202, task) } else { log.Debug().Msg("Executing task synchronously") - out := context.Progress() - detail := task.Detail{} - retValue, err := proc(out, &detail) + task, conflictErr := runTaskInBackground(name, resources, proc) + if conflictErr != nil { + AbortWithJSONError(c, 409, conflictErr) + return + } + + // wait for task to finish + context.TaskList().WaitForTaskByID(task.ID) + + retValue, _ := context.TaskList().GetTaskReturnValueByID(task.ID) + err, _ := context.TaskList().GetTaskErrorByID(task.ID) if err != nil { AbortWithJSONError(c, retValue.Code, err) return diff --git a/task/list.go b/task/list.go index e72faba65..3636850c1 100644 --- a/task/list.go +++ b/task/list.go @@ -56,6 +56,7 @@ func (list *List) consumer() { list.Lock() { task.processReturnValue = retValue + task.err = err if err != nil { task.output.Printf("Task failed with error: %v", err) task.State = FAILED @@ -241,3 +242,14 @@ func (list *List) WaitForTaskByID(ID int) (Task, error) { wgTask.Wait() return list.GetTaskByID(ID) } + +// GetTaskError returns the Task error for a given id +func (list *List) GetTaskErrorByID(ID int) (error, error) { + task, err := list.GetTaskByID(ID) + + if err != nil { + return nil, err + } + + return task.err, nil +} diff --git a/task/task.go b/task/task.go index ed43d3292..02aa7037b 100644 --- a/task/task.go +++ b/task/task.go @@ -47,6 +47,7 @@ type Task struct { detail *Detail process Process processReturnValue *ProcessReturnValue + err error Name string ID int State State